Sei sulla pagina 1di 9

1.

SQL> SELECT * FROM emp WHERE ename LIKE 'S%';


2. SQL> SELECT * FROM emp WHERE ename LIKE '_L%';
3. SQL> SELECT * FROM emp WHERE ename LIKE '%E_';
4. SQL> SELECT * FROM emp WHERE ename LIKE '____';
5. SQL> SELECT * FROM emp WHERE ename LIKE '%L%';
6. SQL> SELECT * FROM emp WHERE sal BETWEEN 2000 and
3000;
7. SQL> SELECT * from emp WHERE job = 'MANAGER';
8. SQL> SELECT * from emp WHERE job = 'MANAGER' and
deptno in (10, 20);
9. SQL> SELECT * FROM emp WHERE job IN ('CLERK','ANALYST')
AND sal >= 1000 AND deptno IN (20,30);
10. SQL> SELECT * FROM emp where deptno in (20,30) and comm
IS NULL;
11. SQL> SELECT * FROM emp WHERE ename LIKE ('A%') OR
ename LIKE ('S%');
12. SQL>SELECT * FROM emp WHERE ename NOT LIKE ('%S')
AND deptno IN (20,30);
13. SQL> SELECT * FROM emp WHERE job = 'MANAGER' OR job =
'CLERK' AND sal >=2000 AND deptno NOT IN (10,20);
14. SQL> SELECT * from emp WHERE sal NOT BETWEEN 2000
AND 3000 AND job LIKE ('%MAN%');

Program no 1:Create an Insurance Database to keep track of car-


owner and accidents with the given tables, by properly specifying
the Primary and Foreign keys. Also execute the given Queries.
Consider the Insurance database given below. The primary keys are
underlined and the datatypes are
specified.
1. PERSON(driver-id#: string, name: string, address: string)
2. CAR(Regno: string, model: string, year: int)
3. ACCIDENT(report-number: int, date: date, location: string)
4. OWNS(driver-id#: string, Regno: string)
5. PARTICIPATED(driver-id#: string, Regno: string, report-
number: int, damage amount: int)

1. Create the tables by properly specifying the primary keys and


the foreign keys.
2. Enter atleast five tuples for each relation.
3. Demonstrate how you
a) Update the damage amount for the car with a specific
Regno in the accident with report number 12 to 25000.
b) Add a new accident to database.
4. Find the total number of the people who owned the cas that
were involved in accidents in 2002.
5. Find the number of accidents in which cars belonging to a
specific model were involved.
SOLUTION:
(I) Create the tables by properly specifying the primary keys and the
foreign keys.
mysql> show databases;
+--------------------+
| Database
|
+--------------------+
| information_schema |
| bang1 |
| mysql |
| test
|
+--------------------+
34 rows in set (0.08 sec)mysql> create database insurance;
mysql> use insurance;
Database changed
mysql> create table person(driverid varchar(10) primary key,name
varchar(20) not null,address
varchar(25) not null);
mysql> create table car(regno varchar(10) primary key,model
varchar(20) not null ,year int not
null);
mysql> create table accident(reportno int primary key,date date not
null,loaction varchar(20) not
null);
mysql> create table owns(driverid varchar(10),regno
varchar(10),primary
key(driverid,regno),foreign key(driverid) references
person(driverid),foreign key(regno)
references car(regno));
mysql> create table participated(driverid varchar(10),regno
varchar(10),reportno int,primary
key(driverid,regno,reportno),damageamount int not null,foreign
key(driverid) references
person(driverid),foreign key(regno) references car(regno), foreign
key(reportno) references
accident(reportno));
mysql> show tables;
+---------------------+
| Tables_in_insurance |
+---------------------+
| accident
| car
| owns
|
|
|
| participated
| person
|
|
+---------------------+
5 rows in set (0.00 sec)
mysql> desc accident;+----------+-------------+------+-----+---------+-------
+
| Field
| Type
| Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| reportno | int(11)
| date
| date
| NO | PRI | NULL
| NO |
| NULL
| loaction | varchar(20) | NO |
|
|
|
|
| NULL
|
|
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.05 sec)
mysql> desc car;
+-------+-------------+------+-----+---------+-------+
| Field | Type
| Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| regno | varchar(10) | NO | PRI | NULL
| model | varchar(20) | NO |
| year | int(11)
| NO |
|
| NULL
|
|
| NULL |
|
|
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.02 sec)
mysql> desc owns;
+----------+-------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| driverid | varchar(10) | NO | PRI | | |
| regno | |
| varchar(10) | NO | PRI |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)mysql> desc person;
+----------+-------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| driverid | varchar(10) | NO | PRI | NULL
| name
|
|
| varchar(20) | NO | | NULL | |
| address | varchar(25) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.02 sec)
mysql> desc participated;
+--------------+-------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| driverid | varchar(10) | NO | PRI | | |
| regno | varchar(10) | NO | PRI | | |
| reportno
| int(11)
| NO | PRI | 0
| damageamount | int(11)
| NO |
|
| NULL
|
|
|
+--------------+-------------+------+-----+---------+-------+
4 rows in set (0.02 sec)
(II) Enter atleast five tuples for each relation.
mysql> insert into person values('d001','ria','rajajinagr');
mysql> insert into person values('d002','pia','h.s.r layout');
mysql> insert into person values('d003','tia','n.g.f layout');
mysql> insert into person values('d004','jia','r.r layout');
mysql> insert into person values('d005','kia','jaynagar');mysql>
select *from person;
+----------+------+--------------+
| driverid | name | address
|
+----------+------+--------------+
| d001 | ria | rajajinagr |
| d002 | pia | h.s.r layout |
| d003 | tia | n.g.f layout |
| d004 | jia | r.r layout |
| d005 | kia | jaynagar
|
+----------+------+--------------+
5 rows in set (0.00 sec)
mysql> insert into car values('ka001','ritz',1992);
mysql> insert into car values('ka002','swift',2000);
mysql> insert into car values('ka003','nano',2010);
mysql> insert into car values('ka004','reva',1999);
mysql> insert into car values('ka005','manza',2011);
mysql> select *from car;
+-------+-------+------+
| regno | model | year |
+-------+-------+------+
| ka001 | ritz | 1992 |
| ka002 | swift | 2000 |
| ka003 | nano | 2010 |
| ka004 | reva | 1999 |
| ka005 | manza | 2011 |+-------+-------+------+
5 rows in set (0.00 sec)
mysql> insert into accident values(01,'2000-12-25','blore');
mysql> insert into accident values(02,'1999-08-3','tumkur');
mysql> insert into accident values(03,'2003-10-24','mlore');
mysql> insert into accident values(04,'2010-05-5','mysore');
mysql> insert into accident values(05,'2012-07-20','goa');
mysql> select *from accident;
+----------+------------+----------+
| reportno | date
| loaction |
+----------+------------+----------+
| 1 | 2000-12-25 | blore
|
| 2 | 1999-08-03 | tumkur |
| 3 | 2003-10-24 | mlore
| 4 | 2010-05-05 | mysore |
| 5 | 2012-07-20 | goa
|
|
+----------+------------+----------+
5 rows in set (0.00 sec)
mysql> insert into owns values('d001','ka001');
mysql> insert into owns values('d002','ka001');
mysql> insert into owns values('d003','ka003');
mysql> insert into owns values('d004','ka003');
mysql> insert into owns values('d005','ka004');
mysql> select *from owns;+----------+-------+
| driverid | regno |
+----------+-------+
| d001 | ka001 |
| d002 | ka001 |
| d003 | ka003 |
| d004 | ka003 |
| d005 | ka004 |
+----------+-------+
5 rows in set (0.00 sec)
mysql> insert into participated values('d001','ka001',1,2000);
mysql> insert into participated values('d002','ka002',2,4000);
mysql> insert into participated values('d003','ka003',3,6000);
mysql> insert into participated values('d004','ka004',4,8000);
mysql> insert into participated values('d005','ka005',5,10000);
mysql> select * from participated;
+----------+-------+----------+--------------+
| driverid | regno | reportno | damageamount |
+----------+-------+----------+--------------+
| d001 | ka001 | 1 | 2000 |
| d002 | ka002 | 2 | 4000 |
| d003 | ka003 | 3 | 6000 |
| d004 | ka004 | 4 | 8000 |
| d005 | ka005 | 5 | 10000 |
+----------+-------+----------+--------------+5 rows in set (0.01 sec)
mysql> select * from accident;
+----------+------------+----------+
| reportno | date
| loaction |
+----------+------------+----------+
| 1 | 2000-12-25 | blore
|
| 2 | 1999-08-03 | tumkur |
| 3 | 2003-10-24 | mlore
| 4 | 2010-05-05 | mysore |
| 5 | 2012-07-20 | goa
|
|
+----------+------------+----------+
5 rows in set (0.00 sec)
mysql> select * from car;
+-------+-------+------+
| regno | model | year |
+-------+-------+------+
| ka001 | ritz | 1992 |
| ka002 | swift | 2000 |
| ka003 | nano | 2010 |
| ka004 | reva | 1999 |
| ka005 | manza | 2011 |
+-------+-------+------+
5 rows in set (0.00 sec)
mysql> select * from owns;
+----------+-------+| driverid | regno |
+----------+-------+
| d001 | ka001 |
| d002 | ka001 |
| d003 | ka003 |
| d004 | ka003 |
| d005 | ka004 |
+----------+-------+
5 rows in set (0.01 sec)
mysql> select * from person;
+----------+------+--------------+
| driverid | name | address
|
+----------+------+--------------+
| d001 | ria | rajajinagr |
| d002 | pia | h.s.r layout |
| d003 | tia | n.g.f layout |
| d004 | jia | r.r layout |
| d005 | kia | jaynagar
|
+----------+------+--------------+
5 rows in set (0.00 sec)
mysql> select * from participated;
+----------+-------+----------+--------------+
| driverid | regno | reportno | damageamount |
+----------+-------+----------+--------------+
| d001
| ka001 |
1|
2000 || d002 | ka002 | 2 | 4000 |
| d003 | ka003 | 3 | 6000 |
| d004 | ka004 | 4 | 8000 |
| d005 | ka005 | 5 | 10000 |
+----------+-------+----------+--------------+
5 rows in set (0.00 sec)
III) Demonstrate how you
a) Update the damage amount for the car with a specific Regno in
the accident with
report number 12 to 25000.
mysql> update participated set damageamount = 25000 where
regno = 'ka005'and rep
ortno=5;
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from participated;
+----------+-------+----------+--------------+
| driverid | regno | reportno | damageamount |
+----------+-------+----------+--------------+
| d001 | ka001 |
1|
2000 |
| d002 | ka002 |
2|
4000 |
| d003 | ka003 |
3|
6000 |
| d004 | ka004 |
4|
8000 |
| d005 | ka005 |
5|
25000 |
+----------+-------+----------+--------------+
5 rows in set (0.00 sec)
b) Add a new accident to database.
mysql> insert into accident values(06,'2013-08-20','bidar');
mysql> select *from accident;
+----------+------------+----------+
| reportno | date
| loaction |
+----------+------------+----------+
|
1 | 2000-12-25 | blore |
|
2 | 1999-08-03 | tumkur |
|
3 | 2003-10-24 | mlore |
|
4 | 2010-05-05 | mysore |
|
5 | 2012-07-20 | goa ||
6 | 2013-08-20 | bidar |
+----------+------------+----------+
6 rows in set (0.00 sec)
IV) Find the total number of the people who owned the cars that
were involved in accidents in 2002.
mysql> select count(distinct p.driverid) from participated p,
accident a where p
.reportno=a.reportno and 'yyyy'=2002;
+----------------------------+
| count(distinct p.driverid) |
+----------------------------+
|
0|
+----------------------------+
1 row in set, 1 warning (0.00 sec)
V). Find the number of accidents in which cars belonging to a
specific model were involved.
mysql> select count(*) from participated p, car c where
p.regno=c.regno and c.mo
del='swift';
+----------+
| count(*) |
+----------+
|
1|
+----------+
1 row in set (0.00 sec)