DBMS Manual1
DBMS Manual1
AIM:
To create a DDL to perform creation of table, alter, modify and drop column.
DDL COMMANDS
1. The Create Table Command: - it defines each column of the table uniquely. Each column
has minimum of three attributes, a name , data type and size.
Syntax:
Syntax:
Syntax:
Syntax:
Syntax:
Syntax:
7. Destroying tables.
Syntax:
CREATION OF TABLE:
SYNTAX:
EXAMPLE:
row created.
row created.
OUTPUT:
Table created.
row created.
SYNTAX:
EXAMPLE:
OUTPUT:
ID NAME GAME
1 Mercy Cricket
2 Sharmi Tennis 19
SYNTAX:
EXAMPLE:
OUTPUT:
MODIFY
desc student;
Id Number(6)
Name Varchar(20)
Game Varchar(25)
Age Number(4)
DROP:
EXAMPLE:
SQL>Table dropped.
TRUNCATE TABLE
DESC
--------------------------------- --------
EName VarChar(15)
Queries:
Q1. Create a table called EMP with the following structure.
Name Type
---------- ----------------------
EMPNO NUMBER(6)
ENAME VARCHAR2(20)
JOB VARCHAR2(10)
DEPTNO NUMBER(3)
SAL NUMBER(7,2)
Allow NULL for all columns except ename and job.
Solution:
1. Understand create table syntax.
2. Use the create table syntax to create the said tables.
3. Create primary key constraint for each table as understand from logical table structure.
Ans:
SQL> create table emp(empno number(6),ename varchar2(20)not null,job varchar2(10) not
null, deptno number(3),sal number(7,2));
Table created.
Q2: Add a column experience to the emp table.
Q3: Modify the column width of the job field of emp table.
Solution:
1. Use the alter table syntax.
2. Modify the column width and its data type.
Ans: SQL> alter table emp modify(job varchar2(12));
Table altered.
Q5: create the emp1 table with ename and empno, add constraints to check the empno value
while entering (i.e) empno > 100.
Solution:
1. Learn alter table syntax.
Q7: Truncate the emp table and drop the dept table
Solution:
RESULT:
Thus the DDL commands have been executed successfully.
To study the various DML commands and implement them on the database.
DML COMMANDS
DML commands are the most frequently used SQL commands and is used to query
and manipulate the existing database objects. Some of the commands are Insert, Select,
Update, Delete.
Insert Command This is used to add one or more rows to a table. The values are separated by
commas and the data types char and date are enclosed in apostrophes. The values must be
entered in the same order as they are defined.
Select Commands It is used to retrieve information from the table. It is generally referred to as
querying the table. We can either display all columns in a table or only specify column from
the table.
Update Command It is used to alter the column values in a table. A single column may be
updated or more than one column could be updated.
Delete command After inserting row in a table we can also delete them if required. The delete
command consists of a from clause followed by an optional where clause.
1 row created.
Q2: Insert more than a record into emp table using a single insert command.
1 row created.
1 row created.
Q3: Update the emp table to set the salary of all employees to Rs15000/- who are working as
ASP
1 Mathi AP 1 10000
1 Mathi AP 1 10000
Q4: Create a pseudo table employee with the same structure as the table emp and insert rows
into the table using select clauses.
Table created.
EMPNO NUMBER(6)
DEPTNO NUMBER(3)
SAL NUMBER(7,2)
RESULT:
Thus the DML commands have been executed successfully.
AIM
Q2: List the records in the emp table orderby salary in ascending order.
Ans: SQL> select * from emp order by sal;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
5 Akalya AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
Q3: List the records in the emp table orderby salary in descending order.
Ans: SQL> select * from emp order by sal desc;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
Q5: Display deptno from the table employee avoiding the duplicated values.
Solution:
1. Use SELECT FROM syntax.
2.Select should include distinct clause for the deptno.
Ans: SQL> select distinct deptno from emp;
DEPTNO
----------
1
Store_Name
Los Angeles
Result:
Store_Name
Los Angeles
15IT302J: DATABASE MANAGEMENT SYSTEM Page 17
San Diego
Boston
8. If we want to select all stores with sales greater than $1,000 or all stores with sales less than
$500 but greater than $275 in Table Store_Information, we key in,
SQL>SELECT Store_Name FROM Store_Information WHERE Sales > 1000 OR (Sales < 500
AND Sales > 275);
Result:
Store_Name
Los Angeles
San Francisco
9. To select all records for the Los Angeles and the San Diego stores in Table
Store_Information, we key in,
Result:
Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
10. To select view all sales information between January 6, 1999, and January 10, 1999, we key
in,
Note that date may be stored in different formats in different databases. This tutorial simply
choose one of the formats.
Result:
11. We want to find all stores whose name contains 'AN'. To do so, we key in,
Result:
Store_Name Sales Txn_Date
LOS ANGELES 1500 Jan-05-1999
SAN DIEGO 250 Jan-07-1999
SAN FRANCISCO 300 Jan-08-1999
12. To list the contents of Table Store_Information by Sales in descending order, we key in,
Result:
Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
Boston 700 Jan-08-1999
San Francisco 300 Jan-08-1999
San Diego 250 Jan-07-1999
13. To see only the stores with sales over $1,500, we would type,
Result:
Store_Name SUM(Sales)
Los Angeles 1800
RESULT:
Thus the Basic select commands have been executed successfully.
AIM
1. The LIMIT clause restricts the number of results returned from a SQL statement. It is
available in MySQL.
Syntax
The syntax for LIMIT is as follows:
[SQL Statement 1]
LIMIT [N];
To retrieve the two highest sales amounts in Table Store_Information, we key in:
2. The TOP keyword restricts the number of results returned from a SQL statement in Microsoft
SQL Server.
Syntax
2. [M] PERCENT: The number of records corresponding to M% of all qualifying records are
returned.
Examples
Table Store_Information
Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
San Francisco 300 Jan-08-1999
To show the two highest sales amounts in Table Store_Information, we key in,
To show the top 25% of sales amounts from Table Store_Information, we key in,
3. EXISTS is a Boolean operator used in a subquery to test whether the inner query returns any
row.
If it does, then the outer query proceeds. If not,
the outer query does not execute, and the entire SQL statement returns nothing.
SELECT "column_name1"
FROM "table_name1"
WHERE EXISTS
(SELECT *
FROM "table_name2"
WHERE "condition");
SUM(Sales)
2750
4. CASE is used to provide if-then-else type of logic to SQL. There are two formats: The first is
a Simple CASE expression,
where we compare an expression to static values. The second is a Searched CASE expression,
where we compare an expression to one or more logical conditions.
Simple CASE Expression Syntax
5. AUTO_INCREMENT is used in MySQL to create a numerical primary key value for each
additional row of data.5
Table USER_TABLE
6. he purpose of the SQL UNION query is to combine the results of two queries together while
removing duplicates. In other words, when using UNION, only unique values are returned
(similar to SELECT DISTINCT).
Syntax
[SQL Statement 1]
UNION
[SQL Statement 2];
Txn_Date
Jan-05-1999
Jan-07-1999
Jan-08-1999
7. The INTERSECT command in SQL combines the results of two SQL statement and returns
only data that are present in both SQL statements.
INTERSECT can be thought of as an AND operator (value is selected only if it appears in both
statements), while UNION and UNION ALL can be thought of as an OR operator (value is
selected if it appears in either the first or the second statement).
Syntax
[SQL Statement 1]
INTERSECT
[SQL Statement 2];
Txn_Date
Jan-07-1999
8. The MINUS command operates on two SQL statements. It takes all the results from the first
SQL statement, and then subtract out the ones that are present in the second SQL statement to get
the final result set. If the second SQL statement includes results not present in the first SQL
statement, such results are ignored.
Syntax
[SQL Statement 1]
MINUS
[SQL Statement 2];
RESULT:
Constraints are used to limit the type of data that can go into a table. This ensures the accuracy
and reliability of the data in the table. If there is any violation between the constraint and the data
action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column, and
table level constraints apply to the whole table.
Q5. Write a query to create Check constraints with table level using alter command
Q7. Write a query to create Not Null constraints with column level
RESULT:
SQL joins are used to query data from two or more tables, based on a relationship between
certain columns in these tables.
The INNER JOIN keyword return rows when there is at least one match in both tables.
The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no
matches in the right table (table_name2).
The RIGHT JOIN keyword Return all rows from the right table (table_name2), even if there are
no matches in the left table (table_name1).
The FULL JOIN keyword return rows when there is a match in one of the tables.
Table:1 - ORDERS
SQL> CREATE table orders(O_Id number(5),
Orderno number(5),
P_Id number(3));
Table created.
1 row created.
1 row created.
1 row created.
TABLE SECTION:
Table created.
1 row created.
1 row created.
1 row created.
OUTPUT
LASTNAME FIRSTNAME ORDERNO
------------------ ------------------ ---------------
Hansen Ola 22456
Hansen Ola 24562
Pettersen Kari 77895
Pettersen Kari 44678
Svendson Tove
4 24562 1
5 34764 15
SQL>SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
INNER JOIN
SQL>SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
RESULT:
CHARACTER/STRING FUNCTION:
RPAD('
------
hai***
SQL> select lpad('hai',3,'*')from dual;
LPAD('
------
***hai
SQL> select replace('Dany','y','ie')from dual;
REPLACE
-------
Danie
SQL> select translate('cold','ld','ol')from dual;
TRANSL
------
cool
NEXT_DAY(
---------
13-APR-10
NUMERIC FUNCTION
MATH FUNCTION:
ABS(45)
---------
45
POWER(10,12)
------------
15IT302J: DATABASE MANAGEMENT SYSTEM Page 39
1.000E+12
SQL> select mod(11,5) from dual;
MOD(11,5)
---------
1
SQL> select exp(10) from dual;
EXP(10)
---------
22026.466
SQL> select sqrt(225) from dual;
SQRT(225)
---------
15
RESULT:
AIM
Sub 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.
Insertion
Table- 2
Insertion
Sql> Insert Into Dept2 Values(107,'Develop','Adyar');
1 Row Created.
Sql> Insert Into Dept2 Values(201,'Debug','Uk');
1 Row Created.
Sql> Insert Into Dept2 Values(200,'Test','Us');
Sql> Insert Into Dept2 Values(201,'Test','Ussr');
1 Row Created.
Sql> Insert Into Dept2 Values(108,'Debug','Adyar');
1 Row Created.
Sql> Insert Into Dept2 Values(109,'Build','Potheri');
Select "Column_Name1"
From "Table_Name1"
Where "Column_Name2" [Comparison Operator]
(Select "Column_Name3"
From "Table_Name2"
Where [Condition])
Ename
----------
Mahesh
Manoj
RESULT:
AIM
To study the various SQL view operations on the database.
Commands Execution
Creation Of Table
--------------------------------
Sql> Create Table Employee (
Employee_Namevarchar2(10),
Employee_Nonumber(8),
Dept_Name Varchar2(10),
Dept_No Number (5),Date_Of_Join Date);
Table Created.
Table Description
-------------------------------
Sql> Desc Employee;
Name Null? Type
------------------------------- -------- ------------------------
Employee_Name Varchar2(10)
Employee_No Number(8)
Dept_Name Varchar2(10)
Dept_No Number(5)
Date_Of_Join Date
Display View:
----------------------
Sql> Select * From Empview;
Employee_N Employee_No Dept_Name Dept_No
---------- ----------- ---------- ----------
Ravi 124 Ece 89
Vijay 345 Cse 21
Raj 98 It 22
Giri 100 Cse 67
View Droped
Create A View With Selected Fields:
AIM
To study the various basic PL/SQL view operations on the database.
SQL> declare
a number;
b number;
c number;
begin
a:=&a;
b:=&b;
c:=a+b;
dbms_output.put_line('sum of'||a||'and'||b||'is'||c);
end;
/
INPUT:
Enter value for a: 23
old 6: a:=&a;
new 6: a:=23;
Enter value for b: 12
old 7: b:=&b;
new 7: b:=12;
OUTPUT:
sum of23and12is35
SQL> DECLARE
<VARIABLE DECLARATION>;
BEGIN
IF(CONDITION)THEN
<EXECUTABLE STATEMENT >;
END;
Coding for If Statement:
DECLARE
b number;
c number;
BEGIN
B:=10;
C:=20;
if(C>B) THEN
dbms_output.put_line('C is maximum');
end if;
OUTPUT:
C is maximum
SQL> DECLARE
<VARIABLE DECLARATION>;
BEGIN
IF (TEST CONDITION) THEN
<STATEMENTS>;
ELSE
<STATEMENTS>;
ENDIF;
END;
******************Less then or Greater Using IF ELSE **********************
SQL> declare
n number;
begin
dbms_output. put_line('enter a number');
n:=&number;
if n<5 then
dbms_output.put_line('entered number is less than 5');
else
dbms_output.put_line('entered number is greater than 5');
end if;
end;
/
Input
Enter value for number: 2
old 5: n:=&number;
new 5: n:=2;
Output:
entered number is less than 5
SQL> DECLARE
<VARIABLE DECLARATION>;
BEGIN
IF (TEST CONDITION) THEN
<STATEMENTS>;
ELSEIF (TEST CONDITION) THEN
<STATEMENTS>;
ELSE
<STATEMENTS>;
ENDIF;
END;
********** GREATEST OF THREE NUMBERS USING IF ELSEIF************
SQL> declare
a number;
b number;
c number;
d number;
begin
a:=&a;
b:=&b;
c:=&b;
if(a>b)and(a>c) then
dbms_output.put_line('A is maximum');
elsif(b>a)and(b>c)then
dbms_output.put_line('B is maximum');
else
dbms_output.put_line('C is maximum');
end if;
end;
/
INPUT:
Enter value for a: 21
old 7: a:=&a;
new 7: a:=21;
Enter value for b: 12
old 8: b:=&b;
new 8: b:=12;
Enter value for b: 45
old 9: c:=&b;
new 9: c:=45;
OUTPUT:
C is maximum
SQL> DECLARE
<VARIABLE DECLARATION>;
BEGIN
LOOP
<STATEMENT>;
END LOOP;
<EXECUTAVLE STATEMENT>;
END;
***********SUMMATION OF ODD NUMBERS USING FOR LOOP***********
SQL> declare
n number;
sum1 number default 0;
endvalue number;
begin
endvalue:=&endvalue;
n:=1;
for n in 1..endvalue
loop
if mod(n,2)=1
then
sum1:=sum1+n;
end if;
end loop;
dbms_output.put_line('sum ='||sum1);
end;
/
INPUT:
Enter value for endvalue: 4
old 6: endvalue:=&endvalue;
new 6: endvalue:=4;
OUTPUT:
sum =4
SQL> DECLARE
<VARIABLE DECLARATION>;
INPUT:
Enter value for endvalue: 4
old 6: endvalue:=&endvalue;
new 6: endvalue:=4;
OUTPUT:
sum of odd no. bt 1 and4is4
PL/SQL procedure successfully completed.
7. TRIGGER
Trigger created.
SQL> update emp set income =900 where empname='kumar';
TABLE IS UPDATED
1 row updated.
SQL> insert into emp values ( 4,'Chandru',700,250,80);
TABLE IS INSERTED
1 row created.
SQL> DELETE FROM EMP WHERE EMPID = 4;
TABLE IS DELETED
1 row deleted.
Create a Trigger to check the age valid or not Using Message Alert:
NAME CHAR(10)
AGE NUMBER(3)
SQL> CREATE TRIGGER DATACHECK
AFTER INSERT OR UPDATE OF AGE ON DATA
FOR EACH ROW
BEGIN
IF(:NEW.AGE<0) THEN
RAISE_APPLICATION_ERROR(-20000,'NO NEGATIVE AGE ALLOWED');
END IF;
END;
/
Trigger created.
SQL> INSERT INTO DATA VALUES('ABC',10);
1 ROW CREATED.
I) PROGRAM:
SQL>create function fnfact(n number)
return number is
b number;
begin
b:=1;
for i in 1..n
loop
b:=b*i;
end loop;
return b;
end;
/
SQL>Declare
n number:=&n;
y number;
begin
y:=fnfact(n);
dbms_output.put_line(y);
end;
/
Function created.
Enter value for n: 5
RESULT:
Thus the pl/sql have been executed successfully.
4. Click add button and select oracle in ORA home 90, click finish
5. A window will appear given the data source home as oracle and select TNS source name
as lion and give the used id as SWTT
7. The above procedure must be follow except the table , A select the table as library
8. Write appropriate Program in form each from created in VB from each from created in
VB form project.
UNIQUE (username) );
CREATE TABLE Librarian( eid INTEGER, ID INTEGER NOT NULL, Pay REAL,
(79916, 'Steven Jensen', '93 Sunny Glen Ln, Garfield Heights, OH 44125',
(93265, 'David Bain', '4356 Pooh Bear Lane, Travelers Rest, SC 29690',
media_id, code) VALUES (8733, 1); INSERT INTO Media( media_id, code)
10.0);
10.0);
INSERT INTO Book(ISBN, title, author, year, dewey, price) VALUES ('978-
INSERT INTO Book(ISBN, title, author, year, dewey, price) VALUES ('978-
'02/15/2007', '03/15/2007');
nvl((SELECT 'Librarian'
FROM Librarian L
FROM Customer C
(SELECT COUNT(*)
FROM Book B
WHERE B.title LIKE '%<user input>%' AND B.author LIKE '%<user input>%' AND
/* Find all copies of a book (used for placing holds or viewing detailed
information). */
nvl((SELECT SI.name
FROM Stored_In SI
(SELECT COUNT(*)
FROM VideoMedia VM
FROM Video V
WHERE V.title LIKE '%<user input>%' AND V.year <= <user input> AND V.year <= <user
input>
AND V.director LIKE '%<user input>%' AND V.rating >= <user input>; /*
Find all copies of a video (used for placing holds or viewing detailed
information). */
FROM Stored_In SI
SELECT S.description
MAX(H.queue)
FROM Hold H
Hold
UNION
UNION
View the total amount of fines the customer has to pay */ SELECT
SUM(CR.fines)
FROM Card CR
/* *\
\* */
INSERT INTO Customer(ID, name, addr, DOB, phone, username, password) VALUES
(<user input>, <user input>, <user input>, <user input>, <user input>, <user input>, <user
input>, );
/* Find a customer */
nvl((SELECT 'Librarian'
FROM Librarian L
FROM Customer C
INSERT INTO Card(num, fines, ID) VALUES ( <user input>, 0, <user input>); /*
UPDATE Media
Find the next Hold entry for a given media */ SELECT H.num, H.name, H.until
FROM Hold H
UPDATE Stored_In
Hold H
WHERE H.queue = 1 AND H.name = <user input> AND H.media_id = <user input> AND
(<user input>, <user input>, <user input>, <user input>, <user input>,
<user input>);
input>, <user input>, <user input>, <user input>, <user input>); /* Add a
<user input>;
<user input>;