SlideShare a Scribd company logo
1 of 18
Database Management System Lab (CS 29006)
KALINGA INSTITUTE OF INDUSTRIAL
TECHNOLOGY
School of Computer Engineering
1 Credit
Strictly for internal circulation (within KIIT) and reference only. Not for outside circulation without permission
Course Committee
Lab Contents
School of Computer Engineering
Sr
#
Major and Detailed Coverage Area Lab#
1 Integrity Constraints
DDL
DML
2
2
Integrity Constraint
School of Computer Engineering
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, ….
Integrity Constraint cont…
School of Computer Engineering
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.
Integrity Constraint cont…
School of Computer Engineering
5
 Unique Key Constraint cont… – Example:
CREATE TABLE EMPLOYEE
(EMP_ID CHAR(9) NOT NULL PRIMARY KEY,
.....
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,
....
CONSTRAINT EMP_ID_FK FOREIGN KEY (EMP_ID) REFERENCES EMPLOYEE
(EMP_ID));
Represents continuation
Represents continuation
Integrity Constraint cont…
School of Computer Engineering
6
 Foreign Key Constraint example-
Class Work
Recreate the STUDENT table with appropriate integrity constraint.
Integrity Constraint cont…
School of Computer Engineering
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,
...
CONSTRAINT PAY_RATE_CHK CHECK ( PAY_RATE > 12.50 ));
Represents continuation
Integrity Constraint cont…
School of Computer Engineering
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;
Class Work
School of Computer Engineering
9
Using appropriate integrity constraints, design suitable tables to store data for
the following system:
 Timetable System involving -
Student/Section/Course/Semester/Class Room/Faculty
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
 Identify data type for each attribute covering all tables
 Define the relationship among the tables
 Expand the attributes (if any) for each table
Further Action
DDL - Table Creation from existing Table
School of Computer Engineering
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 ]
FROM TABLE_NAME
[ WHERE ]
Example
 CREATE TABLE PRODUCT_TMP AS SELECT * FROM PRODUCT;
 CREATE TABLE PRODUCT_TMP AS SELECT * FROM PRODUCT WHERE COST > 30;
Represents either * or individual columns
WHERE statement is optional
DDL – Altering a Table
School of Computer Engineering
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
DML – Data Manipulation
School of Computer Engineering
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
13
DML cont..
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
Updating Existing Data
School of Computer Engineering
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:
update table_name set column_name = value [where condition];
Example - UPDATE ORDER SET QTY = 1 WHERE ORD_NUM = ‘23A16’;
WHERE statement is optional
Updating Existing Data
School of Computer Engineering
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];
Example - UPDATE ORDER SET QTY = 1, CUST_ID = ‘221’ WHERE ORD_NUM =
‘23A16’;
WHERE statement is optional
Class Work
 Update ZIP to “751024” for the employees who are staying in “Bhubaneswar”
Deleting Data from Table
School of Computer Engineering
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];
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.
WHERE statement is optional
School of Computer Engineering
Thank You
End of Lab 2
17
Experiments
School of Computer Engineering
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.

More Related Content

Similar to 2. DBMS Experiment - Lab 2 Made in SQL Used

Similar to 2. DBMS Experiment - Lab 2 Made in SQL Used (20)

Dms 22319 micro project
Dms 22319 micro projectDms 22319 micro project
Dms 22319 micro project
 
SQL
SQLSQL
SQL
 
Sql wksht-2
Sql wksht-2Sql wksht-2
Sql wksht-2
 
Module 3
Module 3Module 3
Module 3
 
Integrity and security
Integrity and securityIntegrity and security
Integrity and security
 
DBMS LAB M.docx
DBMS LAB M.docxDBMS LAB M.docx
DBMS LAB M.docx
 
SQL Query
SQL QuerySQL Query
SQL Query
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdf
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating database
 
Interview Questions.pdf
Interview Questions.pdfInterview Questions.pdf
Interview Questions.pdf
 
Lab2 ddl commands
Lab2 ddl commandsLab2 ddl commands
Lab2 ddl commands
 
DDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using OracleDDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using Oracle
 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)
 
PL/SQL 3 DML
PL/SQL 3 DMLPL/SQL 3 DML
PL/SQL 3 DML
 
Review of SQL
Review of SQLReview of SQL
Review of SQL
 
Intro to tsql unit 9
Intro to tsql   unit 9Intro to tsql   unit 9
Intro to tsql unit 9
 
Adbms
AdbmsAdbms
Adbms
 
Introduction to sql new
Introduction to sql newIntroduction to sql new
Introduction to sql new
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptx
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 

2. DBMS Experiment - Lab 2 Made in SQL Used

  • 1. Database Management System Lab (CS 29006) KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY School of Computer Engineering 1 Credit Strictly for internal circulation (within KIIT) and reference only. Not for outside circulation without permission Course Committee
  • 2. Lab Contents School of Computer Engineering Sr # Major and Detailed Coverage Area Lab# 1 Integrity Constraints DDL DML 2 2
  • 3. Integrity Constraint School of Computer Engineering 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, ….
  • 4. Integrity Constraint cont… School of Computer Engineering 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.
  • 5. Integrity Constraint cont… School of Computer Engineering 5  Unique Key Constraint cont… – Example: CREATE TABLE EMPLOYEE (EMP_ID CHAR(9) NOT NULL PRIMARY KEY, ..... 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, .... CONSTRAINT EMP_ID_FK FOREIGN KEY (EMP_ID) REFERENCES EMPLOYEE (EMP_ID)); Represents continuation Represents continuation
  • 6. Integrity Constraint cont… School of Computer Engineering 6  Foreign Key Constraint example- Class Work Recreate the STUDENT table with appropriate integrity constraint.
  • 7. Integrity Constraint cont… School of Computer Engineering 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, ... CONSTRAINT PAY_RATE_CHK CHECK ( PAY_RATE > 12.50 )); Represents continuation
  • 8. Integrity Constraint cont… School of Computer Engineering 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;
  • 9. Class Work School of Computer Engineering 9 Using appropriate integrity constraints, design suitable tables to store data for the following system:  Timetable System involving - Student/Section/Course/Semester/Class Room/Faculty 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  Identify data type for each attribute covering all tables  Define the relationship among the tables  Expand the attributes (if any) for each table Further Action
  • 10. DDL - Table Creation from existing Table School of Computer Engineering 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 ] FROM TABLE_NAME [ WHERE ] Example  CREATE TABLE PRODUCT_TMP AS SELECT * FROM PRODUCT;  CREATE TABLE PRODUCT_TMP AS SELECT * FROM PRODUCT WHERE COST > 30; Represents either * or individual columns WHERE statement is optional
  • 11. DDL – Altering a Table School of Computer Engineering 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
  • 12. DML – Data Manipulation School of Computer Engineering 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’);
  • 13. School of Computer Engineering 13 DML cont.. 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
  • 14. Updating Existing Data School of Computer Engineering 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: update table_name set column_name = value [where condition]; Example - UPDATE ORDER SET QTY = 1 WHERE ORD_NUM = ‘23A16’; WHERE statement is optional
  • 15. Updating Existing Data School of Computer Engineering 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]; Example - UPDATE ORDER SET QTY = 1, CUST_ID = ‘221’ WHERE ORD_NUM = ‘23A16’; WHERE statement is optional Class Work  Update ZIP to “751024” for the employees who are staying in “Bhubaneswar”
  • 16. Deleting Data from Table School of Computer Engineering 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]; 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. WHERE statement is optional
  • 17. School of Computer Engineering Thank You End of Lab 2 17
  • 18. Experiments School of Computer Engineering 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.