Add new column with foreign key constraint in one command


mushroom

I am trying to add a new column which will be a foreign key. I've been able to add column and foreign key constraints using two separate ALTER TABLEcommands :

ALTER TABLE one
ADD two_id integer;

ALTER TABLE one
ADD FOREIGN KEY (two_id) REFERENCES two(id);

Is there a way to use one ALTER TABLE command instead of two? I can't come up with anything that would work.

Jonathan Leffler

As is often the case with SQL-related issues, it depends on the DBMS. Some DBMSs allow you to combine comma-separated ALTER table operations. E.g...

Informix syntax:

ALTER TABLE one
    ADD two_id INTEGER,
    ADD CONSTRAINT FOREIGN KEY(two_id) REFERENCES two(id);

IBM DB2 LUW has a similar syntax , repeating the keyword ADD, but (if I'm reading the diagram correctly) does not require commas to separate added items.

Microsoft SQL Server syntax:

ALTER TABLE one
    ADD two_id INTEGER,
    FOREIGN KEY(two_id) REFERENCES two(id);

Some others don't allow you to combine ALTER TABLE operations like this. Standard SQL only allows a single operation to be performed in an ALTER TABLE statement, so in standard SQL it must be done in two steps.

Related


Cannot add foreign key constraint

gacat : I checked this answer and made sure both are correct. But there is still the problem of creating constraints: Model: user.js 'use strict'; module.exports = (sequelize, DataTypes) => { const User = sequelize.define('User', { id: { allowNull:

Add new column with foreign key constraint in one command

mushroom I am trying to add a new column which will be a foreign key. I've been able to add column and foreign key constraints using two separate ALTER TABLEcommands : ALTER TABLE one ADD two_id integer; ALTER TABLE one ADD FOREIGN KEY (two_id) REFERENCES two

Cannot add foreign key constraint

Wabaf Chauhan I am not able to add foreign key constraints to the database I am creating errors like this, let me know if there is something wrong with my logic design, I am trying to create a REST api that utilizes this database structure. I'm trying to pract

Failed to add foreign key constraint

Teju_M So I am creating a database and this is the error I keep getting on the last table ERROR 1822 (HY000): Failed to add foreign key constraint. Missing index for constraint 'invoice_ibfk_3' in reference table 'ITEMS' I'm still not sure where exactly I am g

Cannot add foreign key constraint

Wabaf Chauhan I am not able to add foreign key constraints to the database I am creating errors like this, let me know if there is something wrong with my logic design, I am trying to create a REST api that utilizes this database structure. I'm trying to pract

Cannot add foreign key constraint

Juliver Galleto First, here is my main reference table mysql dump -- phpMyAdmin SQL Dump -- version 4.1.14 -- http://www.phpmyadmin.net -- -- Host: 127.0.0.1 -- Generation Time: Nov 17, 2015 at 03:15 AM -- Server version: 5.6.17 -- PHP Version: 5.5.12 SET SQL

Cannot add foreign key constraint

Juliver Galleto First, here is my main reference table mysql dump -- phpMyAdmin SQL Dump -- version 4.1.14 -- http://www.phpmyadmin.net -- -- Host: 127.0.0.1 -- Generation Time: Nov 17, 2015 at 03:15 AM -- Server version: 5.6.17 -- PHP Version: 5.5.12 SET SQL

SQL - Unable to add new foreign key constraint on SQL Fiddle

Tony Slepian I'm working on SQL Fiddle and so far the code looks like this: CREATE TABLE IF NOT EXISTS `orders` ( `id` int(6) unsigned NOT NULL, `date` DATE NOT NULL, `customerID` int(6) unsigned NOT NULL, `paymentAmmount` float(6) unsigned NOT NULL, PR

Unable to add new column with foreign key

Matheen Ulla I am new to laravel and I am getting the above error when adding a new column to an existing table: SQLSTATE[23000]: Integrity constraint violated: 1452 Cannot add or update child row: foreign key constraint failed ( schoolmanagement. #sql-314_2c,

Add a foreign key constraint on the view

Arsnow On ibm db2 database I have two tables TABLE1 and TABLE2. I also have a logical file LOGICAL1 on TABLE1, LOGICAL1 filters records from TABLE1 based on the values of some TABLE1 fields. (like a SQL view) I want to add an outer constraint between TABLE2 an

Cannot add foreign key constraint

gacat : I checked this answer and made sure both are correct. But there is still the problem of creating constraints: Model: user.js 'use strict'; module.exports = (sequelize, DataTypes) => { const User = sequelize.define('User', { id: { allowNull:

Add new column with foreign key constraint in one command

mushroom I am trying to add a new column which will be a foreign key. I've been able to add column and foreign key constraints using two separate ALTER TABLEcommands : ALTER TABLE one ADD two_id integer; ALTER TABLE one ADD FOREIGN KEY (two_id) REFERENCES two

Cannot add foreign key constraint

Juliver Galleto First, here is my main reference table mysql dump -- phpMyAdmin SQL Dump -- version 4.1.14 -- http://www.phpmyadmin.net -- -- Host: 127.0.0.1 -- Generation Time: Nov 17, 2015 at 03:15 AM -- Server version: 5.6.17 -- PHP Version: 5.5.12 SET SQL

SQL - Unable to add new foreign key constraint on SQL Fiddle

Tony Slepian I'm working on SQL Fiddle and so far the code looks like this: CREATE TABLE IF NOT EXISTS `orders` ( `id` int(6) unsigned NOT NULL, `date` DATE NOT NULL, `customerID` int(6) unsigned NOT NULL, `paymentAmmount` float(6) unsigned NOT NULL, PR

Unable to add new column with foreign key

Matheen Ulla I am new to laravel and I am getting the above error when adding a new column to an existing table: SQLSTATE[23000]: Integrity constraint violated: 1452 Cannot add or update child row: foreign key constraint failed ( schoolmanagement. #sql-314_2c,