advanced database lab manual (2)
advanced database lab manual (2)
i. Operating on Database
a. Create database
Syntax:-CREATE DATABASE database_name;
Example:- CREATE DATABASE tmp;
b. Delete database
DROP DATABASE database_name;
Example: drop database tmp;
c. Use database
USE DATABASE database_name; Or USE DatabaseName;
Example:- use tmp;
ii. Table/relation Operation
a. Create table
Syntax:- CREATE TABLE tablename(fields and their types);
i.e CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns ) );
Example:- CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESSS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID) );
To insert values to the table we use the syntax as
INSERT INTO table_name( column1, column2....columnN) VALUES
( value1, value2....valueN);
Example:- insert into CUSTOMERS VALUES(1,'almaz',21,'kebele03',5000);
insert into CUSTOMERS VALUES(2,'hana',21,'kebele04',6000);
c. Delete table
1 2024-01-15 00:00:00.000 1 4
2 2022-01-15 00:00:00.000 2 5
Table 2 ORDERS TABLE DATA
A. Relational Algebras
1. Selection
Syntax:-SELECT column1, column2, columnN FROM table_name;
Or
SELECT column1, column2, columnN FROM table_name WHERE
[condition]
Or
SELECT column1, column2, columnN FROM table_name WHERE
[condition1] AND [condition2]...AND [conditionN];
Or
Database System By Abebe B SELECT column1, column2, columnN
FROM table_name WHERE [condition1] OR [condition2]...OR
[conditionN]
Example:- SELECT ID, NAME, SALARY FROM CUSTOMERS;
2. Projection
Projection --> for selecting the columns of table; and
Selection ---> to select the rows of table?
To project we use select statement.
Example ;-select NAME FROM CUSTOMERS
NAME
Almaz
Hana
Eyob
Nati
Sami
Kal
Nahom
Sara
Abriham
Yisak
3. Cartesian product
The CARTESIAN JOIN or CROSS JOIN returns the Cartesian product
of the sets of records from the two or more joined tables. Thus, it equates
to an inner join where the join-condition always evaluates to True or
where the join-condition is absent from the statement.
Syntax:The basic syntax of INNER JOIN is as follows:
SELECT table1.column1, table2.column2... FROM table1, table2 [,
table3 ]
SQL>SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS,
ORDERS;
4. Join
The SQL Joins clause is used to combine records from two or more
tables in a database. A JOIN is a means for combining fields from two
tables by using values common to each. Consider the above two tables,
i. INNER JOIN
The most frequently used and important of the joins is the INNER JOIN.
They are also referred to as an EQUIJOIN. The INNER JOIN creates a
new result table by combining column values of two tables (table1 and
table2) based upon the join-predicate. The query compares each row of
table1 with each row of table2 to find all pairs of rows which satisfy the
join-predicate. When the join-predicate is satisfied, column values for
each matched pair of rows of A and B are combined into a result row.
Or
Or
Select column-name-list from table-name1 inner join table-name2 where
column-name.table1=column-name.table2
Query optimization
Activity of choosing an efficient execution strategy for processing query.
• As there are many equivalent transformations of same high-level query, aim
of QO is to choose one that minimizes resource usage.
• Generally, reduce total execution time of query.
To rename a user, you use the ALTER USER ... WITH NAME statement:
To map the user with another login account, you use following ALTER USER ...
WITH LOGIN statement:
Table 2: PATIENT
Field Name Type Size Constraint
Patient record No Varchar 50 Primary key
P name Varchar 50 ----
P sex Varchar 6 Male or female
P age Int --- Not grater than 150
Table 3: APPOINTMENT
3. Change the column named “Dname” as type varchar size 50 from table
DOCTOR.
4. Add sample records in to the table as shown below
D id D name D sex Specialization D age Display
XYZ 123 Mikias Male Surgeon 40 11000
XYZ 124 Eden Female Haomatologist 45 18000
Xyz 125 Helen Female Internal 35 8000
medicine
specialists
XYZ 126 Bekalu Male Dermatologist 38 9000
XYZ 127 Surfel Male Dermatologist 55 9000
XYZ 128 Birhanu Male Surgeon 40 18000
Table 1. Lists of Doctors
Patien Record No P name P sex P age
P XyZ 111 Lemlem Female 12
PXyz 112 Hana Female 45`
PxyZ113 Belay Male 22
PXyz114 Woldu Male 91
Pxyz115 Teshome Male 55
Pxyz115 Zeru Male 1
--b answer
select min(Dsalary)as'min salary',max(Dsalary)as'max salary',sum(Dsalary)as'sum
salary',avg(Dsalary)as'avg salary' from Doctor
--c answer
select count(*)from Doctor
--d answer
select Pname from Patient,Appointment where Patient.Prno=Appointment.Prno
and Adate='2014-02-21'
--e answer
select Dname from Doctor where Specialization='Haomatologist'and Dage>40
-- f answer
backup database xyz_clinic to disk='E:/xyzclinc'
-- g answer first drop database
drop database xyz_clinic
restore database xyz_clinic from disk='E:/xyzclinc'
-- i answer
create login login1 with password='1234'
create login login2 with password='1234'
create login login3 with password='1234'
--j answer
create user user1b for login login1
create user user2 for login login2
create user user3 for login login3
-- k answer
create role ICT
exec sp_addrolemember ICT,user1b
exec sp_addrolemember ICT,user2
exec sp_addrolemember ICT,user3
--l answer
grant select,update,delete to user1b with grant option
grant insert to user1b
--m answer
revoke insert from user1b
-- n answer
create table stu
(
sid int primary key,
sname varchar(30),
lname varchar(30),
sage int
);
--TCL Command on insert operation
begin transaction
insert into stu values(2,'alemu','kebe',27);
commit transaction
--TCL command on update operation
begin transaction ;
update stu set sname='ebe' where sid=2;
rollback transaction
--TCL commad on delete operation
begin transaction
delete from stu where sid=2;
rollback transaction
alter table student add tid int foreign key references teacher(tid);
create table teacher(
tid int primary key not null,
tname varchar(20),
tsex varchar check(tsex in('f','m')),
tsalary money check (tsalary between 3000 and 6000)
);
select * from teacher
-- moeny data type can be considered as character or integer
insert into teacher values (1,'hibist','f',5000);
insert into teacher values (2,'amex','m','5000');
--left join
select coustmer.cname,amount from coustmer left join orders on
coustmer.cid=orders.cid;
select coustmer.cname,amount from coustmer right join orders on
coustmer.cid=orders.cid
begin transaction
delete from student where sname='almaz';
rollback transaction
create login login11 with password='123abc';
create user user1 for login login11
create role student
exec sp_addrolemember student,user1
grant select,insert on student to user1
revoke insert on student from user1
create login login22 with password ='345abcc'
create user user22 for login login22
grant insert,delete on student to user1 with grant option
BACKUP DATABASE dmu to disk='E:\BBb'