0% found this document useful (0 votes)
58 views15 pages

Dbms PRACTICAL 4-5-6-7-8-9

1. The document discusses the creation of tables, constraints, indexes and views in MySQL. It creates tables with various constraints like primary key, foreign key, unique, check and default constraints. It also demonstrates the working of these constraints through insert, update and delete operations. 2. The document also explains clauses like select, from, where, group by and having through examples. It creates views on tables and performs insert, update and delete operations on views. 3. Finally, it creates and demonstrates indexes on columns to improve query performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views15 pages

Dbms PRACTICAL 4-5-6-7-8-9

1. The document discusses the creation of tables, constraints, indexes and views in MySQL. It creates tables with various constraints like primary key, foreign key, unique, check and default constraints. It also demonstrates the working of these constraints through insert, update and delete operations. 2. The document also explains clauses like select, from, where, group by and having through examples. It creates views on tables and performs insert, update and delete operations on views. 3. Finally, it creates and demonstrates indexes on columns to improve query performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

PRACTICAL – 4

AIM: Study and implementation of different types of constraints.

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');

mysql> desc employee;


+-------+--------------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+------------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(30) | NO | | NULL | |
| age | int | YES | | NULL | |
| email | varchar(200) | YES | UNI | NULL | |
| DOB | date | YES | | 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)

mysql> insert into employee values(1,'Arin',16,'arin006@gmail.com','2000-03-


11');
ERROR 1062 (23000): Duplicate entry '1' for key 'employee.PRIMARY'

2] Working of NOT NULL constraints-


mysql> insert into employee(id,age,email,DOB)
values(2,17,'poiu@gmail.com','2001-04-12');
ERROR 1364 (HY000): Field 'name' doesn't have a default value

3] Working of CHECK constraints-


mysql> insert into employee values(1,'Ram',20,'ureijf99@gmail.com','2000-03-
11');
ERROR 3819 (HY000): Check constraint 'employee_chk_1' is violated.

4] Working of UNIQUE constraints-


mysql> insert into employee values(3,'Ria',17,'ureijf99@gmail.com','2000-03-
15');
Query OK, 1 row affected (0.00 sec)
mysql> insert into employee values(4,'Ri',17,'ureijf99@gmail.com','2000-03-
15');
ERROR 1062 (23000): Duplicate entry 'ureijf99@gmail.com' for key
'employee.email'
5] Working of DEFAULT constraints-
mysql> insert into employee(id,name,age,email)
values(6,'Rita',17,'ewfd@gmail.com');
Query OK, 1 row affected (0.00 sec)

6] Working of FOREIGN KEY constraints-


mysql> select * from 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 |
+----+------+------+--------------------+------------+

mysql> select * from department;


+------+------+---------+
| id | d_id | d_name |
+------+------+---------+
| 1 | 101 | Sales |
| 3 | 105 | Finance |
+------+------+---------+

mysql> select employee.name,employee.DOB from employee INNER JOIN


department ON employee.id=department.id;
+------+------------+
| name | DOB |
+------+------------+
| Arun | 2000-03-11 |
| Ria | 2000-03-15 |
+------+------------+
PRACTICAL – 5
AIM: Study and implementation of different types of clauses.

----------------Reference table (emp)----------------


+----+--------+------+---------+
| id | name | dept | salary |
+----+--------+------+---------+
| 1 | Arun | BCA | 20000 |
| 2 | Arin | BBA | 40000.5 |
| 3 | Riya | BBA | 565555 |
| 4 | Ram | MBA | 345555 |
| 5 | Arun | BBA | 400055 |
+----+--------+------+---------+

 Select: is used to fetch data from one or more tables


SYNTAX – select column1, column2……. from table_name;
COMMAND – select id, name from emp;
+----+--------+
| id | name |
+----+--------+
| 1 | Arun |
| 2 | Arin |
| 3 | Riya |
| 4 | Ram |
| 5 | Arun |
+----+--------+

 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 |
+----+------+

 Where: used to filter the results and apply conditions in a SELECT,


INSERT, UPDATE, or DELETE statement.
SYNTAX – select column1, column2…..from table_name where
<condition>;
COMMAND –select name, salary from emp where id=2;
+------+---------+
| name | salary |
+------+---------+
| Arin | 40000.5 |
+------+---------+

 Group by: used to collect data across multiple records and group the
results by one or more columns. Used with aggregate functions.

SYNTAX – select column1, column2.….. , aggregate fun( column) from


table_name group by column_name;
COMMAND –select dept,sum(salary) from emp group by dept;
+------+-------------+
| dept | sum(salary) |
+------+-------------+
| BCA | 20000 |
| BBA | 1005610.5 |
| MBA | 345555 |
+------+-------------+

 Having: works same as ‘where’ clause, i.e provides conditions, but


always used with ‘group by’ clause.
SYNTAX – select column1, column2.….. , aggregate fun( column) from
table_name group by column_name having<condition>;
COMMAND – select name,sum(salary) from emp group by name having
name=’Arun’;
+------+-------------+
| name | sum(salary) |
+------+-------------+
| Arun | 420055 |
+------+-------------+

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)

mysql> select *from em;


+-------+---------+------------+
| em_id | em_name | em_dob |
+-------+---------+------------+
| 2 | Aman | 2001-04-12 |
| 3 | Ria | 2000-03-15 |
+-------+---------+------------+

 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

mysql> select *from em;


+-------+---------+------------+
| em_id | em_name | em_dob |
+-------+---------+------------+
| 2 | Aman | 2001-04-12 |
| 3 | Ria | 2002-10-11 |
| 7 | Raj | 2001-09-10 |
+-------+---------+------------+

 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 |
+-------+---------+------------+

------ Base table (employee) after modifications in its view table-----


+----+------+------+--------------------+------------+
| id | name | age | email | DOB |
+----+------+------+--------------------+------------+
| 1 | Arun | 17 | hurehferu | 2000-03-11 |
| 3 | Ria | 17 | ureijf99@gmail.com | 2002-10-11 |
| 6 | Rita | 17 | ewfd@gmail.com | 2000-01-01 |
| 7 | Raj | NULL | NULL | 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 |
+----+--------+---------+

 CREATING INDEX ON TABLE –

mysql> create index i1 on emp(id,address);


Query OK, 0 rows affected (0.04 sec)

mysql> select * from emp where id=2;


+----+------+---------+
| id | name | address |
+----+------+---------+
| 2 | Arin | mumbai |
+----+------+---------+
1 row in set (0.00 sec)

 CREATING UNIQUE INDEX –

mysql> create unique index i2 on emp(name);


mysql> desc emp;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(20) | YES | UNI | NULL | |
| address | varchar(30) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
mysql> insert into emp values(5,'Arun','mumbai');
ERROR 1062 (23000): Duplicate entry 'Arun' for key 'emp.i2'
 DROPING INDEX –

mysql> alter table emp drop index i2;


Query OK, 0 rows affected (0.02 sec)

mysql> desc emp;


+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| address | varchar(30) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+

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 |
+------+

-----------------Reference tables (products and orders )-----------------


+------+--------+-------+
| p_id | p_name | price |
+------+--------+-------+
| 211 | wart | 200 |
| 111 | xyz | 120 |
| 115 | abc | 320 |
+------+--------+-------+

+------+------+----------+
| 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;
/

 Write a program in PL/SQL to add 2 numbers –

DECLARE
x integer;
y integer;
z integer;
BEGIN
x :=&x;
y :=&y;
z :=x+y;
dbms_output.put_line(‘Sum is ’ ||z);
END;
/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy