Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Database Management System Lab (CS 29006)

KALINGA INSTITUTE OF INDUSTRIAL


TECHNOLOGY

School of Computer Engineering

Strictly for internal circulation (within KIIT) and reference only. Not for outside circulation without permission

1 Credit Course Committee


Lab Contents
2

Sr # Major and Detailed Coverage Area Lab#


1 Integrity Constraints 2
DDL
DML

School of Computer Engineering


Integrity Constraint
3

Integrity constraints ensure accuracy and consistency of data in a relational


database and guard against the accidental damage to the database. E.g. an
account balance cannot be null or no two accounts cannot have the same
account number.
 Primary Key Constraint - Primary key is the term that identifies one or
more columns in a table that make a row of data unique. Although the
primary key typically consists of one column in a table, more than one
column can comprise the primary key. For example, either the employee’s
Social Security number or an assigned employee identification number is
the primary key for an employee table. The objective is for every record to
have a unique primary key or value for the employee’s identification
number. Because there is probably no need to have more than one record
for each employee in an employee table, the employee identification
number makes a logical primary key. The primary key is assigned at table
creation. Example: CREATE TABLE EMPLOYEE (EMP_ID CHAR(9) NOT
NULL PRIMARY KEY, ….
School of Computer Engineering
Integrity Constraint cont…
4

 Primary Key Constraint cont… – One can define a primary key that
consists of more than one column by either of the following methods,
which demonstrate creating a primary key in an Oracle table:
CREATE TABLE PRODUCT
(PROD_ID VARCHAR2(10) NOT NULL,
VEND_ID VARCHAR2(10) NOT NULL,
PRODUCT VARCHAR2(30) NOT NULL,
COST NUMBER(8,2) NOT NULL,
PRIMARY KEY (PROD_ID, VEND_ID));
 Unique Key Constraint – A unique column constraint in a table is similar
to a primary key in that the value in that column for every row of data in
the table must have a unique value. Although a primary key constraint is
placed on one column, you can place a unique constraint on another
column even though it is not actually for use as the primary key.

School of Computer Engineering


Integrity Constraint cont…
5

 Unique Key Constraint cont… – Example:


CREATE TABLE EMPLOYEE
(EMP_ID CHAR(9) NOT NULL PRIMARY KEY,
..... Represents continuation
EMP_PHONE INTEGER(10) NULL UNIQUE,
EMP_PAGER INTEGER(10) NULL);
 Foreign Key Constraint – A foreign key is a column in a child table that references a
primary key in the parent table. A foreign key constraint is the main mechanism that
enforces referential integrity between tables in a relational database. A column
defined as a foreign key references a column defined as a primary key in another
table. Example:
CREATE TABLE EMPLOYEE_PAY
(EMP_ID CHAR(9) NOT NULL,
.... Represents continuation
CONSTRAINT EMP_ID_FK FOREIGN KEY (EMP_ID) REFERENCES EMPLOYEE
(EMP_ID));
School of Computer Engineering
Integrity Constraint cont…
6

 Foreign Key Constraint example-

Class Work
Recreate the STUDENT table with appropriate integrity constraint.
School of Computer Engineering
Integrity Constraint cont…
7

 NOT NULL Constraint - NOT NULL is a constraint that can place on a table’s
column. This constraint disallows the entrance of NULL values into a column; in
other words, data is required in a NOT NULL column for each row of data in the
table. NULL is generally the default for a column if NOT NULL is not specified,
allowing NULL values in a column.
 Check Constraint - check (CHK) constraints can be utilized to check the validity of
data entered into particular table columns. Check constraints provide back-end
database edits, although edits are commonly found in the front-end application as
well. General edits restrict values that can be entered into columns or objects,
whether within the database or on a front-end application. The check constraint is a
way of providing another protective layer for the data. Example:
CREATE TABLE EMPLOYEE_PAY
(EMP_ID CHAR(9) NOT NULL,
PAY_RATE NUMBER(4,2) NOT NULL,
... Represents continuation
CONSTRAINT PAY_RATE_CHK CHECK ( PAY_RATE > 12.50 ));

School of Computer Engineering


Integrity Constraint cont…
8

 Default Constraint - Provides a default value for a column when none is specified.
E.g.
CREATE TABLE PERSON
(
PERSON_ID INT NOT NULL,
LAST_NAME VARCHAR2(255) NOT NULL,
FIRST_NAME VARCHAR2(255),
ADDRESS VARCHAR(255),
CITY VARCHAR(255) DEFAULT ‘Bhubaneswar‘,
INJECTED_DATE DATE DEFAULT SYSDATE
)
 To Drop default Constraint - To drop a DEFAULT constraint, the SQL syntax is :
ALTER TABLE TABLE_NAME ALTER COLUMN COLUMN_NAME DROP DEFAULT;
E.g. ALTER TABLE PERSON ALTER COLUMN CITY DROP DEFAULT;

School of Computer Engineering


Class Work
9

Using appropriate integrity constraints, design suitable tables to store data for
the following system:
 Timetable System involving -
Il l u
Student/Section/Course/Semester/Class Room/Faculty s tr
ati
ve
COURSE CLASS_ROOM SEMESTER TIME_TABLE SECTION COURSE_SEMESTER_REL
COURSE_ID CLASS_ROOM_NO SEMESTER_ID TT_ID SECTION_ID COURSE_SEMESTER_REL_ID
NAME NAME NAME DAY NAME COURSE_ID
CREDIT SITTING_CAPACITY TYPE TIME_FROM ADMISSION_YEAR SEMESTER_ID
LAB_EXIST PROJECTOR_AVAILABLE TIME_TO SECTION_CAPACITY
DESCRIPTION

STUDENT FACULTY TIME_TABLE_REL


ROLL_NO FACULTY_ID TT_ID
SECTION_ID EMP_NO FACULTY_ID
NAME NAME COURSE_ID
SEMESTER_ID DOB CLASS_ROOM_NO
DOJ SECTION_ID

Further Action
 Identify data type for each attribute covering all tables
 Define the relationship among the tables
 Expand the attributes (if any) for each table
School of Computer Engineering
DDL - Table Creation from existing Table
10

You can create a copy of an existing table using a combination of the


CREATE TABLE statement and the SELECT statement. The new table has
the same column definitions. You can select any or all columns. New
columns that you create via functions or a combination of columns
automatically assume the size necessary to hold the data. The basic
syntax for creating a table from another table is as follows:
CREATE TABLE NEW_TABLE_NAME AS
SELECT [ * | COLUMN1, COLUMN2 ] Represents either * or individual columns

FROM TABLE_NAME
[ WHERE ] WHERE statement is optional

Example
 CREATE TABLE PRODUCT_TMP AS SELECT * FROM PRODUCT;
 CREATE TABLE PRODUCT_TMP AS SELECT * FROM PRODUCT WHERE COST > 30;

School of Computer Engineering


DDL – Altering a Table
11

You can modify a table after the table has been created by using the ALTER
TABLE command. You can add column(s), drop column(s), change column
definitions, add and drop constraints.
Example
 ALTER TABLE EMPLOYEE MODIFY EMP_ID VARCHAR(10);
 ALTER TABLE EMPLOYEE ADD EMP_SEX VARCHAR(1);
 ALTER TABLE CUSTOMER DROP COLUMN CUSTOMER_NAME;
 ALTER TABLE CUSTOMER ADD (CUSTOMER_NAME VARCHAR2(45), CITY VARCHAR2(40));
 ALTER TABLE CUSTOMER MODIFY (CUSTOMER_NAME VARCHAR2(100) NOT NULL, CITY
VARCHAR2(75));
 ALTER TABLE CUSTOMER RENAME COLUMN CUSTOMER_NAME TO CNAME;
 ALTER TABLE CUSTOMERS RENAME TO CONTACTS;
Class Work
 Alter the Product table with following attributes:
ID, NAME, DESC, STATUS, CREATED_DATE, CREATED_BY, LAST_MODIFIED_DATE,
LAST_MODIFIED_BY, COST, INJECTED_DATE
School of Computer Engineering
DML – Data Manipulation
12

DML is the part of SQL that enables a database user to actually propagate
changes among data in a relational database. With DML, the user can populate
tables with new data, update existing data in tables, and delete data from
tables. Simple database queries can also be performed within a DML command.
Three basic DML commands in SQL are INSERT, UPDATE AND DELETE
Inserting Data into Limited Columns of a Table
There is an way to insert data into specified columns and the syntax is:
INSERT INTO TABLE_NAME (‘COLUMN1’, ‘COLUMN2’) VALUES (‘VALUE1’,
‘VALUE2’);
Example - suppose we want to insert all values for an employee except a pager
number. In this case, specify a column list as well as a VALUES list in INSERT
statement.
INSERT INTO EMPLOYEE (EMP_ID, LAST_NAME, FIRST_NAME, MIDDLE_NAME,
ADDRESS, CITY, STATE, ZIP, PHONE) VALUES (‘123456789’, ‘SMITH’, ‘JOHN’,
‘JAY’, ‘12 BEACON CT’, ‘INDIANAPOLIS’, ‘IN’, ‘46222’, ‘3172996868’);
School of Computer Engineering
DML cont..
13

Inserting NULL Values


Inserting a NULL value into a column of a table is a simple matter. You might
want to insert a NULL value into a column if the value of the column in question
is unknown. For instance, not every person carries a pager, so it would be
inaccurate to enter an erroneous pager number—not to mention, you would
not be budgeting space. You can insert a NULL value into a column of a table
using the keyword NULL. The syntax for inserting a NULL value follows:
insert into table_name values (‘column1 Value’, NULL, ‘column3 Value’);
Class Work
 Insert all values for an employee except a ZIP, Phone and pager number.
 Insert all values for an employee except a ZIP.
 Insert all values for an employee except Phone

School of Computer Engineering


Updating Existing Data
14

Modification of pre-existing data in a table is done with the UPDATE command.


This command does not add new records to a table, nor does it remove records
—UPDATE simply updates existing data. The update is generally used to update
one table at a time in a database, but you can use it to update multiple columns
of a table at the same time. An individual row of data in a table can be updated,
or numerous rows of data can be updated in a single statement, depending on
what’s needed.
Updating the Value of a Single Column
The most simple form of the UPDATE statement is its use to update a single
column in a table. Either a single row of data or numerous records can be
updated when updating a single column in a table. The syntax for updating a
single column follows: WHERE statement is optional

update table_name set column_name = value [where condition];


Example - UPDATE ORDER SET QTY = 1 WHERE ORD_NUM = ‘23A16’;

School of Computer Engineering


Updating Existing Data
15

Updating Multiple Columns in One or More Records


To update multiple columns with a single UPDATE statement, the syntax is
update table_name
set column1 = ‘value’, [column2 = ‘value’,] [column3 = ‘value’]
[where condition]; WHERE statement is optional
Example - UPDATE ORDER SET QTY = 1, CUST_ID = ‘221’ WHERE ORD_NUM =
‘23A16’;

Class Work
 Update ZIP to “751024” for the employees who are staying in “Bhubaneswar”

School of Computer Engineering


Deleting Data from Table
16

The DELETE command removes entire rows of data from a table. It does not
remove values from specific columns; a full record, including all columns, is
removed. To delete a single record or selected records from a table, the syntax
is:
delete from table_name
[where condition]; WHERE statement is optional

Example - DELETE FROM ORDERS WHERE ORD_NUM = ‘23A16’;

NOTE: WHERE clause is an essential part of the DELETE statement if you are
attempting to remove selected rows of data from a table. DELETE statement
without the use of the WHERE clause is rarely used & will delete all the records.

School of Computer Engineering


17

Thank You
End of Lab 2
School of Computer Engineering
Experiments
18

1.Using appropriate integrity constraints, design & create suitable tables


to store data for the following:
 Restaurant Booking System involving –
Customer/Booking/Table/Order/Menu/Ingredients/Staff/Billing
 Hospitality Management System involving –
Patient/Doctor/Disease/Hospital/Billing
 Car Service Center System involving –
Car/Customer/Booking/Mechanic/Billing
 Banking System involving –
Branch/Customer/Account/Employee/
Transaction/Product
2.Add valid & suitable data (minimum 5 records) to all tables using INSERT,

UPDATE and DELETE (if required) statement.


School of Computer Engineering

You might also like