(CSE3083) Lab Practical Assignment 2
(CSE3083) Lab Practical Assignment 2
(CSE3083) Lab Practical Assignment 2
Null Value Concepts:-while creating tables if a row lacks a data value for
particular column that value is said to be null. Column of any data types may
contain null values unless the column was defined as not null when the table was
created.
Syntax:
Create table tablename
(columnname data type (size) not null ……)
Note: Not null constraint cannot be defined at table level.
Primary Key: primary key is one or more columns is a table used to uniquely
identify each row in the table. Primary key values must not be null and must be
unique across the column. A multicolumn primary key is called composite primary
key.
Syntax: primary key as a column constraint
Create table tablename
(columnname datatype (size) primary key,….)
Unique key concept:-A unique key is similar to a primary key except that the
purpose of a unique key is to ensure that information in the column for each record
is unique as with telephone or devices license numbers. A table may have many
unique keys.
Syntax: Unique as a column constraint.
Create table table name
(columnname datatype (size) unique);
Unique as table constraint:
Create table tablename
(columnname datatype(size),columnname datatype(size)…
unique (columnname));
Default value concept: At the time of column creation, a default value can be
assigned to it. When the user is loading a record with values and leaves this column
empty, the DBA will automatically load this column with the default value
specified. The data type of the default value should match the data type of the
column.
Syntax:
Create table tablename
(columnnamedatatype (size) default value,….);
Note: The default value constraint cannot be specified at table level.
Check Integrity Constraints: Use the check constraints when you need to enforce
integrity rules that can be evaluated based on a logical expression. Following are a
few examples of appropriate check constraints.
A check constraints on the column ‘name’ of the Employee table so that the
name is entered in upper case.
A check constraint on the column ‘Emp_no’ of the Employee table so that no
Emp_no value starts with ‘e’.
Syntax:
Create table tablename
(columnname datatype(size),……..
CONSTRAINT constraintname check(expression));
Modifying the Structure of Tables- Alter table command is used to changing the
structure of a table. Using the alter table clause you cannot perform the following
tasks:
(i) change the name of table
(ii) decrease the size of a column if table data exists and occupies larger size.
The following tasks you can perform through alter table command.
(i) Adding new columns:
Syntax:
ALTER TABLE tablename
ADD (newcolumnnamenewdatatype (size));
(ii) Modifying existing table
Syntax:
ALTER TABLE tablename
MODIFY (newcolumnnamenewdatatype (size));
(iii) Deleting a column
Syntax:
ALTER TABLE tablename
DROP COLUMN columnname;
1. Create the following tables and specify constraints at the time of creation.
Department
Column Data Size Constraint
Name Type
Deptno number 3 primary key
Dname varchar2 20 Unique
Location varchar2 20 not null, department are located in Delhi, Pune,
Agra
Employee
Column Data Size Constraint
Name Type
Empno varchar2 5 primary key, should start with ‘E’
Ename varchar2 20 Unique
Designation varchar2 20 not null
Salary number 10 default 25000, must lie between 15000 and
50000
DOB date not null
Dno number 3 foreign key (references department)
Candidate
Column Name Data type Size Constraints
Candidate_ID Number 6 Primary key of the table
Candidate_Name Varchar2 20 Not Null
Candidate_Email Varchar2 30 Unique, Must have ‘@’ followed by ‘.’ in
between the email
Candidate_Dept Number 2 Default ‘HR’
Manager_ID Varchar2 30 It can take only those values which are
present in Candidate_ID column
2. Create the schemas as specified above without specifying any constraints.
Customer Table
Column name Datatype Description Constraints
Unique id generated for each Primary Key, Should
CustomerId Varchar2(6)
customer start with ‘C’
CustomerName Varchar2(30) Name of the customer Not null
Date on which the customer
DateOfReg Date
registered
Decided at the time of
UserId Varchar2(15) It should be unique
registration
Decided at the time of
Password Varchar2(15) Not Null
registration
BankInfo Table
Column name Datatype Description Constraints
AccountNo Number(10) Account no of customer
Foreign key
Unique id provided to referring to Composite
each customer when customer Primary key
CustomerId Varchar2(6)
he/she is registered to table (ON
purchase items DELETE
CASCADE)
Billing Table
Item Table
Colum name Datatype Description Constraints
Unique Id provided for
ItemId Varchar2(6) each item. (eg STN001 Primary Key
for stationery items)
ItemName Varchar2(30) Name of the item Not Null
Should be greater than
Current availability of
QtyOnHand Number(3) ReOrderLevel (Table
item in the shop
level constraint)
Sell price of item per
UnitPrice Number(6,2) Greater than 0
unit
Class of Item is ‘A’ if
UnitPrice is less than
Depending on the
100, ‘B’ if UnitPrice is
UnitPrice, items
Class Char(1) less than 1000, ‘C’ if
belongs to various
UnitPrice is 1000 and
Classes. eg: A,B,C etc.
above (Table level
constraint)
Unit used to measure
UnitOfMeasurement Varchar2(12) the quantity ( eg
Kilogram, dozen etc)
Minimum Quantity
after which the supplier
ReOrderLevel Number(3) Greater than 0
must be ordered for
new stock
Minimum Quantity that
ReorderQty Number(3) can be ordered to the Greater than 0
supplier
Percentage discount on
Discount Number(2)
the item to the customer
Convert the following relational schema into tables with proper constraints
that are required.
Employee
FName Minit LName SSN BDate Address Sex Salary SuperSSN DepNo
Department
DName DepNo MgrSSN MgrSDate
Dept_Locations
DepNo DLocation
Project
Works_On
Dependent