Skip to main content

FOREIGN KEY constraint in sql server

FOREIGN KEY- This constraints responsible to uniquely identified a rows or records to another table.

Example-

Following table structure can clear your concept of foreign key constraint.

Product Table

CREATE TABLE PRODUCT
(
ID INT NOT NULL IDENTITY(1,1),
SKU VARCHAR(20) NOT NULL,
TITLE VARCHAR(200) NOT NULL,
PRICE MONEY NOT NULL,
DISCRIPTION VARCHAR(2000)  NULL,
DTCREATE DATETIME NULL
CONSTRAINT pk_ID PRIMARY KEY (ID)
)

Product Order Table

CREATE TABLE PRODUCT_IMAGE
(
ID INT NOT NULL IDENTITY(1,1),
PRODUCT_ID INT NOT NULL REFERENCES PRODUCT(ID),
IMGURL VARCHAR(200) NOT NULL ,
DTCREATE DATETIME
)

To create foreign key constraint to existing table use following query.

ALTER TABLE PRODUCT_IMAGE ADD FOREIGN KEY (PRODUCT_IDREFERENCES PRODUCT(ID)

Comments

Popular posts from this blog

Bulk Update in sql server 2008..

/*Bulk update query*/ create table Table1 ( ID int identity ( 1 , 1 ), vIsbn varchar ( 20 ), dcPrice decimal ( 18 , 2 ) ) create table Table2 ( ID int identity ( 1 , 1 ), vIsbn varchar ( 20 ), dcPrice decimal ( 18 , 2 ) ) /*Here create two tables, we update Table1's dcPrice column from Table 2 Using Bulk update*/     insert into Table1 ( vIsbn , dcPrice ) values ( '9087657654312' , 0.00 ) insert into Table1 ( vIsbn , dcPrice ) values ( '9989876765454' , 0.00 ) insert into Table2 ( vIsbn , dcPrice ) values ( '9087657654312' , 10.20 ) insert into Table2 ( vIsbn , dcPrice ) values ( '9989876765454' , 20.10 ) select * from Table1 /*here bulk update Query*/ UPDATE Tbl SET dcPrice = Tbl2 . dcPrice FROM Table1 Tbl JOIN Table2 AS Tbl2 ON Tbl . vIsbn = Tbl2 . vIsbn select * from Table1 drop table Table1 dr

TRY/CATCH and error handling in SQL server 2005/2008

SQL Server 2005 provide a strong set of tool to handle error or exception, in privious version it was not easy to handle exceptions. We use TRY/CATCH in SQL Server same as populer languages, We write saperate logic and exception handling code. Syntax: BEGIN TRY -- Your Code or query END TRY BEGIN CATCH -- Handling code END CATCH We write our logic code within TRy bolck and exception code in CATCh block, if nay error occur in logic code it jumps to CATCH  block, resume the execution and transaction is rollback. Functions which is use in CATCH 1- ERROR_NUMBER() Returns error no othe error message. 2- ERROR_SEVERITY() returns the error severity regardless of how many times it is run. 3- ERROR_STATE() Returns the state number of error messages that cause the catch block. 4- ERROR_PROCEDURE() Return the Procedure Name. 5- ERROR_LINE() Return error line number. 6- ERROR_MESSAGE() return error description. Note : If we are calling all above method out side from C

DEFAULT constraint in sql server

DEFAULT – This constraint provides a default value when specified non for this column. Example- Following create a table PRODUCT, column DTCREATE has default value GETDATE() it will store system’s date-time by default if value is not specified for this column. CREATE TABLE PRODUCT ( ID INT NOT NULL, SKU VARCHAR ( 20 ) NOT NULL, TITLE VARCHAR ( 200 ) NOT NULL, PRICE MONEY NOT NULL, DISCRIPTION VARCHAR ( 2000 )   NULL, DTCREATE DATETIME DEFAULT GETDATE () ) If PRODUCT table has already been created and you want apply default price in PRICE column use following query (for SQL server). ALTER TABLE PRODUCT ADD CONSTRAINT DF_SomePrice DEFAULT 20.00 FOR PRICE