Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

LAB EXERCISES

1. Data Definition Language (DDL): IS used to changes the structure of the table like creating a table, deleting a
table, altering a table, etc.
Here are some commands that come under DDL:
1. CREATE - is used to create a new table in a database.
SYNTAX- CREATE TABLE farmer (Fname char (10) primary key, Fage int, Fsubject char (10));
2. ALTER - Once a table is created, Alter includes To Modify, add column(s), drop column(s), and
change column definitions.
- It is also used to establish relationship between tables.
SYNTAX- To add column - ALTER TABLE FARMER add COLUMN address char (10);
SYNTAX- To drop column - ALTER TABLE FARMER DROP COLUMN address;
SYNTAX – to add R/P (foreign key) - ALTER TABLE FARMER ADD FOREIGN KEY (F_SUBJECT)
REFERENCES DEPARTMENT (D_Number);
3. DROP - It is used to delete/remove the created table.
Syntax - To delete table - DROP TABLE FARMER;
2. Data Manipulation Language (DML): are used to manage/modify the database by performing operations
such as inserting, updating, deleting, and navigating through data.
Some commands that come under DML:
1. INSERT- command helps to insert new records to a table.
Syntax 1- INSERT INTO COURSE VALUES (3, ‘Chemistry’, ‘Organic chemistry’,‘105’, ‘NS1421’);
Syntax 2- INSERT INTO DEPARTMENT (D_Number, D_Name, D_Location) VALUES (‘D101’, ‘Biology’, ‘NBR
First Floor’)
2. UPDATE- It simply Updates/MODIFY existing records in the table.
Syntax - UPDATE Teacher SET T_Salary = 15000 WHERE T_Salary<10000;
- UPDATE Teacher SET T_Salary = 20000 WHERE T_sex=’female’;
3. DELETE- command is used to delete a record or multiple records from the database.
- Delete command does not remove the table structure, rather it only deletes the data.
To delete 1 record - DELETE FROM Teacher WHERE T_IDNO =’Teach/2312/91’;
To delete all records - DELETE FROM teacher; or DELETE * FROM teacher;
3. Data Query Language/DQL – SELECT Command - is used to fetch/filter the data from the database and is used
for querying or selecting all or subsets of data from a database.
Syntax - SELECT S_Fname, S_MName, S_LName, S_sex
FROM STUDENT
WHERE s_sex = ‘male’;
Syntax - Asce and Descen order - SELECT T_First_Name, T_Middle_Name, T_Sex, T_Salary
FROM Teacher
ORDER BY T_Salary Desc/ASC
ACTIVITY 1
I. DDL

1. Create the following three tables using SQL commands Access:


A. Student table with attribute – student id (CHAR(10)), name (CHAR(20)), sex(CHAR(1)),
age(INTEGER), grade level (INTEGER). Set the Student id as primary key.
B. Teacher table with attribute – teacher id (CHAR(10)), name (CHAR(20)), sex(CHAR(1)),
age(INTEGER), specialization (CHAR(15)). Set the Teacher id as primary key.
C. Grade table with attribute – student id (CHAR(10)), teacher id (CHAR(10), course
code(CHAR(20)), mark(INTEGER). Use course code as a FOREIGN KEY to create a
relationship with the COURSE table created in Figure 3.9.
D. COURSE table – Contact_hr (int), c_name (text(20)), C_description (text), C_department
(char(10)), C_code (char(10)) primary key
2. Run the SQL command and see the resulting tables created (student, teacher, and grade).
3. Add a column called Address with char data type of size 15 in student table.
4. Remove the column Address from student table.
5. DROP student table.
II. DML

1. Insert the following records in the Student, Teacher and Grade tables.
- Teacher table (001, Abebe, M, 31, chemistry),(002, HANA, F,22,ICT) ,(003, Gebre, M, 45,
Geography).
- Grade Table(10/16, 001,NS1,80), (11/16,002,NS2,90),(12/16,003,NS3,100).
- Course table (1, ‘Chemistry’, ‘Organic chemistry’, ‘101’, ‘NS1’),(2, ICT, SQL, 102, NS2), (1,
PHYSICS, MOTION, 103, NS3).
2. UPDATE/change the teacher name Abebe to Danial in name column.
3. Change the Grade table mark 80 to 55 in mark column.
4. Delete Hana from Teacher table.
5. Delete all records from teacher table.
III. DQL
5. Select all students whose marks are above 80 from grade table. [Hint: marks>80]
6. Select all courses and order them by their names.

Prepared by: B&M

You might also like