You are on page 1of 2

SD 2052

Database Systems

Tutorial: 3
Topic: Manipulating SQL on a single table

Discussion

1. What are the relational algebra operations and how are they different from SQL
commands?
2. How does SQL implement the relational algebra operations product, select, and
project?
3. How does SQL implement the natural, equi and theta joins?

Practical: Operations on a single table


Objective: this tutorial lets you learn how to create your own tables using SQL.

Start up the Command Centre and make sure that it is set up to use semicolon as the
statement termination character (just like Tutorial 2) and create a new script that does the
following:

1. Create a new database named tut3.


2. Create the PERSON table the attributes are P_IC BIGINT, P_NAME VARCHAR
(50), P_HOMETOWN VARCHAR (50), P_STATE VARCHAR (50), P_RELIGION
VARCHAR (50), P_BIRTHDAY DATE and the primary key is P_IC.

create table person


( p_ic bigint not null,
p_name varchar(50),
p_hometown varchar(50),
p_state varchar(50),
p_religion varchar(50),
p_birthday date,
primary key (p_ic)
);

3. Enter yourself and 10 other people into the database. If you can't find out information
about 10 other real people it's OK to make up imaginary people. Give yourself the
P_IC 0 and the other people P_IC 1 to 10. Remember that P_IC should be NOT
NULL. The default year format is YYYY-MM-DD.

Page 1
4. Make sure that there are some people in your database who have the same state as
you and have the same religion as you and are younger than you. (This is so that
question 7 will have some visible results.)

Example of data:
p_ic p_name p_hometown p_state p_religion p_birthday
0 Jessie Shah Alam Selangor Christian 1980-06-
02
1 Bala Krishnan Ipoh Perak Hindu 1982-01-
01
2 Munir Mohd Amin Taiping Perak Muslim
1981-09-07
3 Chua Ah Kow Sungai Petani Kedah Buddhist
1980-01-02
4 P. Pandian Dengkil Selangor Hindu
1979-12-25
5 Dadala Ratnam Petaling Jaya Selangor Christian 1982-01-
05
6 Faruq Tariq Simpang Tiga Kedah Muslim
1981-09-30
7 Marina Matthew Kelang Selangor Christian 1982-07-
22
8 Phua Chu Kang Katong Singapore Christian 1966-08-
20
9 Gurmit Singh Sepang Selangor Sikh 1980-08-
20
10 Ng Faye Faye Cheras Selangor Buddhist 1982-02-
03

Write the SQL scripts to do the following queries:


5. Type in the command to show the birthdays, names, and hometowns of all the people
in your database who are were born before 1 January 1981.
6. Type in the command to show all the people in your database who are the same
religion as you. (Clue: this is a nested query. “You” are the entity with P_IC=0.)
7. Type in the command to show all the people in your database who are from the same
state as you and have the same religion but is younger than you.
8. Save your resulting script on your pen drive.
9. Email the saved file to the tutor. (The tutor will tell you his/her email address for this
purpose.) The tutor will email you back an acknowledgement before the end of this
week so that you will have some sort of proof that you had turned in your tutorial in
case of disputes about missing tutorial marks in the future. If you don’t get an email
acknowledgement from the tutor by the end of this week please see him/her to sort it
out immediately and don’t wait till the end of trimester.
10. Disconnect from the database and DROP DATABASE TUT3 so that the next person
to try this tutorial on the same computer won’t crash into your work.

Page 2

You might also like