Unique, Primary Key, Foreign Key, Check, Not Null, Default Value
Unique, Primary Key, Foreign Key, Check, Not Null, Default Value
Practical No : 6
Aim : 1. Applying data constraints on tables
Unique, primary key, foreign key, check, not null, default value
Constraints are the rules enforced on data columns or on table. These are used to limit the type of the data
that can go into a table. This can ensure the accuracy and reliability of the data in the database.
Table wise: Table-level constraints refer to one or more columns in the table. Table-level
constraints specify the names of the columns to which they apply. Table-level CHECK constraints
can refer to 0 or more columns in the table.
Column wise: Column-level constraints refer to a single column in the table and do not specify a
column name (except check constraints). They refer to the column that they follow.
Constraints can be specified when the table is created with the CREATE TABLE statement, or after the
table is created with the ALTER TABLE statement.
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
1. NOT NULL
The following SQL ensures that the "ID", "LastName", and "FirstName" columns will NOT accept
NULL values when the "Persons" table is created:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
To create a NOT NULL constraint on the "Age" column when the "Persons" table is already
created, use the following SQL:
2. UNIQUE
The following SQL creates a UNIQUE constraint on the "ID" column when the "Persons" table is
created:
CREATE TABLE Persons (
ID int UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
ALTER TABLE Persons ADD UNIQUE (ID);
ALTER TABLE Persons ADD CONSTRAINT ucpid UNIQUE (ID);{find all: user_constraints}
ALTER TABLE Persons DROP CONSTRAINT ucpid;
3. PRIMARY KEY
The following SQL creates a PRIMARY KEY on the "ID" column when the "Persons" table is
created:
CREATE TABLE Persons (
ID int PRIMARY KEY,
LastName varchar(255),
FirstName varchar(255),
Age int
);
On multiple column:
CREATE TABLE Persons (
ID int,
LastName varchar(255),
FirstName varchar(255),
Age int,
CONSTRAINT PK_Person PRIMARY KEY (ID,LastName)
);
ALTER TABLE Persons ADD PRIMARY KEY (ID);
ALTER TABLE Persons ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName);
ALTER TABLE Persons DROP CONSTRAINT PK_Person;
4. FOREIGN KEY
The following SQL creates a FOREIGN KEY on the "PersonID" column when the "Orders" table is
created:
CREATE TABLE Orders (
OrderID int PRIMARY KEY,
OrderNumber int,
PersonID int FOREIGN KEY REFERENCES Persons(PersonID)
);
or
CREATE TABLE Orders (
OrderID int,
OrderNumber int,
PersonID int,
PRIMARY KEY (OrderID),
CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
ALTER TABLE Orders
ADD CONSTRAINT FK_PersonOrder
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);
ALTER TABLE Orders
DROP CONSTRAINT FK_PersonOrder;
5. CHECK
The following SQL creates a CHECK constraint on the "Age" column when the "Persons" table
is created. The CHECK constraint ensures that the age of a person must be 18, or older:
CREATE TABLE Persons (
ID int,
LastName varchar(255),
FirstName varchar(255),
Age int CHECK (Age>=18)
);
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255),
CONSTRAINT CHK_Person CHECK (Age>=18 AND City='Sandnes')
);
ALTER TABLE Persons
ADD CONSTRAINT CHK_PersonAge CHECK (Age>=18 AND City='Sandnes');
ALTER TABLE Persons DROP CONSTRAINT CHK_PersonAge;
6. DEFAULT
The following SQL sets a DEFAULT value for the "City" column when the "Persons" table is
created:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes'
);
CREATE TABLE Orders (
ID int NOT NULL,
OrderNumber int NOT NULL,
OrderDate date DEFAULT GETDATE()
);
ALTER TABLE Persons MODIFY City DEFAULT 'Sandnes';
ALTER TABLE Persons ALTER COLUMN City DROP DEFAULT;
7. INDEX : Indexes are used to retrieve data from the database more quickly than otherwise.
The users cannot see the indexes, they are just used to speed up searches/queries.
a) Create table client (used to store client information) having following attributes:
Column Name Data Type Size Default Attributes
clientno varchar2 6 Primary key at column
level/first letter must start
with ‘C’ (at column level)
name varchar2 20 Not Null
city varchar2 15
pincode number 8
state varchar2 15
baldue number 10, 5
1. c) Create table salesman (used to store salesman information working for the
company) having following attributes:
Column Name Data Type Size Default Attributes
salesmanno varchar2 6 Primary key at table
level /first letter must start
with ‘S’
salesmanname varchar2 20 Not Null
address1 varchar2 30 Not Null
address2 varchar2 30
city varchar2 20
pincode number 8
state varchar2 20
salamt number 8, 2 Not Null, Cannot be 0
tgttoget number 6, 2 Not Null, Cannot be 0
ytdsales number 6, 2 Not Null
remarks varchar2 60
1. e) Create table sales_order_details (used to store client’s orders with details of each
product ordered ) having following attributes:
Column Name Data Type Size Default Attributes
orderno varchar2 6 Foreign key references (at
table level) orderno of
sales_order table
productno varchar2 6 Foreign key (at table level)
references productno of
product table
qtyordered number 8
productrate number 10, 2
2. Alter table salesman, add constraint Not Null on remarks column and observe the
behavior. Mention your remarks.
3. Insert data in all the tables as per Practical – 2 and check if any constraint is getting
violated.
4. Delete data of salesman ‘S01’ from salesman table and observe the error. Rewrite the
query for alteration of table, so that on deletion of ‘S01’ from salesman, corresponding
values associated should also get deleted.
5. Delete data of order ‘O19001’ from sales_order table and observe the error. Rewrite the
query for alteration of table, so that if you remove ‘O19001’ from sales_order,
corresponding values associated should be set to NULL.
6. Drop primary key constraint on ‘orderno’ from sales_order table. Observe the error.
Write the drop query, so that associated constraints with ‘orderno’ also gets dropped.
Check whether the constraints have dropped from user_constraints table.