Professional Documents
Culture Documents
Labsheet 6
Labsheet 6
IS222 – DATABASE
CONCEPTS AND DESIGN
Lab sheet
IS222
Table of Contents
LAB 6: Basics of Structured Query Language (SQL).............................................................2
Objectives:...................................................................................................................................2
Tools/Software:...........................................................................................................................2
Concepts & Descriptions:.............................................................................................................2
Structured Query Language............................................................................................................................2
Types of SQL...................................................................................................................................................2
Oracle Datatypes............................................................................................................................................2
This practice includes two activities.............................................................................................3
Try it/ Solve it:..............................................................................................................................4
Lab Activities..............................................................................................................................10
Deliverables:..............................................................................................................................12
LAB 6: Basics of Structured Query Language (SQL)
Objectives:
This lesson covers the following objectives:
Create, Alter, Drop and Describe tables using SQL Command.
Create rows by using INSERT statement.
Read rows by using simple SELECT statement.
Update rows by using SQL Command.
Delete rows by using SQL Command.
Tools/Software:
To accomplish this session, students should have access to APEX.
Datatype Explanation
VARCHAR2, VARCHAR They store a variable length of characters between 1 and 4000
bytes. Using the same example above but with Name column
defined as VARCHAR2(20), “Ali” will be only of length 3.
The only difference between VARCHAR and VARCHAR2 is how
they see NULL and empty string. The first one treats them
differently while the latter as the same.
It is suggested that we use VARCHAR2.
NCHAR, NVARCHAR2 They store Unicode characters. NCHAR can hold 2000 bytes
while NVARCHAR2 can extend up to 4000 bytes.
Numeric Datatypes
Datatype Explanation
DATE datatype stores both date and time values. It specifically stores the year, month, day,
hours, minutes, and seconds. Most of the time, we use the TO_DATE function to store a
date value. Look at the examples below.
Example Explanation
TO_DATE(‘March 18, 1990’, ‘MONTH DD, YYYY’) Stores the date with all zeroes for
hours, minutes and seconds
Practice Activity 1: Create, Describe, Update and Drop table using SQL command.
Task1: Create two tables using SQL command. First table is called students and second table is
called teachers with the structure below: -
Column Definition
PK Student_ID Number (Not Null)
Student_Name varchar2(30)
Student_Age Number
Column Definition
PK Teacher_ID Number (Not Null)
Teacher_Name varchar2(30)
Teacher_Nationality varchar2(30)
Example Explanation
Create Table Students A table named students was created
( with 3 columns and Student_id as
Student_ID Number not null, primary key (constraint or rule) and
Student_Name varchar2(30), therefore cannot be NULL.
Student_Age Number,
Primary key (Student_ID)
);
Create Table Teachers A table named Teachers was created
( with 3 columns and Teacher_id as
Teacher_ID number not null, primary key (constraint or rule) and
Teacher_Name varchar2(30), therefore cannot be NULL.
Teacher_Age Number,
Primary key (Teacher_ID)
);
Go to the object Browser and search about students and teachers table
Task2: Drop Teachers table and Describe Students table using SQL command.
Example Explanation
Drop Table Teachers; It will delete a table named Teachers.
2- Describe Student table.
1- DESCRIBE <table_name>;
2- DESC <table_name>;
Example Explanation
DESC Table Students; It will display the structure of a table
named Students.
Example Explanation
Alter Table Students DROP COLUMN Student_Age; A table name Student was altered by
dropping one column.
Example Explanation
Alter Table Students A table named students was altered by
Add adding one column.
(
Student_Phone_No varchar2(30)
);
3- Modify Student_Name Column as Varchar2(30) not null.
Example Explanation
Alter Table Students A table named students was altered by
Modify modifying the definitions of its existing
( column.
Student_Name varchar2(30) not null
);
Example Explanation
Alter Table Students Rename Column A table named students was altered by
Student_Name to Student_FullName; changing the name of its existing
column.
Practice Activity 2: Insert, Update, Delete, and Select all records from student table
using SQL command.
Note:
- First syntax
o Used to specify which columns that will be add values to them into table.
o The values for each column are listed in the same order.
o The number values are not enclosed in single quotation marks.
- Second syntax
o used to add values for all columns into table.
o One precaution: the values for each column must match exactly the
default order in which they appear in the table (as shown in a DESCRIBE
statement), and a value must be provided for each column.
Example Explanation
insert into students One row was added to students table with
(student_id, student_fullname) values supplied values only for 2 columns:
(102030,'Ahmed Abdullah') student_id and student_fullname
insert into students values (102040,'Khaled One row was added to students table with
Abdullah','0560333040') supplied values for all its columns:
student_id, student_fullname, and
studebt_phone_no. When using this syntax,
make sure that the sequence of values match
the sequence of column declaration.
Go to the object Browser and search about students table then you can check
your added table data.
Example Explanation
Select * from students; It displays all rows from students table
Task3: Change the name of student to Ali Abdullah for a student whose student_id is "102040".
UPDATE Syntax: -
UPDATE <table_name> SET <column> = <value> WHERE <condition> ;
Example Explanation
update students One row was updated by changing the
set student_fullname= 'Ali Abdullah' where fullname to ‘Ali Abdullah’ for a student
student_id = 102040. whose student_id is "102040".
DELETE Syntax: -
DELETE FROM <table_name> WHERE <condition> ;
Example Explanation
delete from students where student_id= 102030 One row was deleted, the records whose
student_ID is ‘102030.
Lab Activities
1. Using SQL statement, create table COURSES with the structure below:
Column Definition
PK course_id varchar2(10) not null
course_title varchar2(30)
unit number
3. Describe table COURSES. Is the output the same as the one below? If it does then
you got it!
4. Go back in “SQL Commands” and select all records from table COURSES. How many
records are there?
6. Select all records from table COURSES. How many records are there?
8. Select all records from table COURSES. Is the output the same as the one below? If it
does then you got it!
Deliverables:
At the end of this session, students are expected to:
Apply all the practice activities above.
Solve the lab activities and submit their work.