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

Kingdom of Saudi Arabia

Ministry of Higher Education


King Faisal University
College of Computer Sciences &
Information Technology

IS222 – DATABASE
CONCEPTS AND DESIGN
Lab sheet

Lab 6: Basics of Structured

Query Language (SQL)

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.

Concepts & Descriptions:


Structured Query Language
Structured Query Language (SQL) is a programming language designed to manage or
communicate with a database.
Types of SQL
Types of SQL include:

 Data Definition Language (DDL) is used to define the structure of database


(Create Table, Alter Table, Describe Table, and Drop Table).

 Data Manipulation Language (DML) is used to manipulate date of database by


using command (Create row by using” INSERT”, Read a row by using “SELECT”,
Update a row by using “Update”, and Delete row by using “Delete”).
Oracle Datatypes
Built-in datatypes in Oracle can be divided into six categories: character, numeric,
DATE, LOB, RAW and LONG RAW, and ROWID and UROWID. We will concentrate only on the
first three. Please refer to the tables below.
Character Datatypes

Datatype Explanation

CHAR It stores a fixed length of characters between 1 and 2000 bytes.


For example if your column Name was defined as CHAR(20) a
value “Ali” will still be of length 20 with 17 padded spaces.

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

NUMBER It stores fixed and floating-point numbers up to 38 digits of


precision. You may optionally define its precision (total no. of
digits) and scale (no. of digits on the right). For example Grade
column can be defined as simply NUMBER or NUMBER(4,2).

BINARY_FLOAT It stores a 32-bit, single-precision floating-point number


datatype.

BINARY_DOUBLE It stores a 64-bit, double-precision floating-point number


datatype.

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

TO_DATE(’18-MAR-90 12:45 A.M.’, Stores a complete set of date and


time.
‘DD-MON-YY HH:MI A.M.’)

This practice includes two activities.


1. Create, Describe, Update and Drop Table .
In this activity, you create two tables called students and teachers after that drop teachers
table and then update and describe student table.
2. Insert, Update, Delete, and Select all records from student table that created in
previous activity.

In this activity, there are several steps: -


 Step 1: Insert two rows in students table.
 Step 2: Update first row in students table.
 Step3: Delete second row in students table.
 Step 4: select all records from students table.

Try it/ Solve it:

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

Table 1: Students Table

Column Definition
PK Student_ID Number (Not Null)
Student_Name varchar2(30)
Student_Age Number

Table 2: Teachers Table

Column Definition
PK Teacher_ID Number (Not Null)
Teacher_Name varchar2(30)
Teacher_Nationality varchar2(30)

1) Click on SQL Workshop and then choose SQL Commands.


2) Write SQL statement after that click run button to execute the statement.

Create table syntax: -

CREATE TABLE <table_name> ( <column1> <definition>, <column2> <definition>,


<columnN> <definition>, <constraint> );

Table 3: Create Students table and Teachers table

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.

1- Drop Teacher table.

Drop table syntax: -

DROP TABLE <table_name>;

Table 4: Drop Teachers table

Example Explanation
Drop Table Teachers; It will delete a table named Teachers.
2- Describe Student table.

Describe table syntax: -

There are two ways to describe as the following bellow: -

1- DESCRIBE <table_name>;

2- DESC <table_name>;

Example Explanation
DESC Table Students; It will display the structure of a table
named Students.

Task3: Alter Students table as the following bellow: -

1- Drop Student_Age Column.

2-Add one column is called Student_Phone_No as Varchar2(30).

3- Modify Student_Name Column as Varchar2(30) not null.

4- Rename Student_Name Column to Student_FullName.

1- Drop Student_Age Column.

Drop column syntax: -


ALTER TABLE <table_name> DROP COLUMN <existing_column>;

Table 5: Drop Student_Age Column using SQL command

Example Explanation
Alter Table Students DROP COLUMN Student_Age; A table name Student was altered by
dropping one column.

2- Add one column is called Student_Phone_No as Varchar2(30).

Add column syntax: -


ALTER TABLE <table_name> ADD (<column1> <definition>, <columnN> <definition>);

Table 6: Add Student_Moblie_No using SQL command

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.

Modify column syntax: -


ALTER TABLE <table_name> MODIFY ( <existing_column> <definition>, … );

Table 7:Modify Student_Name Column

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

4- Rename Student_Name Column to Student_FullName.

Rename column syntax: -


ALTER TABLE <table_name> RENAME COLUMN <existing_column> TO <new_name>;

Table 8: Rename Student_Name column to Student_FullName

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.

Task1: Insert two rows to students table as the following bellow: -

Student_ID Student_FullName Student_Phone_No


102030 Ahmed Abdullah
102040 Khaled Abdullah 0560333040
Insert syntaxes:
There are two syntax to insert row to table:

1- INSERT INTO <table_name> ( <column1>, <column2>,… <columnN> )


VALUES ( <value1>, <value2>,… <valueN> ) ;

2- INSERT INTO <table_name> VALUES ( <value1>, <value2>,… <valueN> ) ;

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.

Table 9: Insert two rows to students table

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.

Task2: Select all records from students table.


SELECT Syntax: -
SELECT * FROM <table_name> ;

Table 10: Select * records from students table

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

Table 11: Update name of student

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".

Task3: Delete student whose student_id is "102030".

DELETE Syntax: -
DELETE FROM <table_name> WHERE <condition> ;

Table 12: Delete Student

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

Go back in “Object Browser” to see if the COURSE table was added.

2. Go back in “SQL Commands” and alter table COURSES by adding course_objective


column defined as varchar2(50).

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?

5. Insert the following records to table COURSES.


Course_ID Course_Title Unit Course_Objective
DB001 Intro. To DB 3 To learn DBMS
PROG001 Intro. To C++ 5 To learn C++
PROG002 Intro. To Java 5 To learn Java

6. Select all records from table COURSES. How many records are there?

7. Delete all rows whose units is less than 5.

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.

You might also like