foreign-keys

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

主宰稳场 提交于 2019-12-31 04:04: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

Cant drop key even though it exists: Error Code: 1091

喜夏-厌秋 提交于 2019-12-31 02:57:28
问题 I have a table which looks like this: CREATE TABLE `articles` ( `article_id` INT(10) NOT NULL auto_increment, `pubsrc_id` INT(10) NOT NULL, `pub_id` VARCHAR(16) NOT NULL PRIMARY KEY (`article_id`), UNIQUE KEY `pubsrc_id` (`pubsrc_id`,`pub_id`), CONSTRAINT `pub_articles_ibfk_1` FOREIGN KEY (`pubsrc_id`) REFERENCES `pub_sources` (`pubsrc_id`) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT `pub_articles_ibfk_2` FOREIGN KEY (`pubsrc_id`) REFERENCES `pub_sources` (`pubsrc_id`) ) There is

Cross Database foreign key error

北战南征 提交于 2019-12-31 02:45:11
问题 This my model of first Database DB1: from django.db import models class Company(models.Model): name = models.CharField(max_length=100, null=True) address = models.TextField(max_length=200, null=True) website = models.CharField(max_length=200, null=True) conatct_no = models.CharField(max_length=20, null=True) email = models.EmailField(max_length=20, null=True) logo = models.FileField(upload_to='logo/', blank=True, null=True) created = models.DateTimeField('company created', auto_now_add=True)

Why 'foreign key constraint fails' when foreign key exists?

心不动则不痛 提交于 2019-12-30 18:25:50
问题 I have a simple query UPDATE `t_timecard_detail` SET `timeoff_request_id` = 'adad8e0d-c22b-41c3-a460-6cf982729299' WHERE `id` = 'cfc7a0a1-4e03-46a4-af89-069a0661cf55'; which gives this error ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`demo1_timeapp`.`t_timecard_detail`, CONSTRAINT `timeoff_request_id_refs_id_48fe5c4` FOREIGN KEY (`timeoff_request_id`) REFERENCES `t_timeoff_request` (`id`)) constraint is CONSTRAINT `timeoff_request_id_refs_id_48fe5c4`

Advice on design relations between tables

大憨熊 提交于 2019-12-30 12:54:09
问题 I have information about music albums that I want to organise in RDBMS tables with relations between them. I have the following info for each album: artist, album name, year, label, genre, style, rating. So far I think to make 4 tables - artists, albums (name, year, label, rating), genre1 and genre2 (each genre with its styles). On the diagram it looks as follows: But don't know yet how can I establish a connection between albums and the other three tables? I.e., when I will run a query

SQL Table Foreign Key that is part of a Composite Primary Key

倖福魔咒の 提交于 2019-12-30 09:22:08
问题 Is it possible to have a table's foreign key be part of another table's composite primary key? For example, if I have two tables, one contains information on all active projects of different users and another containing information on what equipment is being used by the projects: Project Table: Composite Primary Keys: UserId, ProjectId (neither are unique by themselves) Equipment Table: Composite Primary Keys: UserId, ProjectId, EquipmentId (neither are unique by themselves) Now is it

Entity Framework Is it possible to add an ASSOCIATION between Primary Keys and a Foreign Key

北城以北 提交于 2019-12-30 09:06:09
问题 I've got the following entities on my EDMX :- These two entites were generated by Update Model From Database . Now, notice how my country has the following primary key :- Name & IsoCode this is because each country is UNIQUE in the system by Name and IsoCode. Now, with my States ... it's similar. Primary Key is :- Name & CountryId Each state is unique by name and per country. Now, the Foreign Key for States is a CountryId. This is the sql :- ALTER TABLE [dbo].[States] WITH CHECK ADD

Adding foreign key on multiple columns

隐身守侯 提交于 2019-12-30 08:11:48
问题 I'm trying to create a foreign key on two columns of a table to point to the same column of another table, but I seem to get an error... Here's what I do: CREATE TABLE test2 ( ID INT NOT NULL AUTO_INCREMENT, col1 INT NOT NULL, col2 INT NOT NULL, PRIMARY KEY (ID), CONSTRAINT fk FOREIGN KEY (col1, col2) REFERENCES test1(ID, ID) ON UPDATE CASCADE ON DELETE RESTRICT ) ENGINE=InnoDB; But I get ERROR 1005 (HY000): Can't create table 'DB.test2' (errno: 150) If I only have one column, however, the

Is it possible to add a logic Constraint to a Foreign Key?

独自空忆成欢 提交于 2019-12-30 06:38:31
问题 I've got two tables and I've added a foreign key constraint. Kewl - works great. Now, is it possible to further constrain that relationship against some data in the parent table? Basically, I have animals in the parent table, and for the child table wishto only contain data where the parent data are .... um .. mammals. eg. Animals ^^^^^^^ AnimalId INT PK NOT NULL IDENTITY AnimalType TINYINT NOT NULL -- 1: Mammal, 2:Reptile, etc.. Name Mammals ^^^^^^^ AnimalId INT PK FK NOT NULL

Hibernate criteria query using Max() projection on key field and group by foreign primary key

∥☆過路亽.° 提交于 2019-12-30 06:32:08
问题 I'm having difficulty representing this query (which works on the database directly) as a criteria query in Hibernate (version 3.2.5): SELECT s.* FROM ftp_status s WHERE (s.datetime,s.connectionid) IN (SELECT MAX(f.datetime), f.connectionid FROM ftp_status f GROUP BY f.connectionid); so far this is what I've come up with that doesn't work, and throws a could not resolve property: datetime of: common.entity.FtpStatus error message: Criteria crit = s.createCriteria(FtpStatus.class); crit = crit