DBMS Lab Manual
DBMS Lab Manual
01
AIM:
OBJECTIVE:
After completing the exercise the students can able to Understand how to create a table with list of
fields, Modify a row using where clause, Drop a table, Delete the unwanted rows in a table.
ALGORITHM:
DDL COMMAND:
CREATE
ALTER
DROP
TRUNCATE
COMMENT
RENAME
CREATE TABLE
Constraints are condition for the data item to be stored into a database. There are two types
of Constraints viz., Column Constraints and Table Constraints.
Tables
In relational database systems (DBS) data are represented using tables (relations). A query
issued against the DBS also results in a table. A table has the following structure:
Tuple or record 1
Tuple or record 2
.
.
.
Tuple or record n
Column 1 Column 2 Columnn
A table is uniquely identified by its name and consists of rows that contain the stored information,
each row containing exactly one tuple (or record). A table can have one or more columns. A column is
made up of a column name and a data type, and it describes an attribute of the tuples. The structure of a
table, also called relation schema, thus is defined by its attributes. The type of information to be stored in a
table is defined by the data types of the attributes at table creation time. Oracle offers the following basic
data types:
Sl.No Data Type Description
1 Char(n) Character String . n is the size of variable. Maximum size is 255 characters. The default size is 1
2 Varchar2(n) Character string . n is the size of the variable
3 Number Defines Numeric data type with space for 40 digit and space for sign and decimalpoint
4 Number (n) Numeric variable.n is the size of the variable
5 Number(n,d) Numeric variable.n is the size of variable and d is the size if the decimal point
6 Raw(size) Raw Binary data of length size bytes .Maximum size is 32767 bytes.
7 Integer It is same as number data type but values will be whole numbers. Columns defined with this format not accept
decimal values.
8 Integer(n) Specifies an integer data type of length n.
9 Long Defines a character data type upto 32760bytes. One one long column may be efined for table. This type of column
may not be used in sub queries, Where clauses or indexes.
10 Long Raw Same as LONG except it contains binary data or byte strings and not interpreted by PL/SQL
11 LOB Type LOB variables can used interchangeably with LONG and LONG RAW variables. It consists of BFILE,BLOB,CLOB and
NLOB
12 BFILE It is used to store large binary objects in operating system files outside the database
13 BLOB The BLOB datat ype to store large binary objects in the database, in-line or out-of-line. Every BLOB variable stores
a locator, which points to a large binary object. The size of a BLOB cannot exceed four gigabytes
14 CLOB The CLOB datatype to store large blocks of character data in the database, in-line or out-of-line. Both fixed-width
and variable-width character sets are supported. Every CLOB variable stores a locator, which points to a large block
of character data. The size of a CLOB cannot exceed four gigabytes
15 NCLOB The NCLOB datatype to store large blocks of NCHAR data in the database, in-line or out-of-line. Both fixed-width
and variable-width character sets are supported. Every NCLOB variable stores a locator, which points to a large
block of NCHAR data. The size of an NCLOB cannot exceed four gigabytes.
16 The DATE datatype to store fixed-length datetimes, which include the time of day in seconds since midnight. The
DATE date portion defaults to the first day of the current month; the time portion defaults to midnight. The date function
SYSDATE returns the current date and time
SQL uses the terms table, row, and column for relation, tuple, and attribute, respectively. A table can have
up to 254 columns which may have different or same data types and sets of values (domains), respectively.
Possible domains are alphanumeric data (strings), numbers and date formats.
Sample Databases used for illustration of SQL Commands is given below with ER Diagram and corresponding
Relational Model with suitable data entered in the tables.
Command:
Table created.
Constraints are condition for the data item to be stored into a database. There are two types
of Constraints viz., Column Constraints and Table Constraints.
Syntax:
[CONSTRAINT constraint name]
REFERENCES table
TABLE DESCRIPTION
It is used to view the table structure to confirm whether the table was created correctly.
QUERY: 02
Q2: Write a query to display the column name and data type of the table employee.
Command:
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUERY: 03
Q3: Write a query for create a from an existing table with all the fields
Syntax: syntax for create a table from an existing table with all fields.
SQL> CREATE TABLE <TRAGET TABLE NAME> SELECT * FROM<SOURCE TABLE NAME>;
Command:
Table created.
Command:
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUERY: 04
Q4: Write a query for create a from an existing table with selected fields
Syntax: Syntax for create a from an existing table with selected fields.
SQL> CREATE TABLE <TRAGET TABLE NAME> AS SELECT EMPNO, ENAMEFROM <SOURCE
TABLE NAME>;
Command:
Table created.
Command:
Q5: Write a query for create a new table from an existing table without any record:
Syntax: The syntax for create a new table from an existing table without any record.
SQL> CREATE TABLE <TRAGET TABLE NAME> AS SELECT * FROM<SOURCE TABLE NAME>
Command:
Table created.
Command:
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2);
ALTER & MODIFICATION ON TABLE
To modify structure of an already existing table to add one more columns and also modify
the existing columns.
QUERY: 06
Q6: Write a Query to Alter the column EMPNO NUMBER (4) TO EMPNO NUMBER (6).
Command:
Table altered.
Command:
EMPNO NUMBER(6)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUERY: 07
Q7. Write a Query to Alter the table employee with multiple columns (EMPNO,ENAME.)
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2);
QUERY: 08
Q8. Write a query to add a new column in to employee
Command:
SQL> ALTER TABLE EMP ADD QUALIFICATION VARCHAR2(6);
Table altered.
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)
QUERY: 09
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)
DOB DATE
DOJ DATE
REMOVE / DROP
It will delete the table structure provided the table should be empty.
QUERY: 10
Q10. Write a query to drop a column from an existing table employee
Command:
SQL> ALTER TABLE EMP DROP COLUMN DOJ;
Table altered.
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)
DOB DATE
QUERY: 11
Q10. Write a query to drop multiple columns from employee
Command:
SQL> ALTER TABLE EMP DROP (DOB, QUALIFICATION);
Table altered.
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
RENAME
QUERY: 12
Q10. Write a query to rename table emp to employee
Syntax:The Syntax for add a new column.
SQL> ALTER TABLE RENAME <OLD NAME> TO <NEW NAME>
Command:
SQL> ALTER TABLE RENAME EMP TO EMPLOYEE;
SQL> DESC EMPLOYEE;
Name Null? Type
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
TRUNCATE TABLE
If there is no further use of records stored in a table and the structure has to be retained then the records
alone can be deleted.
Syntax:
TRUNCATE TABLE <TABLE NAME>;
Example:
Truncate table EMP;
DROP:
To remove a table along with its structure and data.
Syntax:The Syntax for add a new column.
SQL> Drop table<table name>;
Command:
SQL> drop table employee;
RESULT:
Thus the SQL commands for DDL commands in RDBMS has been verified and executed successfully.
Practical No. 02
DATA MANIPULATION LANGUAGE (DML) COMMANDS IN RDBMS
AIM:
To execute and verify the DML commands are the most frequently used SQL commands and is
used to query and manipulate the existing database objects.
DML (DATA MANIPULATION LANGUAGE)
SELECT
INSERT
DELETE
UPDATE
ALGORITHM:
The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.
QUERY: 01
Command:
1 row created.
QUERY: 03
Q3. Write a query to insert the records in to employee using substitution method.
SQL :> INSERT INTO <TABLE NAME> VALUES< ‘&column name’, ‘&column nam
); e 2’, …..
Command:
SELECT
SELECT Statement is used to fetch the data from a database table which returns data in the form
of result table. These result tables are called result-sets.
QUERY: 02
Command:
The SQL UPDATE Query is used to modify the existing records in a table. You can use WHERE
clause with UPDATE query to update selected rows, otherwise all the rows would be affected.
QUERY: 04
Command:
1 row updated.
QUERY: 05
Command:
1 row updated.
The SQL DELETE Query is used to delete the existing records from a table. You can use
WHERE clause with DELETE query to delete selected rows, otherwise all the records would be deleted.
QUERY: 06
Command:
1 row deleted.
RESULT:
Thus the SQL commands for DML has been verified and executed successfully.
Practical No. 03
Performing Insertion, Deletion, Modifying, Altering, Updating and Viewing records based on
conditions.
AIM:
To performing insertion, deletion, modifying, altering, updating and viewing records based
on conditions.
ALGORITHM:
STEP 4: Insert the record into table based on some condition using WHERE CLAUSE
STEP 5: Update the existing records into the table based on some condition
BETWEEN Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
SQL> SELECT EMPNO,ENAME,JOB,SAL FROM EMP WHERE SAL BETWEEN 10000 AND 30000;
EMPNO ENAME JOB SAL
IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);
SQL> SELECT EMPNO,ENAME,JOB,SAL FROM EMP WHERE ENAME LIKE 'R '; //3_
7566 RAJAOWNER
no rows selected
SQL> SELECT EMPNO,ENAME,JOB,SAL FROM EMP WHERE ENAME LIKE ' R'; // 4_
SQL> SELECT EMPNO,ENAME,JOB,SAL FROM EMP WHERE ENAME LIKE 'K R'; // 3_
SQL> SELECT EMPNO,ENAME,JOB,SAL FROM EMP WHERE ENAME NOT LIKE 'R_J_';
7 rows selected.
RELATIONAL OPERATOR
SQL> SELECT EMPNO,ENAME,JOB,SAL FROM EMP WHERE SAL=55000;
EMPNO ENAME JOB SAL
ORDER BY
Syntax:
SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;
8 rows selected.
SQL> SELECT EMPNO,ENAME,JOB,SAL FROM EMP ORDER BY ENAME ASC;
EMPNO ENAME JOB SAL
TOP
Syntax
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
SQL> SELECT EMPNO,ENAME,JOB,SAL FROM EMP WHERE ROWNUM <4 ORDER BY ENAME;
EMPNO ENAME JOB SAL
DISTINCT
Syntax:
SELECT DISTINCT column_name,column_name
FROM table_name;
Ex:
SQL> SELECT DISTINCT JOB FROM EMP;
JOB
CLERK
SALESMAN
SR.SALESMAN
MANAGER
COE
OWNER
6 rows selected.
USING ALTER
This can be used to add or remove columns and to modify the precision of the datatype.
a) ADDING COLUMN
Syntax:
alter table <table_name> add <col datatype>;
Ex:
SQL> DESC EMP;
Name Null? Type
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(20)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(8,2)
DEPTNO NUMBER(3)
SQL> alter table EMP add TAX number;
Table altered.
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(20)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(8,2)
DEPTNO NUMBER(3)
TAX NUMBER
b) REMOVING COLUMN
Syntax:
alter table <table_name> drop <col datatype>;
Ex:
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(20)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(8,2)
DEPTNO NUMBER(3)
c) INCREASING OR DECREASING PRECISION OF A COLUMN
Syntax:
Ex:
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(20)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(8,2)
DEPTNO NUMBER(5)
Syntax:
alter table <table_name> set unused column <col>;
Ex:
SQL> alter table EMP set unused column DEPTNO;
Table altered.
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(20)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(8,2)
Ex:
SQL> alter table EMP drop unused columns;
Table altered.
e) RENAMING
COLUMN Syntax:
alter table <table_name> rename column <old_col_name> to <new_col_name>;
Ex:
SQL> alter table EMP rename column SAL to SALARY;
Table altered.
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(20)
MGR NUMBER(4)
HIREDATE DATE
SALARY NUMBER(8,2)
INSERT
Method 1
Method 2
Method 3
Method 4
Method 5
Ex:
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(20)
MGR NUMBER(4)
HIREDATE DATE
SALARY NUMBER(8,2)
EMP_NO NUMBER(4)
EMP_NAME VARCHAR2(10)
EMP_JOB VARCHAR2(20)
HR NUMBER(4)
HIREDATE DATE
SALARY NUMBER(8,2)
Method 6
MULTIBLE INSERTS
We have table called DEPT with the following columns and data
SQL> select * from DEPT;
DEPTNO DNAME LOC
----------- ---------- -------
10 accounting new york
20 research dallas
30 sales Chicago
40 operations boston
1 a 100
2 b 200
3 c 300
4 d
e 400
3 c 300
6 rows selected.
-- This inserts 9 rows because in the select statement retrieves 3 records (3 inserts for each row
retrieved) SQL> Select * from student;
NO NAME MARKS
1 a 100
2 b 200
3 c 300
4 d
e 400
3 c 300
1 a 100
1 a 100
1 a 100
2 b 200
2 b 200
2 b 200
3 c 300
3 c 300
3 c 300
15 rows selected.
e) MULTI INSERT WITH CONDITIONS BASED
ram 111
sam 222
tam 333
ram 111
sam 222
tam 333
SQL> select * from yourtbl1;
NAME NO
ramu 1111
ramu 1111
samu 2222
tamu 3333
-- This inserts 4 rows because the first condition satisfied 3 times, second condition
satisfied once and the last none.
f) MULTI INSERT WITH CONDITIONS BASED AND ELSE
ram 111
sam 222
tam 333
SQL> create table yourtbl1(name varchar2(20),no number(10));
Table created.
SQL> create table yourtbl2(name varchar2(20),no number(10));
Table created.
SQL> create table yourtbl3(name varchar2(20),no number(10));
Table created.
SQL> create table yourtbl4(name varchar2(20),no number(10));
Table created.
ramu 1111
ramu 1111
samu 2222
tamu 3333
g) MULTI INSERT WITH CONDITIONS BASED AND FIRST
ram 111
sam 222
tam 333
ramu 1111
-- This inserts 1 record because the first clause avoid to check the remaining conditions once the condition is satisfied.
h) MULTI INSERT WITH CONDITIONS BASED, FIRST AND ELSE
ram 111
sam 222
tam 333
SQL> insert first
when no=111 then
into yourtbl1 values('ramu',1111)
when name = 'bam' then
into yourtbl2 values('samu',2222)
when name = 'tam' then
into yourtbl3
values('tamu',3333) else
into yourtbl4 values('kamu',4444) select
* from mytbl1 where name='ram';
1 row created.
ramu 1111
ram 111
sam 222
tam 333
ramu 11111
SQL> select * from yourtbl2;
NAME NO
samu 22222
SQL> select * from yourtbl3;
NAME NO
tamu 33333
SQL> select * from yourtbl4;
NAME NO
kamu 44444
** You can use multi tables with specified fields, with duplicate rows, with conditions, with first and else clauses.
GROUP BY
Using group by, we can create groups of related information. Columns used in select must be used with group by;
otherwise it was not a group by expression.
Ex:
CLERK
SALESMAN
SR.SALESMAN
MANAGER
CLERK 8000
SALESMAN 8000
SR.SALESMAN 34000.55
MANAGER 75000
HAVING
This will work as where clause which can be used only with group by because of absence of where clause in group by.
RAM 101
RAM 101
SAM 102
SAM 102
RAMU
RAMU
SAMU 103
SAMU 103
SAMU 103
TAM
RAJA 555
KAJA 123
12 rows selected.
SQL> delete from myTBL t1
where t1.rowid > (select min(t2.rowID) from myTBL
t2 where t1.name = t2.name and t1.mark = t2.mark);
4 rows deleted.
SQL> SELECT * FROM myTBL;
NAME MARK
--------- ----------
RAM 101
SAM 102
RAMU
SAMU 103
TAM
RAJA 555
KAJA 123
8 rows selected.
Using UPDATE
SQL> UPDATE EMP SET SAL = 55555,JOB = 'SR.MANAGER' WHERE ENAME LIKE
'R '; 1 row updated.
RESULT:
Thus the SQL commands for Performing Insertion, Deletion, Modifying, Altering, Updating and
Viewing records based on conditions has been verified and executed successfully.
Practical No. 03
Creation of Views
AIM:
To create the view, execute and verify the various operations on views.
OBJECTIVE:
A view is nothing more than a SQL statement that is stored in the database with an associated name. A
view is actually a composition of a table in the form of a predefined SQL query.
A view can contain all rows of a table or select rows from a table. A view can be created from one or
many tables which depends on the written SQL query to create a view.
Views, which are kind of virtual tables, allow users to do the following:
Structure data in a way that users or classes of users find natural or intuitive.
Restrict access to the data such that a user can see and (sometimes) modify exactly what they
need and no more.
ALGORITHM:
Database views are created using the CREATE VIEW statement. Views can be created from a
single table, multiple tables, or another view. To create a view, a user must have the appropriate
system privilege according to the specific implementation.
TABLE DESCRIPTION:
EMPLOYEE_NAME VARCHAR2(10)
EMPLOYEE_NO NUMBER(8)
DEPT_NAME VARCHAR2(10)
DEPT_NO NUMBER(5)
DATE_OF_JOIN DATE
CREATE VIEW
SUNTAX FOR CREATION OF VIEW
CREATION OF VIEW
DESCRIPTION OF VIEW
EMPLOYEE_NAME VARCHAR2(10)
EMPLOYEE_NO NUMBER(8)
DEPT_NAME VARCHAR2(10)
DEPT_NO NUMBER(5)
DISPLAY VIEW:
COMMAND:
Command:
SQL> DELETE FROM EMPVIEW WHERE
EMPLOYEE_NAME='SRI'; 1 row deleted.
SQL> SELECT * FROM EMPVIEW;
EMPLOYEE_N EMPLOYEE_NO DEPT_NAMEDEPT_NO
UPDATE STATEMENT:
A view can be updated under certain conditions:
The SELECT clause may not contain the keyword DISTINCT.
The SELECT clause may not contain summary functions.
The SELECT clause may not contain set functions.
The SELECT clause may not contain set operators.
The SELECT clause may not contain an ORDER BY
clause. The FROM clause may not contain multiple tables.
The WHERE clause may not contain subqueries.
The query may not contain GROUP BY or
HAVING. Calculated columns may not be updated.
All NOT NULL columns from the base table must be included in the view in
order for the INSERT query to function.
SYNTAX:
Command:
SYNTAX:
EXAMPLE
SQL>DROP VIEW
EMPVIEW; view droped
CREATE A VIEW WITH SELECTED FIELDS:
SYNTAX:
EXAMPLE-2:
EXAMPLE-3:
Note:
Replace is the keyboard to avoid the error “ora_0095:name is already used by an existing ab
TYPE-1:
TYPE-2:
TYPE-3:
View created.
EID EMPNAME DNO D_NAME D_LOC
JOIN VIEW:
EXAMPLE-5:
SQL> CREATE OR REPLACE VIEW DEPT_EMP_VIEW AS SELECT A.EMPNO,
A.ENAME,
A.DEPTNO,
B.DNAME,
B.LOC
FROM EMPL A, DEPMT B
WHERE A.DEPTNO=B.DEPTNO;
SQL> SELECT * FROM DEPT_EMP_VIEW;
View created.
FORCE VIEW:
EXAMPLE-6:
RESULT:
Thus the SQL commands for View has been verified and executed successfully.
Practical No. 04
Creating an Employee database to set various constraints in RDBMS
AIM:
ALGORITHM:
STEP 4: Insert record values into the table and then check the constraint.
STEP 5: disable the constraints and insert the values into the table.
STEP 6: if you want to re-enable the constraint then enable you can do.
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.
a) Domain Integrity
b) Entity Integrity
c) Referential Integrity
TYPES OF CONSTRAINTS:
1) Primary key
2) Foreign key/references
3) Check
4) Unique
5) Not null
6) Null
7) Default
OPERATION ON CONSTRAINT:
i) ENABLE
ii) DISABLE
iii) DROP
PRIMARY KEY CONSTRAINTS
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.
QUERY: 13
Q13. Write a query to create primary constraints with column
level Syntax: Column level constraints using primary key.
SQL> CREATE<OBJ.TYPE><OBJ.NAME> (COLUMN NAME.1 <DATATYPE> (SIZE)<TYPE OF
CONSTRAINTS>, COLUMN NAME.1 <DATATYPE> (SIZE)........................................);
Command:
SQL> CREATE TABLE TBL_PKEY(
RegNo NUMBER(5) PRIMARY
KEY, Name VARCHAR2(20),
ANY_SUB_MARK NUMBER(3)
);
Table created.
SQL> insert into result values(10001,'raju',75);
1 row created.
SQL> insert into result values(10002,'KAMAL;',100);
1 row created.
SQL> insert into result values(0,'RAVI;',75);
1 row created.
SQL> insert into result values(NULL,'KAVI',65);
1 row created.
SQL> insert into TBL_PKEY values(10001,'raju',75);
1 row created.
SQL> insert into TBL_PKEY values(10002,'raj',85);
1 row created.
SQL> insert into TBL_PKEY values(0,'Kaj',22);
1 row created.
SQL> insert into TBL_PKEY values(NULL,'Kaj',22);
insert into TBL_PKEY values(NULL,'Kaj',22)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SENTHIL"."TBL_PKEY"."REGNO")
SQL> insert into TBL_PKEY values(10002,'RAJAN',95);
insert into TBL_PKEY values(10002,'RAJAN',95)
*
ERROR at line 1:
ORA-00001: unique constraint (SENTHIL.SYS_C0011650) violated
SQL> insert into TBL_PKEY values(10003,'RAJA',85);
1 row created.
SQL> select * FROM TBL_PKEY;
REGNO NAME ANY_SUB_MARK
10001 raju 75
10002 raj 85
0 Kaj 22
10003 RAJA 85
Column level constraints using primary key with naming convention:
QUERY: 14
Q14. Write a query to create primary constraints with column level with naming convention
Syntax: syntax for column level constraints using primary key.
SQL >CREATE <OBJ.TYPE><OBJ.NAME> (
COL NAME.1 <DATATYPE> (SIZE)CONSTRAINTS <NAME OF CONSTRAINTS><TYPE OF
CONSTRAINTS>, COL NAME.2 <DATATYPE> (SIZE)… ........................................................... );
Command:
SQL>CREATE TABLE EMPLOYEE (
EMPNO NUMBER (4) CONSTRAINT EMP_EMPNO_PK PRIMARY KEY,
ENAMEVARCHAR2 (10),JOB VARCHAR2 (6),SAL NUMBER (5),DEPTNO NUMBER (7));
Table level primary key constraints:
QUERY: 15
Q15. Write a query to create primary constraints with table level with naming convention
Syntax: The syntax for table level constraints using primary key
SQL: >CREATE <OBJ.TYPE><OBJ.NAME> (COLUMN NAME.1 <DATATYPE>
(SIZE), COLUMN NAME.1 <DATATYPE> (SIZE),
CONSTRAINTS <NAME OF THE CONSTRAINTS><TYPE OF THE CONSTRAINTS>);
Command:
SQL>CREATE TABLE EMPLOYEE (EMPNO NUMBER(6),ENAME VARCHAR2(20),JOB
VARCHAR2(6), SAL NUMBER(7), DEPTNO NUMBER(5),
CONSTRAINT EMP_EMPNO_PK PRIMARY KEY(EMPNO));
Command:
SQL> CREATE TABLE DEPT(
DEPTNO NUMBER(3) PRIMARY KEY,
DNAME VARCHAR2(20),LOCATION VARCHAR2(15));
Table created.
SQL> desc DEPT;
Name Null? Type
Command:
SQL> CREATE TABLE EMPL(EMPNO NUMBER(4),
DEPTNO NUMBER(3) REFERENCES DEPT(DEPTNO),
DESIGN VARCHAR2(10));
Table created.
SQL> desc EMPL;
Name Null? Type
EMPNO NUMBER(4)
DEPTNO NUMBER(3)
DESIGN VARCHAR2(10)
QUERY: 18
Write a query to create foreign key constraints with column level
Parent Table:
Syntax: The syntax for column level constraints using primary key.
SQL :> CREATE<OBJ.TYPE><OBJ.NAME> (COLUMN NAME.1 <DATATYPE>(SIZE)<TYPE OF
CONSTRAINTS>,COLUMN NAME.1 <DATATYPE> (SIZE)…);
Child Table:
Syntax: syntax for column level constraints using foreign key.
SQL :> CREATE<OBJ.TYPE><OBJ.NAME> (COLUMN NAME.1 <DATATYPE>(SIZE) ,
COLUMN NAME2 <DATATYPE> (SIZE) CONSTRAINT <CONST.NAME>REFERENCES <TABLE NAME>
(COLUMN NAME>…);
Command:
SQL>CREATE TABLE DEPT (DEPTNO NUMBER (2) PRIMARYKEY,
DNAME VARCHAR2 (20), LOCATION VARCHAR2 (15));
SQL>CREATE TABLE EMP4A (EMPNO NUMBER (3),
DEPTNO NUMBER (2) CONSTRAINT EMP4A_DEPTNO_FK REFERENCES DEPT (DEPTNO),
DESIGN VARCHAR2 (10));
Table level foreign key constraints:
QUERY: 19
Write a query to create foreign key constraints with Table level.
Parent Table:
Syntax:
SQL :> CREATE<OBJ.TYPE><OBJ.NAME> (COLUMN NAME.1 <DATATYPE>(SIZE) <TYPE OF
CONSTRAINTS>, COLUMN NAME.1 <DATATYPE> (SIZE)…);
Child Table:
Syntax: The syntax for table level constraints using foreign key.
SQL :> CREATE<OBJ.TYPE><OBJ.NAME> (COLUMN NAME.1
<DATATYPE>(SIZE), COLUMN NAME2 <DATATYPE> (SIZE),
CONSTRAINT <CONST.NAME>REFERENCES <TABLE NAME> (COLUMN NAME>);
Command:
SQL>CREATE TABLE DEPT(DEPTNO NUMBER(2) PRIMARY KEY,
DNAME VARCHAR2(20),LOCATION VARCHAR2(15));
Command:
SQL>CREATE TABLE DEPT(DEPTNO NUMBER(2) PRIMARY KEY, DNAME VARCHAR2(20),
LOCATION VARCHAR2 (15));
SQL>CREATE TABLE EMP5 (EMPNO NUMBER(3), DEPTNO NUMBER (2), DESIGN VARCHAR2 (10));
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.
QUERY: 21
Write a query to create Check constraints with column level
Command:
SQL>CREATE TABLE EMP7(EMPNO NUMBER(3),ENAME VARCHAR2(20),DESIGN VARCHAR2(15),
DEPTNO NUMBER(2));
QUERY: 22
Write a query to create Check constraints with table level
Command:
QUERY:23
Write a query to create Check constraints with table level using alter command.
Command:
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.
QUERY:26
Write a query to create unique constraints with table level
Syntax: syntax for table level constraints with check using alter.
Command:
SQL>CREATE TABLE EMP12(EMPNO NUMBER(3),ENAME VARCHAR2(20),DESIGN
VARCHAR2(15), SAL NUMBER(5));
SQL>ALTER TABLE EMP12 ADD CONSTRAINT EMP12_DESIGN_UKUNIQUE(DESING);
NOT NULL CONSTRAINTS
While creating tables, by default the rows can have null value .the enforcement of not null constraint in a
table ensure that the table contains values.
QUERY: 27
Command:
NULL CONSTRAINTS
Setting null value is appropriate when the actual value is unknown, or when a value would not be meaningful.
Not null Integrity constraint cannot be defined using the alter table command when the table contain rows.
QUERY:28
Command:
Default constraints assign the default values if the values is not passed at the time of inserting the values to the table
QUERY:28
Command:
REGNO NUMBER(5),
NAME VARCHAR2(20),
);
Table created.
1 row created.
1 row created.
1 row created.
1001 ARJUN
1005 55
1001 RAJ 78
Practical No. 05
CREATING RELATIONSHIP BETWEEN THE DATABASES IN RDBMS
AIM:
To Execute and verify The SQL Commands For Set Operators Implementation In Relational Model
.
OBJECTIVE:
Set operators are used to retrieve the data from two or multiple tables.
They are different types.
Union
Union all
Intersect
Minus
ALGORITHM:
STEP 1: Start
STEP 2: Create two different tables with its essential attributes.
STEP 3: Insert attribute values into the tables.
STEP 4: Create the result for the various set operation.
STEP 5: Execute Command and extract information from the tables.
STEP 6: Stop
SAMPLE TABLES
UNION ALL
This will combine the records of multiple tables having the same structure but including duplicates.
Ex:
SQL> select * from student_IT union all select * from student_ECE;
REG_NO NAME BRANC SUBJECT
INTERSECT
This will give the common records of multiple tables having the same structure.
Ex:
RESULT:
Thus the SQL commands for SET operators has been verified and executed successfully.
Practical No. 06
CREATING RELATIONSHIP BETWEEN THE DATABASES IN RDBMS
AIM:
Nested Query can have more than one level of nesting in one single query. A SQL nested query is
a SELECT query that is nested inside a SELECT, UPDATE, INSERT, or DELETE SQL query.
ALGORITHM:
STEP 4: Create the Nested query from the above created table.
TABLE- 2
MAHESH
MANOJ
KARTHIK
MANI
VIKI
MOHAN
NAVEEN
PRASAD
AGNESH
RESULT:
Thus the SQL commands for to implementation of nested queries has been verified and
executed successfully.
Practical No. 07
CREATING RELATIONSHIP BETWEEN THE DATABASES IN RDBMS
AIM:
To execute and verify the SQL commands for various join operation.
ALGORITHM:
JOINS:
Types of Joins:
1. EQUI_JOIN
2. NON EQUI_JOIN
3. SELF JOIN
4. OUTER JOIN
Ex:
select empno, ename, dname from emp, dept where emp.deptno = dept.deptno;
Note:
Ex:
Note:
we need to mention table name dot column(emp.deptno) name for the common column to resolve
the any table.
To improve the performance of the join we need mention table name dot column name for all the columns.
Ex:
Table alias:
By using a table alias length of the table reduces and at the same time performance is maintains.
Table alias are create in same clause can be used in select clause as well as where clause.
Table alias is temporary once the query is executed the table alias are losed.
Ex:
City State
Newyork AP
Dallas Mh
Ex:
When we do not use NON EQUI JOIN to operator in the join condition is NON EQUI JOIN.
Ex:
SELF JOIN:
When a table is joining to it self it is called self join. In self joins we need to create two table
aliases for the same table.
When tables are joined without any join condition it is called Cartesian product. In the result we
get all possible combination.
ANSI JOINS:
INNER JOINS:
Ex:
Ex:
SQL>Select empno, ename, sal, deptno, dname,loc from NATURAL JOIN dept;
Ex:
DEFAULT:
Ex:
Sname varchar2(10),
SUPER KEY:
Combination of columns which can be used unique key identify every row is called as super
key. Table object
Column Attributes
Row Tuple/Record
OUTER JOINS:
SQL Syntax:
ANSI SYSTAX:
SQL Syntax:
ANSI SYNTAX:
ANSI SYNTAX:
RESULT:
Thus the SQL commands to implementation the join operations has been verified and
executed successfully.