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

Lab 03

This document describes a lab experiment on implementing SQL queries and constraints. The objectives are to practice data definition language commands and constraints. The document provides instructions for completing the lab individually and sequentially. It lists the required database facilities. The procedure describes check, unique key, primary key, foreign key, and referential integrity constraints. It provides examples of creating tables with various constraints and modifying tables. Tasks include creating tables, adding and modifying columns, setting constraints, truncating and dropping tables. Questions at the end define DDL and list SQL command categories and integrity constraint types.

Uploaded by

Ishaq UDİN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Lab 03

This document describes a lab experiment on implementing SQL queries and constraints. The objectives are to practice data definition language commands and constraints. The document provides instructions for completing the lab individually and sequentially. It lists the required database facilities. The procedure describes check, unique key, primary key, foreign key, and referential integrity constraints. It provides examples of creating tables with various constraints and modifying tables. Tasks include creating tables, adding and modifying columns, setting constraints, truncating and dropping tables. Questions at the end define DDL and list SQL command categories and integrity constraint types.

Uploaded by

Ishaq UDİN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

LAB NO.

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

FACILITIES REQUIRED AND PROCEDURE

Facilities required to do the experiment

S.No Facilities Required Quantity


.
1 System 1
2 Operating System Windows 10
3 Front End Java
4 Back End Oracle 11g, MySQL
Procedure for doing the experiment

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));

Primary Key Constraint


Example: Create table stud(regno number(6) constraint primary key, name char(20));

Tasks

Q1. Create a table called EMP with the following structure.

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.

SQL> create table emp (


empno number(6),
ename varchar2(20) not null,
job varchar2(10) not null,
deptno number(3),
sal number(7,2));

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.

SQL> alter table emp add (experience number(2));


Table altered.

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.

SQL> alter table emp modify (job varchar2(12));


Table altered.
SQL> alter table emp modify (job varchar2(13));
Table altered.
Q4: Create department table with the following structure.
Name Type
------------ ---------------------
DEPTNO NUMBER(2)
DNAME VARCHAR2(10)
LOC VARCHAR2(10)
Deptno as the primarykey

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.

SQL>  create table department (


deptno number(2),
dname varchar2(10),
loc varchar2(10),
constraint dept_deptno_pk primary key (deptno));

Table created.

Q5. Create a table called staff with the following structure.


Name Type
---------- ----------------------
EMPNO NUMBER(6)
ENAME VARCHAR2(20)
JOB VARCHAR2(10)
DEPTNO NUMBER(3)
SAL NUMBER(7)
Deptno as foreign key

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.

SQL> create table staff (


empno number(6),
ename varchar2(20),
job varchar2(10),
sal number (7,2)
deptno number(3),
constraint staff_deptno_fk foreign key (deptno) references dept(deptno);

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.

SQL> create table emp1(ename varchar2(10), empno number(6) constraint ch


check(empno>100));

Table created.

Q7: Drop a column experience to the emp table.

Answer:
1. Learn alter table syntax. Use the alter table syntax to drop the column.

SQL> alter table emp drop column experience;


Table altered.

Q8: Truncate the emp table and drop the dept table

Answer:
1. Learn drop, truncate table syntax.

SQL>truncate table emp;


Table truncated.
SQL>drop table dept;
Table dropped.

OUTCOME
Thus the data definition language commands was performed and implemented successfully

QUESTIONS AND ANSWERS


Define the terms 
DDL:
Data base schema is specified by a set of definitions expressed by a special language called a
data definition language.

What are the categories of SQL command?


SQL commands are divided in to the following categories:
Data Delimitation language
Data manipulation language
Data control language
Transaction Control Language

What is integrity constraint?


An integrity constraint is a mechanism used by oracle to prevent invalid data entry into the table.
It has enforcing the rules for the columns in a table.
List the types of constraint.
Domain Integrity
Entity Integrity
Referential Integrity
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.
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.

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.

Paste code and screenshot here 

Q2. To create a NOT NULL constraint on the "Age" column when the "Persons" table is already
created

Paste code and screenshot here 


Q3. Create a table Persons and UNIQUE constraint set on the "ID" column with "LastName",
and "FirstName”.

Paste code and screenshot here 

Q.4 Create a table Persons and Primary key constraint set on the "ID" column with
"LastName","FirstName”, “Age” and “Address”.

Paste code and screenshot here 

Q5. Create table Order and set FOREIGN KEY "PersonID" column in this table.

Paste code and screenshot here 


Q6. Create a SQL statement in which set a CHECK constraint on the "Age" column when the
"Persons" table is created. The CHECK constraint ensures that you cannot have any person
below 18 years

Paste code and screenshot here 

You might also like