foreign-keys

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

送分小仙女□ 提交于 2019-11-28 20:06:54
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). 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 differnce Here's some code to clarify the difference between is_disabled & isnotrusted. -- drop table t1 -- drop

View all foreign key constraints for entire MySQL database

梦想的初衷 提交于 2019-11-28 20:06:10
I have a large database with over 150 tables that I've recently been handed. I'm just wondering if there is an easy way to view all foreign key constraints for the entire DB instead of on a per-table basis. You can use the INFORMATION_SCHEMA tables for this. For example, the INFORMATION_SCHEMA TABLE_CONSTRAINTS table. Something like this should do it: select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where CONSTRAINT_TYPE = 'FOREIGN KEY' This is what I prefer to get useful informations: SELECT CONSTRAINT_NAME, UNIQUE_CONSTRAINT_NAME, MATCH_OPTION, UPDATE_RULE, DELETE_RULE, TABLE_NAME,

Can Foreign Key be null? [duplicate]

Deadly 提交于 2019-11-28 19:43:37
This question already has an answer here: Can a foreign key be NULL and/or duplicate? 11 answers 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. The column (or columns) of a primary key must be NOT NULL. A record cannot be uniquely identified by a NULL. So the ID columns on the referenced end of the foreign key must be defined as

How to update foreign key value in mysql database

萝らか妹 提交于 2019-11-28 19:21:21
问题 I have three tables: categories, languages and categories_languages. Categories_languages is many to many table which links together categories and languages. I would like to update a foregin key value in table languages but it throws me error #1451 - Cannot delete or update a parent row: a foreign key constraint fails! CREATE TABLE IF NOT EXISTS `categories` ( `id` int(11) unsigned NOT NULL auto_increment, `name` varchar(20) NOT NULL, `modified` int(10) unsigned NOT NULL, PRIMARY KEY (`id`),

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

戏子无情 提交于 2019-11-28 19:04:55
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 primary key. And explain something about join . What is its use? mvsagar I think the question is a bit

Primary and Foreign Key at the same time

对着背影说爱祢 提交于 2019-11-28 18:35:40
Would it be possible in SQL Server 2008 to have a table created with 2 columns that are at the same time primary and foreign keys? If yes, how would such a code look like? I've searched and came up with nothing. Sure, no problem: CREATE TABLE dbo.[User] ( Id int NOT NULL IDENTITY PRIMARY KEY, Name nvarchar(1024) NOT NULL ); CREATE TABLE [Group] ( Id int NOT NULL IDENTITY PRIMARY KEY, Name nvarchar(1024) NOT NULL ); CREATE TABLE [UserToGroup] ( UserId int NOT NULL, GroupId int NOT NULL, PRIMARY KEY CLUSTERED ( UserId, GroupId ), FOREIGN KEY ( UserId ) REFERENCES [User] ( Id ) ON UPDATE NO

Setting default value for Foreign Key attribute

烈酒焚心 提交于 2019-11-28 18:32:15
What is the best way to set a default value for a foreign key field in a model? Suppose I have two models, Student and Exam with student having exam_taken as foreign key. How would I ideally set a default value for it? Here's a log of my effort class Student(models.Model): .... ..... exam_taken = models.ForeignKey("Exam", default=1) Works, but have a hunch there's a better way. def get_exam(): return Exam.objects.get(id=1) class Student(models.Model): .... ..... exam_taken = models.ForeignKey("Exam", default=get_exam) From here , but fails with tables does not exist error while syncing. Any

How to select SQL results based on multiple tables

限于喜欢 提交于 2019-11-28 17:53:47
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? Chris Cunningham This will give you the coach: SELECT team.Teamname, person.Firstname, person.Lastname FROM person JOIN coach ON person.id = coach.person_id JOIN team

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

被刻印的时光 ゝ 提交于 2019-11-28 17:52:14
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 want to mess with all those FK,PK. I want to know smarter method. Stefan Steiger EXEC sp_MSforeachtable

Why do I need to use foreign key if I can use WHERE?

时间秒杀一切 提交于 2019-11-28 17:49:14
A beginners' question about foreign key in MySQL. In w3school it says, A FOREIGN KEY in one table points to a PRIMARY KEY in another table. And also there is WHERE, WHERE id = page_id So if I can use WHERE for linking the tables, what is the main purpose of having foreign key? It's not strictly needed for the query, it's true. It exists for several reasons: As a constraint on the table to stop you inserting something that doesn't point to anything; As a clue for the optimizer; and For historical reasons where is was more needed. (1) is probably the important one of the three. This is called