foreign-keys

How to deal with mutually recursive inserts

此生再无相见时 提交于 2019-12-18 03:42:00
问题 I have a model that defines mutually recursive tables: Answer questionId QuestionId text Question text correct AnswerId What do I need to do to actually insert a question? I need to know what the correct answer is first. But to insert an answer, I need to know what question it answers. I'm running Postgres, if it matters. The DDL is: CREATE TABLE answer ( id integer NOT NULL, -- answer id text character varying NOT NULL, -- answer text question_id bigint NOT NULL -- question id ); CREATE

unable to drop the foreign key

别来无恙 提交于 2019-12-18 01:42:32
问题 I would like to drop the foreign key in my table but been into this error message mysql> alter table customers drop foreign key customerid; ERROR 1025 (HY000): Error on rename of '.\products\customers' to '.\products\#sql2-7ec-a3' (errno: 152) mysql> 回答1: To avoid getting this error while trying to drop a foreign key, use the constraint name rather than the column name of the foreign key. When I tried mysql> ALTER TABLE mytable DROP PRIMARY KEY; I got error as ERROR 1025 (HY000): Error on

How to list out all Foreign Keys with “WITH NOCHECK” in SQL Server

十年热恋 提交于 2019-12-17 22:44:13
问题 Does anyone know a query for listing out all foreign keys in a database with "WITH NOCHECK" description applied to it? (removing them will boost performance and stability). 回答1: The following will return the name of the foreign keys in the current database that are disabled i.e. WITH NOCHECK For SQL Server 2005/2008: select * from sys.foreign_keys where is_disabled=1 There was some discussion in the answer about the difference between disabled & not trusted. What's below explains the

Can Foreign Key be null? [duplicate]

大城市里の小女人 提交于 2019-12-17 22:42:52
问题 This question already has answers here : Can a foreign key be NULL and/or duplicate? (11 answers) Closed 5 years ago . In our database project we have a table Sale that has an primary key and two exclusive foreign keys: Vehicle_ID and Piece_ID . For example if we sell a vehicle we need Vehicle_ID as a foreign key but not Piece_ID . Can we put NULL to Piece_ID , could a foreign key be null? Or is there a way to do this job? Thanks. 回答1: The column (or columns) of a primary key must be NOT NULL

How to disable Constraints for all the tables and enable it?

跟風遠走 提交于 2019-12-17 22:33:59
问题 I have 60 tables. I want to drop 10 tables where these 10 tables are Constraints(PK,FK) to other 20 tables. While dropping these 10 tables, I need to truncate or delete data from the other 20 tables. Finally I want to disable all 60 table Constraints(FK,PK) and then enable all 60 table constraints after I am done with my work of adding/dropping tables. Is this possible? When I drop a table it is asking for FK. When I truncate those FK dependencies it also is still showing the same. I don't

Django: limit_choices_to (Is this correct)

Deadly 提交于 2019-12-17 22:22:54
问题 Is this correct? class Customer(models.Model): account = models.ForeignKey(Account) class Order(models.Model): account = models.ForeignKey(Account) customer = models.ForeignKey(Customer, limit_choices_to={'account': 'self.account'}) I'm trying to make sure that an Order form will only display customer choices that belong to the same account as the Order. If I'm overlooking some glaring bad-design fallacy, let me know. The main thing I'm concerned with is: limit_choices_to={'account': 'self

Can a foreign key refer to a primary key in the same table?

邮差的信 提交于 2019-12-17 22:17:16
问题 I just think that the answer is false because the foreign key doesn't have uniqueness property. But some people said that it can be in case of self joining the table. I am new to SQL . If its true please explain how and why? Employee table | e_id | e_name | e_sala | d_id | |---- |------- |----- |--------| | 1 | Tom | 50K | A | | 2 | Billy | 15K | A | | 3 | Bucky | 15K | B | department table | d_id | d_name | |---- |------- | | A | XXX | | B | YYY | Now, d_id is foreign key so how it can be a

How to select SQL results based on multiple tables

被刻印的时光 ゝ 提交于 2019-12-17 22:14:54
问题 I need to select results from one table based on certain matching values in a couple of other tables. I have the following tables: person: id, firstname, lastname team: id, teamname player: id, person_id(FK), team_id(FK) coach: id, person_id(FK), team_id(FK) I need to return all the coaches and players names for each team. I've only ever used inner joins, and it doesn't seem like I can use those here, so any idea how to do this? 回答1: This will give you the coach: SELECT team.Teamname, person

MySQL terminology “constraints” vs “foreign keys” difference?

ぃ、小莉子 提交于 2019-12-17 21:44:39
问题 I'm looking at the MySQL docs here and trying to sort out the distinction between FOREIGN KEYs and CONSTRAINTs. I thought an FK was a constraint, but the docs seem to talk about them like they're separate things. The syntax for creating an FK is (in part)... [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name, ...) REFERENCES tbl_name (index_col_name,...) So the "CONSTRAINT" clause is optional. Why would you include it or not include it? If you leave it out does MySQL create a

MySQL Error Code 1452 Foreign Key Constraint

你说的曾经没有我的故事 提交于 2019-12-17 21:31:32
问题 I'm receiving an error when I attempt to create two tables. There was a multivalued dependency, so I separated the tables and came up with this: CREATE TABLE NAME ( NameID Integer NOT NULL AUTO_INCREMENT, Name varChar(255) NOT NULL, CONSTRAINT NAME_PK PRIMARY KEY(NameID) ); CREATE TABLE PHONE ( NameID Integer NOT NULL, PhoneNumber varChar(15) NOT NULL, NumType varChar(5) NOT NULL, CONSTRAINT PHONE_FK FOREIGN KEY(NameID) REFERENCES NAME(NameID), CONSTRAINT PHONE_PK PRIMARY KEY(NameID) ); But