Professional Documents
Culture Documents
DBMS Experiment - Lab 2
DBMS Experiment - Lab 2
Strictly for internal circulation (within KIIT) and reference only. Not for outside circulation without permission
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.
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 ));
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;
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
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
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;
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
Class Work
Update ZIP to “751024” for the employees who are staying in “Bhubaneswar”
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
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.
Thank You
End of Lab 2
School of Computer Engineering
Experiments
18