Dbms PRACTICAL 4-5-6-7-8-9
Dbms PRACTICAL 4-5-6-7-8-9
TABLE – 1 (employee)
mysql> create table employee(id int Primary key,name varchar
(30) NOT NULL,age int check(age<=18),email varchar(200) UNIQ
UE,DOB DATE DEFAULT '2000-01-01');
TABLE – 2 (department)
mysql> create table department(id int ,d_id int,d_name varch
ar(30),FOREIGN KEY(id) REFERENCES employee(id));
mysql> desc department;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int | YES | MUL | NULL | |
| d_id | int | YES | | NULL | |
| d_name | varchar(30) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
1] Working of PRIMARY KEY constraints-
mysql> insert into employee values(1,'Arun',17,'hurehferu','2000-03-11');
Query OK, 1 row affected (0.05 sec)
From: names one or more tables from which data values are retrieved
by the query.
SYNTAX – select column1, column2 ……from table_name;
COMMAND – select id, dept from emp;
+----+------+
| id | dept |
+----+------+
| 1 | BCA |
| 2 | BBA |
| 3 | BBA |
| 4 | MBA |
| 5 | BBA |
+----+------+
Group by: used to collect data across multiple records and group the
results by one or more columns. Used with aggregate functions.
PRACTICAL – 6
AIM: Study and implementation of views.
mysql> create table employee(id int Primary key,name varchar
(30) NOT NULL,age int check(age<=18),email varchar(200) UNIQ
UE,DOB DATE DEFAULT '2000-01-01');
-------------------Reference Base table (employee)------------------
+----+------+------+--------------------+------------+
| id | name | age | email | DOB |
+----+------+------+--------------------+------------+
| 1 | Arun | 17 | hurehferu | 2000-03-11 |
| 2 | Aman | 17 | poiu@gmail.com | 2001-04-12 |
| 3 | Ria | 17 | ureijf99@gmail.com | 2000-03-15 |
| 6 | Rita | 17 | ewfd@gmail.com | 2000-01-01 |
+----+------+------+--------------------+------------+
CREATING VIEW -
mysql> create view em(em_id,em_name,em_dob) as select id,nam
e,DOB from employee where DOB>'2000-03-13';
Query OK, 0 rows affected (0.02 sec)
INSERT -
mysql> insert into em values(7,'Raj','2001-09-10');
Query OK, 1 row affected (0.01 sec)
mysql> select *from em;
+-------+---------+------------+
| em_id | em_name | em_dob |
+-------+---------+------------+
| 2 | Aman | 2001-04-12 |
| 3 | Ria | 2000-03-15 |
| 7 | Raj | 2001-09-10 |
+-------+---------+------------+
UPDATE -
mysql> update em set DOB='2002-10-11' where em_id=3;
ERROR 1054 (42S22): Unknown column 'DOB' in 'field list'
mysql> update em set em_dob='2002-10-11' where em_id=3;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
DELETE -
mysql> delete from em where em_id=2;
Query OK, 1 row affected (0.00 sec)
mysql> select *from em;
+-------+---------+------------+
| em_id | em_name | em_dob |
+-------+---------+------------+
| 3 | Ria | 2002-10-11 |
| 7 | Raj | 2001-09-10 |
+-------+---------+------------+
PRACTICAL – 7
AIM: Study and implementation of index.
------Reference table (emp)-----
+----+--------+---------+
| id | name | address |
+----+--------+---------+
| 1 | Arun | delhi |
| 2 | Arin | mumbai |
| 3 | Riya | panji |
| 4 | Ram | delhi |
+----+--------+---------+
PRACTICAL – 8
AIM: Study and implementation of sub-queries.
-------------------Reference table 1 (empl)--------------------
+------+-------+------+-----------+--------+
| id | name | age | dept | salary |
+------+-------+------+-----------+--------+
| 101 | Ram | 29 | Sales | 40000 |
| 102 | Kiran | 26 | Finance | 30000 |
| 103 | Tiya | 25 | Finance | 45000 |
| 104 | Shiv | 25 | Marketing | 50000 |
+------+-------+------+-----------+--------+
EXAMPLE 1]
mysql> select *from empl where salary >(select salary from empl where
id=101);
+------+------+------+-----------+--------+
| id | name | age | dept | salary |
+------+------+------+-----------+--------+
| 103 | Tiya | 25 | Finance | 45000 |
| 104 | Shiv | 25 | Marketing | 50000 |
+------+------+------+-----------+--------+
EXAMPLE 2]
mysql> select name,dept,salary from empl where salary>(selec
t avg(salary) from empl);
+------+-----------+--------+
| name | dept | salary |
+------+-----------+--------+
| Tiya | Finance | 45000 |
| Shiv | Marketing | 50000 |
+------+-----------+--------+
EXAMPLE 3]
mysql> select id from empl where id IN (select id from empl where
salary>30000);
+------+
| id |
+------+
| 101 |
| 103 |
| 104 |
+------+
+------+------+----------+
| o_id | p_id | quantity |
+------+------+----------+
| 1005 | 115 | 10 |
| 1012 | 211 | 5|
| 1092 | 111 | 23 |
+------+------+----------+
EXAMPLE 4]
mysql> select p_name from products where p_id=ANY(select p_id from orders
where quantity=5);
+--------+
| p_name |
+--------+
| wart |
+--------+
EXAMPLE 5]
mysql> select p_name,price from products where p_id=ALL(select p_id from
orders where quantity=23);
+--------+-------+
| p_name | price |
+--------+-------+
| xyz | 120 |
+--------+-------+
PRACTICAL – 9
AIM: Study and implementation of PL/SQL.
Simple program to print text using PL/SQL –
DECLARE
message varchar2(20) := ’hello,world!’;
BEGIN
dbms_output.put_line(message);
END;
/
DECLARE
x integer;
y integer;
z integer;
BEGIN
x :=&x;
y :=&y;
z :=x+y;
dbms_output.put_line(‘Sum is ’ ||z);
END;
/