How to add unique constraint as foreign key ? - sql

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);

Related

PostgreSQL: Add constraint with mulitple conditions

I'm having difficulties adding a constraint with two constraint conditions to an already existing table. I'm working with the two relations below, and I'm trying to add the constraint to the "books" table.
The relations are:
books((book_id), title, author_id, subject_id)
subjects((subject_id), subject, location)
Where the keys within the parantheses are primary keys, and the italics are foreign keys.
The first criteria/condition is that the subject_id is NOT NULL, and that the subject_id when inserting a new book-tuple into books, already has to exist as a primary key in the subjects relation.
ALTER TABLE books ADD CONSTRAINT hasSubject
CHECK(subject_id IS NOT NULL AND subject_id REFERENCES subjects(subject_id))
I keep getting the error message: "syntax error at or near 'REFERENCES'"
Any ideas? Thank you in advance!
REFERENCES is a separate type of constraint. So, you need two constraints:
ALTER TABLE books ADD CONSTRAINT chk_books_subject_id
CHECK (subject_id IS NOT NULL);
ALTER TABLE books ADD CONSTRAINT fk_books_subject_id
FOREIGN KEY (subject_id) REFERENCES subjects(subject_id);
I put these in two ALTER TABLE statements, to emphasize that they are different. They can be combined in one statement.
EDIT:
In one statement:
ALTER TABLE books
ADD CONSTRAINT chk_books_subject_id CHECK (subject_id IS NOT NULL),
ADD CONSTRAINT fk_books_subject_id FOREIGN KEY (subject_id) REFERENCES subjects(subject_id);

How to add foreign key to an existing column in SQL Server 2012

I am trying to add foreign key to my existing column using below query
ALTER TABLE Sub_Category_Master
ADD FOREIGN KEY (Category_ID) REFERENCES Category_Master(Category_ID)
but I'm getting an error
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__Sub_Categ__Categ__5812160E". The conflict occurred in database "shaadikarbefikar_new", table "shaadikarbefikar.Category_Master", column 'Category_ID'.
Well, the error clearly tells you that Category_ID in your Sub_Category_Master table contains some values that are not present in Category_Master (column Category_ID). But that's exactly the point of having a foreign key constraint - making sure your child table (Sub_Category_Master) only uses defined values from its parent table.
Therefore, you must fix those "voodoo" values first, before you're able to establish this foreign key relationship. I would also strongly recommend to explicitly name that constraint yourself, to avoid those system-generated, but not really very useful constraint names like FK__Sub_Categ__Categ__5812160E:
ALTER TABLE Sub_Category_Master
ADD CONSTRAINT FK_SubCategoryMaster_CategoryMaster
FOREIGN KEY (Category_ID) REFERENCES Category_Master(Category_ID)
ALTER TABLE Sub_Category_Master
ADD CONSTRAINT FKSub_Category_Master_Category_ID FOREIGN KEY (Category_ID)
REFERENCES Category_Master(Category_ID);
CREATE TABLE Orders
(
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
CONSTRAINT FK_PersonOrder
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

Oracle SQL-ALTER TABLE Error

I've been looking over the following SQL code for awhile and just can't seem to find the problem. I'm relatively new to SQL, so I'm sure it's just something I'm overlooking. The error message I get is: ORA-01735: Invalid ALTER TABLE option.
Code:
ALTER TABLE PATIENT
(
ADD CONSTRAINT PProfileForeignKey
FOREIGN KEY (pProfileID) REFERENCES PATIENT_PROFILE(Profile_ID),
ADD CONSTRAINT InsForeignKey
FOREIGN KEY (pInsID) REFERENCES INSURANCE(Insurance_ID)
ON DELETE SET NULL
);
I have triple checked to make sure the foreign key column names and the referenced column names are correct.
seems The parentheses are in wrong place
ALTER TABLE PATIENT
ADD (CONSTRAINT PProfileForeignKey
FOREIGN KEY (pProfileID) REFERENCES PATIENT_PROFILE(Profile_ID),
CONSTRAINT InsForeignKey
FOREIGN KEY (pInsID) REFERENCES INSURANCE(Insurance_ID)
ON DELETE SET NULL);

Add Foreign Key to an existing column?

Hello i am beginner in SQL and i have one question:
How to add new Column_B(int) which is foreign key to an existing Column_A(id) in same Table_A?
I tried this but i got Error Code: 1215. Cannot add foreign key constraint
ALTER TABLE Table_A ADD COLUMN Column_B int;
ALTER TABLE Table_A
ADD fk_Table_A FOREIGN KEY (Column_B) REFERENCES Table_A (Column_A);
alter table Table_A
ADD constraint fk_Table_A FOREIGN KEY (Column_B) REFERENCES Table_A (Column_A);
It is possible that your references does not match. It can only add a foreign key when all the rows satisfy the foreign key condition, which is in your case that every value of Column_B is in table_A.Column_A.

What is the Difference between adding Column as Foreign Key and as a Constraint

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.