This document discusses different types of constraints in SQL including not null, unique key, primary key, foreign key, check key, and default key. It provides the syntax for creating constraints and examples of each constraint type. The examples show the creation of tables for customers, products, and sales with the appropriate constraints defined. Integrity constraints are used to prohibit illegal data and ensure referential integrity between tables.
3. Constraints are used to prohibit
illegal work in to a database
Constraints can be specified when a
table is created (with the CREATE
TABLE statement) or after the table is
created (with the ALTER TABLE
statement)
4. You can define constraints in two ways:
1. As part of the definition of an individual
column or attribute. This is called inline
specification Or called column level
definition
2. As part of the table definition.
This is called out-of-line specification
Or called table level definition
5. Syntax For Create Constraint
Sql> CREATE TABLE <table name>(
<attribute name> DATA TYPE (<size>) [constraint],
<attribute name> DATA TYPE (<size>) [constraint],
<attribute name> DATA TYPE (<size>) [constraint],
…………………….
……………………
);
NOTE: [ ]->Optional,( )->Required, < >->Required & depend
on programmer;
7. Not Null
constraint enforces a column to NOT accept
NULL values.
This means that you cannot insert a
new record, or update a record without
adding a value to this field.
Expression of Not Null we use short cut NN
8. 1. Not Null Example :-
Sql>CREATE TABLE customer
(
c_code CHAR (3) PRIMERY KEY,
c_name CHAR (20) NOT NULL,
c_city CHAR (10) DEFAULT “patna”,
gender CHAR (6) CHECK (gender=“Male”
OR gender=“Female”),
mob_no CHAR (14) UNIQUE,
);
11. 2. Unique Key
This constraint ensures that a column or
a group of columns in each row have a
distinct value. A column(s) can have a
null value but the values cannot be
duplicated.
13. 3. Primary Key
This constraint defines a column or combination of
columns which uniquely identifies each row in the
table.
Primary keys must contain unique values
A primary key column cannot
contain NULL values.
Each table should have a primary
key, and each table can have only
ONE primary key
14. 3. Primary Key Example
Sql>CREATE TABLE product
(
p_code CHAR (3) PRIMERY KEY,
p_name CHAR (20) NOT NULL,
p_rate NUMBER (5,2) CHECK
(c_rate>0),
qoh NUMBER(2),
);
15. Unique Key Vs Primary
Unique
Key
Unique Key
Key
1.Unique key use
many times in a table
2Unique key accept
only one null value
Primary
Key
1.Primary key use only
one times in a table
2 Primary key does not
accept null value
16. 4. Foreign Key or Referential
Integrity
Unique Key
This constraint identifies any column referencing the
PRIMARY KEY in another table. It establishes a relationship
between two columns in the same table or between different
tables. For a column to be defined as a Foreign Key, it
should be a defined as a Primary Key in the table which it is
referring. One or more columns can be defined as Foreign
key.
A FOREIGN KEY in one table points to a
PRIMARY KEY in another table
The FOREIGN KEY constraint is used
to prevent actions that would destroy
18. Structure of Customer
table
Unique Key
The “Costomer" table:
Column name Data type Size Constraint
C_code Char/text 3 PRIMARY KEY
C_name Char/text 20 NOT NULL
C_city Char/text 20 DEFAULT(Patna)
gender Char/text 6 CHECK(male,female)
Mob_no Char/text 14 UNIQUE,NOT NULL
19. Structure of Product
table
Unique Key
The “Product" table:
Column name Data type Size Constraint
p_code Char/text 3 PRIMARY KEY
p_name Char/text 20 NOT NULL
P_rate Number 5,2 CHECK(>0),NOT NULL
Qoh Number 5 CHECK(>=0),NOT NULL
20. Structure of Sales table
Unique Key
The “Sales" table:
Column
name
Data type Size Constraint
c_code Char/text 3 FOREIGN KEY,(RFFERENCES
CUSTOMER (c_code))
p_code Char/text 3 FOREIGN KEY,(RFFERENCES
PRODUCT (p_code))
qty Number 5 CHECK(>0),NOT NULL
21. 5. Check Key
This constraint defines a business rule on a
column. All the rows must satisfy this rule. The
constraint can be applied for a single column or a
group of columns.
23. 65.. DCehefacukl tK Keeyy
The DEFAULT constraint is used to
insert a default value into a column
The default value will be added to
all new records
if no other value is specified.
25. Some Other Keys
Composite Key :
A composite key is a combination of more
than one column to identify a unique row in a
table.
Candidate Key:
All keys in a table that become unique called
as candidate key. Ex-email id,mob no
Alternate Key:
Among of candidate keys if any single key or
combination of keys made as primary key
then rest candidate key called as alternate
key.