foreign-keys

How can I INSERT data into two tables simultaneously in SQL Server?

冷暖自知 提交于 2019-12-17 04:19:06
问题 Let's say my table structure looks something like this: CREATE TABLE [dbo].[table1] ( [id] [int] IDENTITY(1,1) NOT NULL, [data] [varchar](255) NOT NULL, CONSTRAINT [PK_table1] PRIMARY KEY CLUSTERED ([id] ASC) ) CREATE TABLE [dbo].[table2] ( [id] [int] IDENTITY(1,1) NOT NULL, [table1_id] [int] NOT NULL, [data] [varchar](255) NOT NULL, CONSTRAINT [PK_table2] PRIMARY KEY CLUSTERED ([id] ASC) ) The [id] field of the first table corresponds to the [table1_id] field of the second. What I would like

MySQL Errno 150

若如初见. 提交于 2019-12-17 03:42:46
问题 I'm creating a few simple tables and I can't get passed this foreign key error and I'm not sure why. Here's the script below. create TABLE Instructors ( ID varchar(10), First_Name varchar(50) NOT NULL, Last_Name varchar(50) NOT NULL, PRIMARY KEY (ID) ); create table Courses ( Course_Code varchar(10), Title varchar(50) NOT NULL, PRIMARY KEY (Course_Code) ); create table Sections ( Index_No int, Course_Code varchar(10), Instructor_ID varchar(10), PRIMARY KEY (Index_No), FOREIGN KEY (Course_Code

When/Why to use Cascading in SQL Server?

雨燕双飞 提交于 2019-12-17 02:55:13
问题 When setting up foreign keys in SQL Server, under what circumstances should you have it cascade on delete or update, and what is the reasoning behind it? This probably applies to other databases as well. I'm looking most of all for concrete examples of each scenario, preferably from someone who has used them successfully. 回答1: Summary of what I've seen so far: Some people don't like cascading at all. Cascade Delete Cascade Delete may make sense when the semantics of the relationship can

Migration: Cannot add foreign key constraint in laravel

て烟熏妆下的殇ゞ 提交于 2019-12-17 02:54:09
问题 I'm trying to create foreign keys in Laravel however when I migrate my table using artisan i am thrown the following error: [Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL : alter table `priorities` add constraint priorities_user_id_foreign foreign key (`user_id`) references `users` (`id`)) My migration code is as so: priorities migration file public function up() { // Schema::create('priorities', function($table) { $table-

Cannot truncate table because it is being referenced by a FOREIGN KEY constraint?

做~自己de王妃 提交于 2019-12-17 02:33:26
问题 Using MSSQL2005, can I truncate a table with a foreign key constraint if I first truncate the child table (the table with the primary key of the FK relationship)? I know that I can either Use a DELETE without a where clause and then RESEED the identity (or) Remove the FK, truncate the table, and recreate the FK. I thought that as long as I truncated the child table before the parent, I'd be okay without doing either of the options above, but I'm getting this error: Cannot truncate table

Add Foreign Key to existing table

孤者浪人 提交于 2019-12-17 02:18:08
问题 I want to add a Foreign Key to a table called "katalog". ALTER TABLE katalog ADD CONSTRAINT `fk_katalog_sprache` FOREIGN KEY (`Sprache`) REFERENCES `Sprache` (`ID`) ON DELETE SET NULL ON UPDATE SET NULL; When I try to do this, I get this error message: Error Code: 1005. Can't create table 'mytable.#sql-7fb1_7d3a' (errno: 150) Error in INNODB Status: 120405 14:02:57 Error in foreign key constraint of table mytable.#sql-7fb1_7d3a: FOREIGN KEY (`Sprache`) REFERENCES `Sprache` (`ID`) ON DELETE

How to select rows with no matching entry in another table?

人盡茶涼 提交于 2019-12-17 02:17:46
问题 I'm doing some maintenance work on a database application and I've discovered that, joy of joys, even though values from one table are being used in the style of foreign keys, there's no foreign key constraints on the tables. I'm trying to add FK constraints on these columns, but I'm finding that, because there's already a whole load of bad data in the tables from previous errors which have been naively corrected, I need to find the rows which don't match up to the other table and then delete

Error Code: 1005. Can't create table '…' (errno: 150)

瘦欲@ 提交于 2019-12-17 01:40:21
问题 I searched for a solution to this problem on the Internet and checked the Stack Overflow questions, but none of the solutions worked for my case. I want to create a foreign key from table sira_no to metal_kod. ALTER TABLE sira_no ADD CONSTRAINT METAL_KODU FOREIGN KEY(METAL_KODU) REFERENCES metal_kod(METAL_KODU) ON DELETE SET NULL ON UPDATE SET NULL ; This script returns: Error Code: 1005. Can't create table 'ebs.#sql-f48_1a3' (errno: 150) I tried adding an index to the referenced table:

MySQL - Conditional Foreign Key Constraints

心不动则不痛 提交于 2019-12-17 00:46:18
问题 I have following 'comments' table in my app: comments -------- id INT foreign_id INT model TEXT comment_text TEXT ... the idea of this table is to store comments for various parts of my app - it can store comments for blog post i.e: 1|34|blogpost|lorem ipsum... user picture: 2|12|picture|lorem ipsum... and so on. now, is there a way to force FOREIGN KEY constraint on such data? i.e. something like this in comments table: FOREIGN KEY (`foreign_id`) REFERENCES blogposts (`id`) //but only when

How do I see all foreign keys to a table or column?

徘徊边缘 提交于 2019-12-16 19:52:23
问题 In MySQL, how do I get a list of all foreign key constraints pointing to a particular table? a particular column? This is the same thing as this Oracle question, but for MySQL. 回答1: For a Table: SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '<database>' AND REFERENCED_TABLE_NAME = '<table>'; For a Column: SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME