foreign-keys

Foreign keys referring other foreign keys in PostgreSQL

那年仲夏 提交于 2019-12-02 06:43:21
In PostgreSQL I have a database, which I intend to make the following table declaration: CREATE TABLE canvas_user ( id INTEGER, login_id VARCHAR(50) UNIQUE NOT NULL, email VARCHAR(355) UNIQUE NOT NULL, name_given VARCHAR(30), name_family VARCHAR(30), name_full VARCHAR(50), role canvas_role, last_login TIMESTAMP, PRIMARY KEY (id) ); CREATE TABLE problem ( id SERIAL, title VARCHAR(50), author VARCHAR(50), path TEXT, compiler VARCHAR(20), PRIMARY KEY (id) ); CREATE TABLE assignment ( id INTEGER, title TEXT NOT NULL, points_possible INTEGER NOT NULL, problem_id INTEGER, PRIMARY KEY (id), FOREIGN

database design: a 'code' table that get referenced by other entities

心不动则不痛 提交于 2019-12-02 05:58:06
I am building a database as a simple exercise, it could be hosted on any database server, so I am trying to keep things as much standard as possible. Basically what I would like to do is a 'code' table that get referenced by other entities. I explain: xcode id code r role p property code r admin r staff p title .... then I would like to have some view like: role (select * from code where xcode='r') r admin r staff property (select * from code where xcode='p') p title then, suppose we have an entity myentity id - 1 role - admin (foreign key to role) title - title (foreign key to property)

Doctrine2 Map entities with composite foreign keys in the composite primary keys

浪子不回头ぞ 提交于 2019-12-02 05:44:57
问题 I have a model which has many tables, but in this case we only need three. The point is that the composite primary key of one is also the foreing key (composite too) and Symfony throws this exception: MappingException: It is not possible to map entity 'Your\SomethingBundle\Entity\Empleado' with a composite primary key as part of the primary key of another entity 'Your\SomethingBundle\Entity\EmpleadoHorario#empleado' . Here I explain the relationship: 1º Salon, it has a primary key ID 2º

MySQL InnoDB constraint does not work

谁说我不能喝 提交于 2019-12-02 05:03:39
I stumble upon strange behavior with innoDB constraint, and cannot find cause of it. I have tables with data. Below listed their structures: CREATE TABLE `contents` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`), KEY `title` (`title`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `fields` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(45) NOT NULL, `type` varchar(45) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `nameUNIQUE` (`name`), KEY `name` (`name`), KEY `type` (`type`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE

primary key and foreign key

自闭症网瘾萝莉.ら 提交于 2019-12-02 04:49:00
I have 3 tables Student Loan Book - StudentID LoanID BookID which foreign keys do i need to set so when given the student name, search all loan from that student and display the book detail Here's a start with such vague requirements: CREATE TABLE dbo.Students ( StudentID INT PRIMARY KEY -- , other columns about students ); CREATE TABLE dbo.Loans ( LoanID INT PRIMARY KEY, StudentID INT NOT NULL FOREIGN KEY REFERENCES dbo.Students(StudentID) -- , other columns about loans ); CREATE TABLE dbo.Books ( BookID INT PRIMARY KEY, -- , other columns about books ); CREATE TABLE dbo.StudentBooks (

For a composite foreign key, is a/why is a composite UNIQUE constraint in the referenced table required for a column combination with a primary key?

房东的猫 提交于 2019-12-02 04:14:48
I have a question regarding explicitly defining of the uniqueness of something. This relates to the creation of a composite foreign key. I've created an example below to try and make my question as clear as possible (I've included some data inserts for ease of testing). Each entry for [Table1] must have a unique [Name] . CREATE TABLE [Table1] ( [ID] INT IDENTITY NOT NULL PRIMARY KEY, [Name] NVARCHAR(255) UNIQUE NOT NULL CHECK(LTRIM(RTRIM([Name])) <> '') ); INSERT INTO [Table1]([Name]) VALUES ('Name 1'), ('Name 2'), ('Name 3'), ('Name 4'), ('Name 5'), ('Name 6'), ('Name 7') Each [Value] in

Unable to determine the principle end of an association

可紊 提交于 2019-12-02 04:02:08
Using EF5 Code first, I have two classes: [Table("UserProfile")] public class UserProfile { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime DateOfBirth { get; set; } [ForeignKey("AddressId")] public virtual Address Address { get; set; } } [Table("Address")] public class Address : IEntity { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int AddressId { get; set; } public string Address1 { get; set; } public string Address2 { get; set; }

Possible to condense primary key/serial?

半城伤御伤魂 提交于 2019-12-02 03:55:03
问题 I have a database where everything is linked with foreign keys, so the Postgres knows exactly how the database is layed out.. Well, Lets say I have Table1 and Table2. Table1 has 3 fields. RID, table2_rid,data So table1.table2_rid references table2.RID and this is expressed with a foreign key. In both the RID field is the primary key and is a serial type. What I would like to know is how to "condense" the primary keys? Like say you add 5 records and deleted record number 3. Your primary keys

How can I create table as select (CTAS) in oracle?

一笑奈何 提交于 2019-12-02 03:54:26
问题 I need to use CTAS (Create Table As Select) to create a table named Au_Books_ZL that contains au_id, fname, lname, title_id, title, Pub_id, price and revenue (which is price*sales) . I have browsed other questions online, but they don't show how to include all the attributes (lname, fname, title_id ect.) in the query. How could I write up my CTAS to create the new table? 回答1: The syntax for creating a table would be something like CREATE TABLE au_books_zl AS SELECT au_id, fname, lname, title

ERROR 1005 (HY000): Can't create table 'shrewd_db.alert_disable_register' (errno: 150)

好久不见. 提交于 2019-12-02 03:49:46
I want to create a table in MySQL by running following SQL, CREATE TABLE IF NOT EXISTS `shrewd_db`.`alert_disable_register` ( `id_alert_disable_register` MEDIUMINT NOT NULL AUTO_INCREMENT, `id_label` MEDIUMINT UNSIGNED NULL, `id_indicator` MEDIUMINT UNSIGNED NULL, `id_user` MEDIUMINT UNSIGNED NULL, `active` TINYINT(1) NULL DEFAULT 1, `id_alert_disable_rule` MEDIUMINT NULL, `id_escalation_plan` INT NULL, PRIMARY KEY (`id_alert_disable_register`), INDEX `id_escalation_plan_alert_rule_idx` (`id_alert_disable_rule` ASC), INDEX `id_label_idx` (`id_label` ASC), INDEX `id_indicator_idx` (`id