0% found this document useful (0 votes)
18 views12 pages

SQL DAY-5

This document discusses different types of constraints that can be used to enforce data integrity in SQL tables. The five main types of constraints are primary key, unique, foreign key, check, and default constraints. It provides the syntax for defining each type of constraint using the CREATE TABLE and ALTER TABLE statements. Primary key constraints enforce uniqueness of rows and do not allow null values. Foreign key constraints require column values to match the primary key of another table. Check constraints restrict the type of data that can be entered into a column. Default constraints supply a default value if none is provided.

Uploaded by

RAA GHAV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views12 pages

SQL DAY-5

This document discusses different types of constraints that can be used to enforce data integrity in SQL tables. The five main types of constraints are primary key, unique, foreign key, check, and default constraints. It provides the syntax for defining each type of constraint using the CREATE TABLE and ALTER TABLE statements. Primary key constraints enforce uniqueness of rows and do not allow null values. Foreign key constraints require column values to match the primary key of another table. Check constraints restrict the type of data that can be entered into a column. Default constraints supply a default value if none is provided.

Uploaded by

RAA GHAV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

DAY-5

CHAPTER 6

DATA INTEGRITY
Constraints
• A constraint is a rule defined for a single table that limits the possible values user can
enter into the table or column.
• Constraints enforce data integrity on a column (column_level constraints) or on a
table (table_level constraints).
• Constraints are maintained separately from a table’s structure so that we can create,
change, and drop constraints without having to drop and recreate the table.

You can enforce the data integrity by using constraints


Types of Constraints
Constraints can be divided into five categories
1. PRIMARY KEY constraint
2. UNIQUE constraint
3. FOREIGN KEY constraint
4. CHECK constraint
5. DEFAULT constraint
Defining a constraint
Constraints are defined either using CREATE
TABLE or ALTER TABLE statements.
Using CREATE TABLE
Syntax:
CREATE TABLE table_name
(
Col_name datatype constraint
Constraint_name constraint_type, …….
)
PRIMARY KEY CONSTRAINT
Primary key constraints defined on a column or set of
columns uniquely the rows in a table. NULL values are
not allowed. No two rows can have the same value in the
constrained column.
Defining PRIMARY KEY using CREATE TABLE
• CREATE TABLE courses (courseid int constraint pk_cid
PRIMARY KEY, coursename char (10), Description char
(40), fee int)
Defining PRIMARY KEY after the table has been created:
ALTER TABLE
• ALTER TABLE employee ADD constraint pk_eid PRIMARY
KEY (empid)
Defining PRIMARY KEY constraint on multiple columns:
• CREATE TABLE product (pcode char (5), pname varchar (15),
pdesc varchar(40), price money, constraint pk_prod PRIMARY
KEY (pcode, pname))
UNIQUE CONSTRAINT
• It is used to enforce uniqueness on non-primary key column.
• UNIQUE constraint is similar to PRIMARY KEY except it allows
NULL values over one or more columns, but there is similar to
PRIMARY KEY except it allows NULL values over one or more
columns, but there can be only one row with a NULL value.
Syntax:
CONSTRAINT constraint_name UNIQUE
(col_name[, col_name….])
Defining UNIQUE constraint using CREATE
TABLE
• CREATE TABLE shop (shopid char (5),
shopname char (30) CONSTRAINT uk_shname
UNIQUE, city varchar(10))
Adding UNIQUE constraint using ALTER TABLE:
• ALTER TABLE books ADD constraint uk_bname
UNIQUE (title) Add UNIQUE constraint
UK_bname to title column of books table.
FOREIGN KEY CONSTRAINT
• The foreign key constraint defines a column or combination of columns whose
values match the primary key of a table.
• The FOREIGN KEY constraint is used in conjunction with the REFERENCES clause.
• FOREIGN KEY constraints must reference either the PRIMARY KEY or a UNIQUE
constraint of another table.
Syntax:
• [CONSTRAINT constraint_name FOREIGN key (col_name…..) REFRENCES
reftable_name (ref_col…..]
• CREATE TABLE stud_enroll (ento int CONSTRAINT pk_Eno PRIMARY KEY, sname
varchar(25), age int, address char(30), course char(10))
• CREATE TABLE stud_marks (entno int CONSTRAINT fk_eno FOREIGN KEY
REFERENCES stud_enroll (entno), sname char(20), Doj datetime, course char(10))
Using ALTER TABLE to define FOREIGN KEY
• ALTER TABLE stud_marks ADD CONSTRAINT fk_eno FOREIGN KEY (entono)
REFERENCES stud_enroll (entno)
CHECK CONSTRAINT
• Restricts the type of data that can be entered in a particular
column.
• Check constraints can contain user-defined search conditions
similar to the where clasuse of a SELECT statement.
• Check constraints can reference other columns in the same table.
• Specifying the WITH NO CHECK option prevents a check constraint
from validating existing data.
Syntax:
• CONSTRAINT constraint_name
CHECK (expression)
• CREATE TABLE Lib_member (memberno int, name varchar (20), age
int CONSTRAINT ck_age CHECK (age>=18), city char(15))
• ALTER TABLE Lib_member ADD CONSTRAINT ck_city CHECK (city IN
(‘delhi’, ‘chennai’)
Default constraint
• We can define DEFAULT constraint for a column to supply a value when a user does
not enter a value.
• A column can have only one DEFAULT constraint.
• DEFAULT constraint cannot be placed on IDENTITY columns.
• SYSTEM_supplied values like USER, GETDATE (), CURRENT_USER and other similar
values can be assigned as defaults.
Syntax:
• CONSTRAINT constraint_name DEFAULT (constraint_expression | NULL [for col_name]
• ALTER TABLE employee ADD CONSTRAINT df_sal DEFAULT 0 for SALARY.
• CREATE TABLE employee (ecode char (5), fname char (15), iname char (15), address
varchar (25), city varchar (10), country varchar (15) CONSTRAINT ck_cty DEFAULT
‘india’, pcode char (6), DOJ datetime CONSTRAINT ck_doj DEFAULT getdate ())
Disabling constraint checking:
• ALTER TABLE employee NO CHECK CONSTRAINT ck_city
Enabling constraint
• ALTER TABLE employee CHECK CONSTRAINT ck_cty
To Drop the constraint:
Syntax:
• ALTER TABLE tablename DROP CONSTRAINT constraintname
• ALTER TABLE shop DROP CONSTRAINT uk_shname
• ALTER TABLE employee DROP CONSTRAINT pk_eid
Creating an IDENTITY column
• We can create a column with the IDENTITY property using
CREATE TABLE, ALTER TABLE.
Syntax:
• IDENTITY [ (seed, increment) ]
Seed
• Is the value that is used for the very first row loaded into the
table.
CREATE TABLE students (studid int IDENTITY
(101, 5), name varchar (15))
Inserting values to student table:
Insert into students values (‘john’)
Insert into students values (‘kajol’)
IDENT_SEED (‘table’)
• SELECT IDENT_SEED (‘students’)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy