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

Lab 5 DBS - 082807

These 5 files are the lab manual of the labs performed by me

Uploaded by

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

Lab 5 DBS - 082807

These 5 files are the lab manual of the labs performed by me

Uploaded by

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

Contents:

1. Keys in a Database......................................................................................................................................................3
1.1. KEYS....................................................................................................................................................................3
1.2. TYPES of KEYS..................................................................................................................................................3
1.2.1. Candidate Keys................................................................................................................................................3
1.2.2. Super Keys.......................................................................................................................................................3
1.2.3. Primary Keys...................................................................................................................................................3
1.2.4. Alternate Keys.................................................................................................................................................3
1.2.5. Foreign Keys....................................................................................................................................................3
2. Constraints................................................................................................................................................................... 3
2.1. Definition and Explanation.................................................................................................................................3
2.2. Types of Constraints............................................................................................................................................3
2.2.1. NOT NULL......................................................................................................................................................4
2.2.2. UNIQUE...........................................................................................................................................................4
2.2.3. CHECK............................................................................................................................................................4
2.2.4. DEFAULT........................................................................................................................................................4
2.2.5. INDEX..............................................................................................................................................................5
2.3. Dropping the Constraints...................................................................................................................................5
3. Working in DBS using KEYS and CONSTRAINTS................................................................................................5
3.1. DDL Statements:.................................................................................................................................................5
3.2. DDL Statements:.................................................................................................................................................6
3.2.1. Creating and Using Database “umer_abid_lab_task_06”........................................................................6
3.2.2. Creating Table PUBLISHERS...................................................................................................................6
3.2.3. Altering Table PUBLISHERS....................................................................................................................6
3.2.4. Creating New Table Books..........................................................................................................................6
3.2.5. Altering New Table Books..........................................................................................................................7

CS – 2102 Database Systems 1|Page


1. Keys in a Database
1.1. KEYS
A key is an attribute or set of attributes that uniquely identifies a tuple in a relation. The keys are defined in
relations to access the stored data quickly and efficiently. They are also used to create relationship between
different relations.

1.2. TYPES of KEYS


There are following keys in a database:
 Candidate Keys
 Super Keys
 Primary Keys
 Alternate Keys
 Foreign Keys

1.2.1. Candidate Keys


` A relation may contain many attributes or set of attributes that can be used as primary key. The attribute
or set of attributes that can be used as primary key is called a candidate key.

1.2.2. Super Keys


Super key is an attribute set that can uniquely identify a tuple. A super key is a superset of a candidate
key.

1.2.3. Primary Keys


An attribute or set of attributes that uniquely identifies a row or record in a relation is known as a
primary key.

1.2.4. Alternate Keys


The candidate keys that are not selected as a primary key are called alternate keys.

1.2.5. Foreign Keys


A foreign key is an attribute or set of attributes in a relation whose values match a primary key in
another relation.

2. Constraints
2.1. Definition and Explanation
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. 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.

2.2. Types of Constraints


Constraints used in database are as follows:
 NOT NULL

CS – 2102 Database Systems 2|Page


 UNIQUE
 CHECK
 DEFAULT
 INDEX

2.1.1. NOT NULL


This constraint is used to specify that an attribute will not accept null values. This constraint can be
specified only as column level constraint and not as table-level constraint.
Syntax:
1. <Attribute> <datatype> NOT NULL
2. ALTER TABLE TABLE_NAME ALTER COLUMN Column_Name Datatype
NOT NULL;
Example:
1. Page_Count Numeric (4) NOT NULL
2. ALTER TABLE PUBLISHERS ALTER COLUMN P_Name VARCHAR (50)
NOT NULL;

2.1.2. UNIQUE
This constraint ensures that the set of attributes have unique values. No two tuples can have same
value in the specified attributes.
Syntax:
1. <Attribute> <datatype> UNIQUE
2. ALTER TABLE Table_Name ADD CONSTRAINT cons_name UNIQUE(Attribute);
Example:
1. P_Name varchar (50) UNIQUE
2. ALTER TABLE BOOKS ADD CONSTRAINT UQ_Book_Title UNIQUE(Book_Title);

2.1.3. CHECK
This constraint ascertains that the value inserted in an attribute must satisfy a given expression.
Syntax:
1. <Attribute> <datatype> CHECK (<expression>)
2. ALTER TABLE Table_Name ADD CONSTRAINT cons_name CHECK
(Expression);
Example:
1. Price Numeric (4) CHECK (Price > 20)
2. ALTER TABLE PUBLISHERS ADD CONSTRAINT chk_PID_greater_than_0
CHECK (P_ID>0);

2.1.4. DEFAULT
This constraint consists of a set of default values for a column when no value is specified.

CS – 2102 Database Systems 3|Page


Syntax:
1. <Attribute> <datatype> DEFAULT ‘value’
2. ALTER TABLE Table_Name ADD CONSTRAINT cons_name DEFAULT
'def_value' FOR Attribute;
Example:
1. City varchar (25) DEFAULT ‘New York’
2. ALTER TABLE BOOKS ADD CONSTRAINT def_Category DEFAULT
'Novel' FOR Category;

2.1.5. INDEX
This constraint is used to create and retrieve data from the database very quickly.

2.3. Dropping the Constraints


1. ALTER TABLE Table_Name DROP CONSTRAINT Cons_1;
2. ALTER TABLE Table_Name ALTER COLUMN Column_Name DROP NOT
NULL;
3. ALTER TABLE Table_Name DROP CONSTRAINT cons_name;

3. Working in DBS using KEYS and CONSTRAINTS


3.1. DDL Statements:
1. CREATE DATABASE umer_abid_lab_task_06;
2. USE umer_abid_lab_task_06;
3. CREATE TABLE PUBLISHERS
(
P_ID VARCHAR (4) PRIMARY KEY,
P_Name VARCHAR (50),
Address VARCHAR (50),
State VARCHAR (15),
Phone VARCHAR (20),
Email_ID VARCHAR (30)
)
4. ALTER TABLE PUBLISHERS ADD CONSTRAINT chk_PID_greater_than_0 CHECK
(P_ID>0);
5. ALTER TABLE PUBLISHERS ALTER COLUMN P_Name VARCHAR (50) NOT NULL;
6. ALTER TABLE PUBLISHERS ADD CONSTRAINT UQ_Phone UNIQUE (Phone);
7. CREATE TABLE BOOKS
(
ISBN VARCHAR (15) PRIMARY KEY,
Book_Title VARCHAR (50) NOT NULL,
Category VARCHAR (20),
Price NUMERIC (6, 2),
Copyright_Date NUMERIC (4),
Year NUMERIC (4),
Page_Count NUMERIC (4),
P_ID VARCHAR (4) NOT NULL FOREIGN KEY REFERENCES PUBLISHERS
(P_ID)
CS – 2102 Database Systems 4|Page
)
8. ALTER TABLE BOOKS ADD CONSTRAINT UQ_Book_Title UNIQUE(Book_Title);
9. ALTER TABLE BOOKS ADD CONSTRAINT chk_book_price_greater_than_20 CHECK
(Price>20);
10. ALTER TABLE BOOKS ADD CONSTRAINT def_Category DEFAULT 'Novel' FOR
Category;
11. ALTER TABLE BOOKS DROP CONSTRAINT def_Category;

3.2. Working in Database:


3.2.1. Creating and Using Database “umer_abid_lab_task_06”
Execution:

3.2.2. Creating Table PUBLISHERS


Execution:

3.2.3. Altering Table PUBLISHERS


Execution:

CS – 2102 Database Systems 5|Page


3.2.4. Creating New Table Books
Execution:

3.2.5. Altering New Table Books


Execution:

CS – 2102 Database Systems 6|Page

You might also like