0% found this document useful (0 votes)
24 views

DBMS Exp PDF

this is database management system experiment with some sql queries that were fired and outputs attached

Uploaded by

anushkaanilsingh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

DBMS Exp PDF

this is database management system experiment with some sql queries that were fired and outputs attached

Uploaded by

anushkaanilsingh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Experiment No.

: 5

Aim: Create a set of tables and apply constraints in SQL.

Learning Objective: Student should be able to apply Constraints for the specified System
Tool:- SQL Server
Theory:
SQL Constraints
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of
the data in the table. If there is any violation between the constraint and the data action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column, and table level
constraints apply to the whole table.
The following constraints are commonly used in SQL:
● NOT NULL - Ensures that a column cannot have a NULL value

● UNIQUE - Ensures that all values in a column are different


● PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a
table
● FOREIGN KEY - Uniquely identifies a row/record in another table
● CHECK - Ensures that all values in a column satisfies a specific condition
● DEFAULT - Sets a default value for a column when no value is specified
SQL NOT NULL Constraint

By default, a column can hold NULL values.

The NOT NULL constraint enforces a column to NOT accept NULL values.

This enforces a field to always contain a value, which means that you cannot insert a new record, or update a
record without adding a value to this field.

SQL NOT NULL on CREATE TABLE

The following SQL ensures that the "ID", "LastName", and "FirstName" columns will NOT accept NULL
values when the "Persons" table is created:

Example
CREATE TABLE Persons (
ID int NOT NULL,
LastNamevarchar(255) NOT NULL,
FirstNamevarchar(255) NOT NULL,
Age int
);
SQL NOT NULL on ALTER TABLE

To create a NOT NULL constraint on the "Age" column when the "Persons" table is already created, use the
following SQL:

ALTER TABLE Persons


alter column Age int NOT NULL;
SQL UNIQUE Constraint

The UNIQUE constraint ensures that all values in a column are different.

Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of
columns.

A PRIMARY KEY constraint automatically has a UNIQUE constraint.

However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per
table.

SQL UNIQUE Constraint on CREATE TABLE

The following SQL creates a UNIQUE constraint on the "ID" column when the "Persons" table is created:

CREATE TABLE Persons (


ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
SQL UNIQUE Constraint on ALTER TABLE
ALTER TABLE Persons
ADD CONSTRAINT UC_Person UNIQUE (ID,LastName)

DROP a UNIQUE Constraint


ALTER TABLE Persons
DROP CONSTRAINT UC_Person;
SQL PRIMARY KEY Constraint

The PRIMARY KEY constraint uniquely identifies each record in a table.

Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple
columns (fields).

SQL PRIMARY KEY on CREATE TABLE

The following SQL creates a PRIMARY KEY on the "ID" column when the "Persons" table is created:

CREATETABLE Persons (
ID int NOT NULL PRIMARYKEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);

To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple
columns, use the following SQL syntax:

CREATE TABLE Persons (


ID int NOTNULL,
LastName varchar (255) NOT NULL,
FirstName varchar (255),
Age int,
CONSTRAINT PK_Person PRIMARY KEY (ID,LastName)
);

SQL PRIMARY KEY on ALTER TABLE

To create a PRIMARY KEY constraint on the "ID" column when the table is already created, use the following
SQL:

ALTER TABLE Persons


ADD PRIMARY KEY (ID);
DROP a PRIMARY KEY Constraint

To drop a PRIMARY KEY constraint, use the following SQL:

ALTER TABLE Persons


DROP CONSTRAINT PK_Person;

SQL FOREIGN KEY Constraint

A FOREIGN KEY is a key used to link two tables together.

A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another
table.

The table containing the foreign key is called the child table, and the table containing the candidate key is called
the referenced or parent table.

SQL FOREIGN KEY on CREATE TABLE

The following SQL creates a FOREIGN KEY on the "PersonID" column when the "Orders" table is created:

CREATE TABLE Orders (


OrderID int NOT NULL PRIMARYKEY,
OrderNumber int NOT NULL,
PersonID int FOREIGN KEY REFERENCES Persons(PersonID)

SQL FOREIGN KEY on ALTER TABLE

To create a FOREIGN KEY constraint on the "PersonID" column when the "Orders" table is already created,
use the following SQL:

ALTER TABLE Orders


ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);
DROP a FOREIGN KEY Constraint

To drop a FOREIGN KEY constraint, use the following SQL:

ALTER TABLE Orders


DROP CONSTRAINT FK_PersonOrder;
SQL CHECK Constraint

The CHECK constraint is used to limit the value range that can be placed in a column.

If you define a CHECK constraint on a single column it allows only certain values for this column.

If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other
columns in the row
SQL CHECK on CREATE TABLE

The following SQL creates a CHECK constraint on the "Age" column when the "Persons" table is created. The
CHECK constraint ensures that you can not have any person below 18 years:

CREATETABLE Persons (
ID int NOT NULL,
LastName varchar (255) NOTNULL,
FirstName varchar(255),
Age int CHECK (Age>=18)
);
SQL CHECK on ALTER TABLE

To create a CHECK constraint on the "Age" column when the table is already created, use the following SQL:

ALTER TABLE Persons


ADD CHECK (Age>=18);
DROP a CHECK Constraint

To drop a CHECK constraint, use the following SQL:

ALTER TABLE Persons


DROP CONSTRAINT CHK_PersonAge;
SQL DEFAULT Constraint

The DEFAULT constraint is used to provide a default value for a column.

The default value will be added to all new records IF no other value is specified.

SQL DEFAULT on CREATE TABLE

The following SQL sets a DEFAULT value for the "City" column when the "Persons" table is created:

CREATETABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes'
);
SQL DEFAULT on ALTER TABLE

To create a DEFAULT constraint on the "City" column when the table is already created, use the following
SQL:

ALTER TABLE Persons


ADD CONSTRAINT df_City
DEFAULT 'Sandnes' FOR City;
DROP a DEFAULT Constraint

To drop a DEFAULT constraint, use the following SQL:

ALTER TABLE Persons


ALTER COLUMN City DROP DEFAULT;

Result and Discussion:


Learning Outcomes: Students should have the ability to
LO1: Define Syntax for various Constraints
LO2: Identify Constraint for given query
LO3: Apply Constraints for desired Output
Course Outcomes: Upon completion of the course students will be able to Solve and build basic SQL Queries
on given Data.
Conclusion:

Viva Questions:
1. List various Constraints in SQL
2. Explain syntax of Check Constraint while creation of table with example
3. Explain syntax of Check Constraint to existing table with example
4. Explain syntax of Default Constraint while creation of table with example
5. Explain syntax of Default Constraint to existing table with example
6. Explain syntax of Primary Key Constraint while creation of table with example
7. Explain syntax of Primary Key Constraint to existing table with example
8. Explain syntax of Foreign Key Constraint while creation of table with example
9. Explain syntax of Foreign Key Constraint to existing table with example
10. Explain syntax of Not Null Constraint while creation of table with example
11. Explain syntax of Not Null Constraint to existing table with example
12. Explain syntax of Unique Constraint while creation of table with example
13. Explain syntax of Unique Constraint to existing table with example

For Faculty Use


Correction Formative Timely completion Attendance /
Parameters Assessment of Practical [ 40%] Learning
[40%] Attitude [20%]

You might also like