Skip to main content

Posts

Constraints in sql server

Constraints   - Constraints are the rules enforced on data columns on table. This ensures the accuracy and reliability of the data in the database. Constraints define some conditions that restricts the column to remain true while inserting or updating or deleting data in column. Constraints can be defined in two ways   1) The constraints can be specified immediately after the column definition. This is called column-level definition.   2) The constraints can be specified after all the columns are defined. This is called table-level definition.   Types of Constraints in SQL-Server NOT NULL - This constraint is responsible for a column to confirm that a column cannot have a null value. DEFAULT – This constraint provides a default value when specified non for this column. UNIQUE - This constraint insure that each row for the column has different value. PRIMARY KEY - Primary key constraint is a combination of a NOT NULL constraint and a UNIQUE constra
Recent posts

CHECK constraint in sql server

CHECK- The CHECK responsible to enables a condition to check the value being entered into a record Example- In following example we create PRODUCT table it has PRICE column and its value should be greater than 20.00, so that you cannot have any product which price is less than or equal to 20.00 CREATE TABLE PRODUCT ( ID INT NOT NULL IDENTITY ( 1 , 1 ), SKU VARCHAR ( 20 ) NOT NULL, TITLE VARCHAR ( 200 ) NOT NULL, PRICE MONEY NOT NULL CHECK ( PRICE > 20.00 ), DISCRIPTION VARCHAR ( 2000 )   NULL, DTCREATE DATETIME NULL CONSTRAINT pk_ID PRIMARY KEY ( ID ) ) If table already exists then use following query to create check constraints. ALTER TABLE PRODUCT ADD CHECK ( PRICE > 20.00 )

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_ID )  REFERENCES PRODUCT ( ID )

PRIMARY KEY constraint in sql server

PRIMARY KEY- Primary key constraint is a combination of a NOT NULL constraint and a UNIQUE constraint. This constraint ensures that the specific column for a table have an unique identity. Example- Following we create a table PRODUCT in which column ID has primary key constraints. 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 ) ) If table already exits then use following query. ALTER TABLE PRODUCT  ADD CONSTRAINT pk_products_pid PRIMARY KEY ( ID )

UNIQUE KEY constraint in sql server

UNIQUE- This constraint insure that each row for the column has different value. Example- Following create a table PRODUCT, column SKU has unique key constraints. CREATE TABLE PRODUCT ( ID INT NOT NULL, SKU VARCHAR ( 20 ) NOT NULL UNIQUE , TITLE VARCHAR ( 200 ) NOT NULL, PRICE MONEY NOT NULL, DISCRIPTION VARCHAR ( 2000 )   NULL, DTCREATE DATETIME NULL ) If table has already been created and we want to apply unique constraints then use following query. ALTER TABLE PRODUCT ADD UNIQUE ( SKU ) You can also use following syntax, which supports naming the constraint in multiple columns as well ALTER TABLE PRODUCT ADD UNIQUE ( SKU,ID )

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

NOT NULL constraint in sql server

NOT NULL- This constraint is responsible for a column to confirm that a column cannot have a null value. Example- Following we create new table PRODUCT and add six columns where ID, SKU, TITLE, PRICE specify not to accept NULLs. 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 NULL ) If PRODUCT table already created, then to add a NOT NULL constraints to SKU column we use following query.   ALTER TABLE PRODUCT   ALTER COLUMN SKU  VARCHAR ( 20 ) NOT NULL