I am trying to add unique constraint as foreign key by this statement:
ALTER TABLE SOME_TABLE ADD(
CONSTRAINT FK_ID FOREIGN KEY (S_ID) REFERENCES OTHER_TABLE(O_ID) UNIQUE (S_ID)
);
I thought that this statement is correct, but all time I got "missing right parenthesis error". Probably I have wrong order of key words.
Could you give me advice how to create an unique constraint ?
I red this issue:
Add a unique constraint of a sql table as foreign key reference to an another sql table
but still I have problem with this.
First, you don't need parentheses. Second, this is two constraints and you might as well give both names:
ALTER TABLE SOME_TABLE
ADD CONSTRAINT FK_ID FOREIGN KEY (S_ID) REFERENCES OTHER_TABLE(O_ID);
ALTER TABLE SOME_TABLE
ADD CONSTRAINT UNQ_ST_S_ID UNIQUE (S_ID);
I am trying to add a foreign key to my table but i am getting this error,
ERROR at line 3: ORA-00904: "DEDUCID": invalid identifier
ALTER TABLE pr_cust
ADD CONSTRAINT deduc_fk
FOREIGN KEY (deducid)
REFERENCES pr_deduc;
I have this other table named pr_deduc that has a column named deducid, that is a char with one value as my primary key. I have it spelled corrected, unless i am missing something.
The deducid you mention has to be a column on pr_cust, and you are not referencing the column in the other table. The propper syntax is:
ALTER TABLE pr_cust
ADD CONSTRAINT deduc_fk
FOREIGN KEY (deducid)
REFERENCES pr_deduc(deducid);
ALTER TABLE pr_cust
ADD CONSTRAINT deduc_fk
FOREIGN KEY (deducid)
REFERENCES pr_deduc(deducid);
I am trying to add a new column that will be a foreign key. I have been able to add the column and the foreign key constraint using two separate ALTER TABLE commands:
ALTER TABLE one
ADD two_id integer;
ALTER TABLE one
ADD FOREIGN KEY (two_id) REFERENCES two(id);
Is there a way to do this with one ALTER TABLE command instead of two? I could not come up with anything that works.
As so often with SQL-related question, it depends on the DBMS. Some DBMS allow you to combine ALTER TABLE operations separated by commas. For example...
Informix syntax:
ALTER TABLE one
ADD two_id INTEGER,
ADD CONSTRAINT FOREIGN KEY(two_id) REFERENCES two(id);
The syntax for IBM DB2 LUW is similar, repeating the keyword ADD but (if I read the diagram correctly) not requiring a comma to separate the added items.
Microsoft SQL Server syntax:
ALTER TABLE one
ADD two_id INTEGER,
FOREIGN KEY(two_id) REFERENCES two(id);
Some others do not allow you to combine ALTER TABLE operations like that. Standard SQL only allows a single operation in the ALTER TABLE statement, so in Standard SQL, it has to be done in two steps.
In MS-SQLServer:
ALTER TABLE one
ADD two_id integer CONSTRAINT fk FOREIGN KEY (two_id) REFERENCES two(id)
In MS SQL SERVER:
With user defined foreign key name
ALTER TABLE tableName
ADD columnName dataType,
CONSTRAINT fkName FOREIGN KEY(fkColumnName)
REFERENCES pkTableName(pkTableColumnName);
Without user defined foreign key name
ALTER TABLE tableName
ADD columnName dataType,
FOREIGN KEY(fkColumnName) REFERENCES pkTableName(pkTableColumnName);
For SQL Server it should be something like
ALTER TABLE one
ADD two_id integer constraint fk foreign key references two(id)
In Oracle :
ALTER TABLE one ADD two_id INTEGER CONSTRAINT Fk_two_id REFERENCES two(id);
2020 Update
It's pretty old question but people are still returning to it I see. In case the above answers did not help you, make sure that you are using same data type for the new column as the id of the other table.
In my case, I was using Laravel and I use "unsigned integer" for all of my ids as there is no point of having negative id LOL.
So for that, the raw SQL query will change like this:
ALTER TABLE `table_name`
ADD `column_name` INTEGER UNSIGNED,
ADD CONSTRAINT constrain_name FOREIGN KEY(column_name) REFERENCES foreign_table_name(id);
I hope it helps
PostgreSQL DLL to add an FK column:
ALTER TABLE one
ADD two_id INTEGER REFERENCES two;
For DB2, the syntax is:
ALTER TABLE one ADD two_id INTEGER FOREIGN KEY (two_id) REFERENCES two (id);
ALTER TABLE TableName
ADD NewColumnName INTEGER,
FOREIGN KEY(NewColumnName) REFERENCES [ForeignKey_TableName](Foreign_Key_Column)
You can do it like below in SQL Server
ALTER TABLE one
ADD two_id int foreign key
REFERENCES two(id)
If you also need to add default values in case you already have some rows in the table then add DEFAULT val
ALTER TABLE one
ADD two_id int DEFAULT 123,
FOREIGN KEY(two_id) REFERENCES two(id);
Try this:
ALTER TABLE product
ADD FOREIGN KEY (product_ID) REFERENCES product(product_ID);
For SQL Server with constraint name
ALTER TABLE one
ADD two_id INT NOT NULL,
CONSTRAINT FK_name FOREIGN KEY (two_id) REFERENCES two(id);
I am Using Oracle 10g. I am Adding new column deptId to my UserList Table where I use deptId column as Foreign key which references other table Column Departments.DepartmentId
Is there Difference between adding foreign key as constraint and First Query
Query1
ALTER TABLE UserList
ADD FOREIGN KEY (DeptId)
REFERENCES Departments(DepartmentId)
Query2
ALTER TABLE UserList
ADD CONSTRAINT fk_DeptId FOREIGN KEY (DeptId)
REFERENCES Departments(DepartmentId)
There is no difference except in your use of the optional "CONSTRAINT" and constraint name clause.
There are two kinds of constraint definition: inline and out of line. The former operates on a column as part of the column definition, and hence does not need to name the DeptID column. The latter is part of the table definition and therefore does.
Both of your examples are out of line constraints, but you have not named the constraint in the former case, which is a bad practice:
http://docs.oracle.com/cd/E18283_01/server.112/e17118/clauses002.htm#g1053592
The second syntax allows you to name your constraint. The first doesn't.
ALTER TABLE [dbo].[UserList] WITH NOCHECK ADD CONSTRAINT [fk_DeptId] FOREIGN KEY([DeptId])
REFERENCES [dbo].[Departments] ([DepartmentId])
No, there is no difference in both of your queries.But you to name the foreign key constraints names . You can use the above query.
I'm trying to add the NOT_NULL constraint to a column in an SQL h2 database, using
ALTER TABLE CHARACTERS ADD CONSTRAINT nn_PID NOT_NULL (PLAYER_ID);
This follows the pattern I found here:
ALTER TABLE Persons ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)
Except I change the constraint, table and column names. But I get this error:
Syntax error in SQL statement "ALTER TABLE CHARACTERS ADD CONSTRAINT NN_PID NOT_NULL[*] (PLAYER_ID) "; expected "., COMMENT, PRIMARY, INDEX, KEY, CHECK, UNIQUE, FOREIGN"; SQL statement:
ALTER TABLE CHARACTERS ADD CONSTRAINT nn_PID NOT_NULL (PLAYER_ID) [42001-168] 42001/42001 (Help)
How can I add the NOT_NULL constraint?
From H2 SQL Grammar:
ALTER TABLE TEST ALTER COLUMN NAME SET NOT NULL;
So we can use:
ALTER TABLE CHARACTERS ALTER PLAYER_ID SET NOT NULL;