Lab 03
Lab 03
3
Simple SQL Queries and Constraints
To practice and implement data definition language commands and constraints.
Objective
s
Instructions
This is individual Lab work/task.
Complete this lab work within lab timing.
Discussion with peers is not allowed.
You can consult any book, notes & Internet.
Copy paste from Internet will give you negative marks.
Lab work is divided into small tasks, complete all tasks sequentially.
Show solution of each lab task to your Lab Instructor.
Paste your solution (i.e. code) in given space under each task.
Also make a zip/rar archive of all lab tasks and upload this file on LMS before leaving
lab.
In-Lab Exercises/Tasks
Ste Details
p
1 Check Constraint:
Check constraint can be defined to allow only a particular range of values. When the
manipulation violates this constraint, the record will be rejected. Check condition
cannot contain sub queries.
2 b) Entity Integrity
Maintains uniqueness in a record. An entity represents a table and each row of a table
represents an instance of that entity. To identify each row in a table uniquely we need to
use this constraint. There are 2 entity constraints:
Unique Key Constraint
It is used to ensure that information in the column for each record is unique, as with
telephone or drivers license numbers. It prevents the duplication of value with rows of a
specified column in a set of column. A column defined with the constraint can allow
null value.
If unique key constraint is defined in more than one column i.e., combination of column
cannot be specified. Maximum combination of columns that a composite unique key
can contain is 16.
Primary Key Constraint
A primary key avoids duplication of rows and does not allow null values. It can be
defined on one or more columns in a table and is used to uniquely identify each row in
a table. These values should never be changed and should never be null.A table should
have only one primary key. If a primary key constraint is assigned to more than one
column or combination of column is said to be composite primary key, which can
contain 16 columns.
3 c) Referential Integrity
It enforces relationship between tables. To establish parent-child relationship between 2
tables having a common column definition, we make use of this constraint. To
implement this, we should define the column in the parent table as primary key and
same column in the child table as foreign key referring to the corresponding parent
entry.
Foreign key
A column or combination of column included in the definition of referential integrity,
which would refer to a referenced key.
Referenced key
It is a unique or primary key upon which is defined on a column belonging to the parent
table.
SQL COMMANDS
CHECK CONSTRAINT
Example: Create table student (regno number (6), mark number (3) constraint b check
(mark >=0 and mark <=100));
Alter table student add constraint b2 check (length(regno<=4));
Entity Integrity
Unique key constraint
Example: Create table cust(custid number(6) constraint uni unique, name char(10));
Alter table cust add(constraint c unique(custid));
Tasks
Name Type
---------- ----------------------
EMPNO NUMBER(6)
ENAME VARCHAR2(20)
JOB VARCHAR2(10)
DEPTNO NUMBER(3)
SAL NUMBER(7,2)
Allow NULL for all columns except ename and job.
Answer:
1. Understand create table syntax.
2. Use the create table syntax to create the said tables.
3. Create primary key constraint for each table as you understand from logical table structure.
Table created.
Q2: Add a column experience to the emp table. Experience numeric null
allowed.
Answer:
1. Learn alter table syntax.
2. Define the new column and its data type.
3. Use the alter table syntax.
Q3: Modify the column width of the job field of emp table.
Answer:
1. Use the alter table syntax.
2. Modify the column width and its data type.
Answer:
1. Understand create table syntax.
2. Decide the name of the table.
3. Decide the name of each column and its data type.
4. Use the create table syntax to create the said tables.
5. Create primary key constraint for each table as understand from logical table structure.
Table created.
Answer:
1. Understand create table syntax.
2. Use the create table syntax to create the said tables.
3. Create foreign key constraint for each table as you understand from logical table structure.
Table created.
Q6: create the emp1 table with ename and empno, add constraints to check
the empno value while entering (i.e) empno > 100.
Answer:
1. Learn alter table syntax.
2. Define the new constraint [columns name type]
3. Use the alter table syntax for adding constraints.
Table created.
Answer:
1. Learn alter table syntax. Use the alter table syntax to drop the column.
Q8: Truncate the emp table and drop the dept table
Answer:
1. Learn drop, truncate table syntax.
OUTCOME
Thus the data definition language commands was performed and implemented successfully
Extra Tasks
Q1. Create a table name as Persons with columns name as “ID", "LastName", and "FirstName"
and these columns will NOT accept NULL values.
Q2. To create a NOT NULL constraint on the "Age" column when the "Persons" table is already
created
Q.4 Create a table Persons and Primary key constraint set on the "ID" column with
"LastName","FirstName”, “Age” and “Address”.
Q5. Create table Order and set FOREIGN KEY "PersonID" column in this table.