all labs f3038

38
Table of Contents TITLE Page Number Course Description 2 LAB 1 : Entity Relational Model (Part 1) 4 LAB 2 : Entity Relational Model (Part 2) 10 LAB 3 : Relational Schema 7 LAB 4 : Normalization 11 LAB 5 : Structured Query Language (Part 1) 13 LAB 6 : Structured Query Language (Part 2) 15 LAB 7 : Structured Query Language (Part 3) 18 LAB 8 : Structured Query Language (Part 4) 21 LAB 9 : Structured Query Language (Part 5) 24 LAB 10 : Structured Query Language (Part 6) 26

Upload: shaemal-jamaludin

Post on 07-Mar-2015

118 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: All Labs F3038

Table of Contents

TITLEPage

NumberCourse Description 2

LAB 1 : Entity Relational Model (Part 1) 4

LAB 2 : Entity Relational Model (Part 2) 10

LAB 3 : Relational Schema 7

LAB 4 : Normalization 11

LAB 5 : Structured Query Language (Part 1) 13

LAB 6 : Structured Query Language (Part 2) 15

LAB 7 : Structured Query Language (Part 3) 18

LAB 8 : Structured Query Language (Part 4) 21

LAB 9 : Structured Query Language (Part 5) 24

LAB 10 : Structured Query Language (Part 6) 26

Page 2: All Labs F3038

Course Description

F3038 DATABASE SYSTEM

INSTRUCTIONAL DURATION : 15 WEEKS

CREDIT(S) : 3

PRE-REQUISITE(S) : NONE

SYNOPSIS

DATABASE SYSTEM course offers a comprehensive coverage of databases and the various operations of data manipulation. This course covers how to design and develop normalized databases and manipulate data using Structured Query Language.

LEARNING OUTCOMES

Upon completion of this course, students should be able to:

1. describe the characteristics of database system.

2. design the entity-relational model for representing and designing databases.

3. apply normalization techniques to design a database.

4. discover the security issues, concurrency and recovery in database.

5. create database using basic Structured Query Language.

2

Page 3: All Labs F3038

LAB 1: Entity Relationship Model

Learning Outcomes

By the end of this lab, students should be able to:1. Identify entity types and sets of entity.2. Identify the attribute and a value set.3. Identify the E-R model.4. Identify the following relationship types in E-R model:

a. 1 : 1 (one to one)b. 1: N (one to many)c. M : N (many to many)

5. Draw E-R model based on a given situation.

Theory/ Terminologies

Entity Relationship Model is a notation can be used to design database. It is a traditional approaches but yet very popular. In E/R model, the elements of the database is represented graphically using E/R diagrams

using three principal elements types:- Entity sets- Attributes- Relationships

Activity 1Draw an ER-Diagram using Chen Model to show the relationship for the following entities inside the STUDENTS database.

a. StudentInfob. Subjectc. Lecturerd. Hostele. AcademicAdvisor

Procedures:

Step 1: Identify all the possible attributes for each entity.Step 2: Identify the Primary Keys.Step 3: Identify all the relationships and connectivity.Step 4: Draw the ER-Diagram.

3

Page 4: All Labs F3038

LAB 2: Entity Relationship Model

Learning Outcomes

By the end of this lab, students should be able to:1. Apply the concept of Crow’s Foot Modeling Symbols2. Identify the attribute and a value set.3. Identify the Primary Keys.4. Identify the following relationship types in E-R model:

a. 1 : 1 (one to one)b. 1: N (one to many)c. M : N (many to many)

5. Identify the solution and draw E-R model based on a given problem.

Theory/ Terminologies

Crow’s Foot model is one of the Entity Relationship Modeling Symbols. The Crow’s Foot approach uses sticklike symbols to illustrate ER model. In ER model, the elements of the database is represented graphically using ER diagrams

using three principal elements types:- Entity sets- Attributes- Relationships

Activity 2Consider that the bank keeps track of customer, employees, accounts and bank’s loan. Develop a Crow’s Foot ERD (Entity relationship diagram) based on these requirements:

The bank is organized into branches. Each branch is located in a particular city and is identified by a unique name. The bank monitors the assets of each branch. A branch can originate one or more loans.

Bank customers are identified by their customer-id values. The bank stores customer’s name, street and city where the customer lives. Customer may have accounts and can take out loans. A customer may be advised by a particular bank/employees, who may act as loan officer or personal banker for that customer, and that particular banker can advised one or more customer.

Bank employees are identified by their employee-id values. The bank administration stores the name and telephone number of the employee, the names of employee’s dependents and the employee-id of the employee’s manager. The bank also keeps track of the employee’s start date and the length of employment. An employee works for only one manager and that manager supervises one or more employees.

4

Page 5: All Labs F3038

The bank maintains information about accounts. Accounts can be held by more than one customer, and a customer can have more than one account. Each account is assigned with a unique account number. The bank maintains a record of each account’s balance, and the most recent date on which the account was accessed by each customer holding the account.

A loan originates at a particular branch and can be held by one or more customer. A loan is identified by a unique loan number. For each loan, the bank keeps track of the loan amount. Each loan is associated with one or more payments. A payment number identifies a particular payment for a specific loan. The date and amount are recorded for each payment.

Procedures:

Step 1: Identify all the possible entities.Step 2: Identify all the possible attributes for each entity.Step 3: Identify the Primary Keys.Step 4: Identify all the relationships and connectivity.Step 5: Draw the ER-Diagram using Crow’s Foot Symbols.

5

Page 6: All Labs F3038

STUDENTS INFORMATION DATABASE

STUDENT LECTURER

Id_Student Name Age Sex State

1111 Ahmad 18 M Penang2222 Salmah 19 F Johor3333 Zanita 18 F Pahang4444 Ai Ling 18 F Perak5555 Raju 19 M Johor6666 Monaj 20 M Penang7777 Teng Kiat 19 M Sabah8888 Yamin 18 M Kedah

SUBJECT

Id_Lec Lec_Name Subject State Salary

3120 Badariah 3101 Johor 23003333 Zanita 2113 Pahang 40005123 Khairul 0101 Selangor 32001273 Yogess 2211 Johor 18656545 Najwa 3418 Sarawak 50507777 Teng Kiat 3101 Sabah 2300

Id_Student Name LecId

1111 Ahmad 31202222 Salmah 33333333 Zanita 51234444 Ai Ling 12735555 Raju 65456666 Monaj 77777777 Teng Kiat 51238888 Yamin 1273

Subject_Code Subject_Name Lecturer Credit_Hours

0101 C++ Khairul 32113 Fundamental of IT Zanita 23418 Database System Najwa 42211 Computer Hardware Yogess 23101 Network Security Teng Kiat 2

SUPERVISOR

6

Page 7: All Labs F3038

LAB 3: Relational Algebra

Learning Outcomes

By the end of this lab, students should be able to:1. Understand the use of Relational Algebra in databases.2. Identify the relational algebra operator in databases.3. Apply the Relational Algebra operation.

Theory/ Terminologies

Relational algebra is the basic set of operations for the relational model These operations enable a user to specify basic retrieval requests (or queries) The result of an operation is a new relation, which may have been formed from one or

more input relations

Activity 3AWrite the correct symbol for each Rational Algebra operation.

i. SELECT (…..)ii. PROJECT (…..)iii. RENAME (…..)iv. UNION (…..)v. INTERSECTION (…..)vi. DIFFERENCE (…..)vii. CARTESIAN PRODUCT (…..)viii. JOIN (…..)

Activity 3BWrite the general form of all the statements below using Relational Algebra symbol.

i. Select the STUDENT tuples whose Age number is 18 and State is Pahang:

ii. Retrieve Subject_Code, Subject_Name and Credit_Hours from SUBJECT:

iii. Rename the attributes inside table LECTURER to LecId, LName, Sub_Code, State_Name and Sal

7

Page 8: All Labs F3038

iv. Join table LECTURER and SUPERVISOR where Id_Lec equal to LecId.

v. Join table SUBJECT and LECTURER where Subject_Code equal to Subject or Credit_Hours equal to 2.

Activity 3CDraw a table to represent the output of each operation.

i. STUD Id_Student, Name (STUDENT)

ii. LEC Lec_Id, Lec_Name (LECTURER)

iii. STUD LEC

iv. STUD LEC

8

Page 9: All Labs F3038

v. STUD LEC

vi. LEC STUD

9

Page 10: All Labs F3038

A SAMPLE REPORT LAYOUT

10

Page 11: All Labs F3038

LAB 4: Normalization

Learning Outcomes

By the end of this lab, students should be able to:1. Understand the uses of Normalization in databases.2. Apply 1st Normal Form, 2nd Normal Form and 3rd Normal Form to the given

tables.3. Apply Normal Form in designing database.

Theory/ Terminologies

Normalization is a technique for producing a set of suitable relations that support the data requirements of an enterprise.

These process will decomposing unsatisfactory "bad" relations by breaking up their attributes into smaller relations

Activity 4APerform the normalization process from Un-Normal Form (UNF) to First Normal Form (1NF).

Procedures:

Step 1: Eliminate the repeating GroupsStep 2: Identify the Primary Key.Step 3: Identify all Dependencies.

Activity 4BPerform the normalization process from First Normal Form (1NF) to Second Normal Form (2NF).

Procedures:

Step 1: Identify all Key Components Step 2: Identify the Dependent Attribute.

Activity 4CPerform the normalization process from Second Normal Form (2NF) to Third Normal Form (3NF).

Procedures:

Step 1: Identify Each New Determinant

11

Page 12: All Labs F3038

Step 2: Identify the Dependent AttributesStep 3: Remove the dependent Attributes from Transitive Dependencies.

12

Page 13: All Labs F3038

LAB 5: Structured Query Language

Learning Outcomes

By the end of this lab, students should be able to:1. Open the Mysql connection using MS-Dos command.2. Create database.3. Create tables inside the database.4. Use UNIQUE functions SQL command.5. View tables using SQL command.

Theory/ Terminologies

Structured Query Languages (SQL) is a main language for relational DBMSs. The main characteristics of SQL are:

- Relatively easy to learn;- Non-procedural - you specify what information you require, rather than how to get it;- Essentially free-format;- Consists of standard English words like SELECT, INSERT, and UPDATE;- Can be used by range of users.

Activity 5AOpen the Mysql connection using MS-Dos.

Procedures:

C:\>cd xampp

C:\xampp>cd mysql

C:\xampp\mysql>cd bin

C:\xampp\mysql\bin>mysql -u root -p

Enter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4 to server version: 4.1.9-max

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

13

Page 14: All Labs F3038

Activity 5BCreate database using SQL command.

Procedures:

mysql>CREATE database COMPANY;

Activity 5CCreate tables inside the database using SQL command.

Procedures:

CREATE TABLE VENDOR (V_KOD NUMBER(5) NOT NULL UNIQUE,V_NAMA VARCHAR(35) NOT NULL,V_POSKOD VARCHAR(5) NOT NULL,V_TELEFON NUMBER(5) NOT NULL,PRIMARY KEY (V_KOD) );

CREATE TABLE PRODUCT (P_KOD VARCHAR(7) NOT NULL UNIQUE,P_ITEM VARCHAR(35) NOT NULL,P_HANTAR NUMBER(1) NOT NULL,P_HARGA DECIMAL(8,2) NOT NULL,V_KOD NUMBER(5) NOT NULL,PRIMARY KEY (P_KOD),FOREIGN KEY (V_KOD) REFERENCES VENDOR

ON DELETE RESTRICT ON UPDATE CASCADE );

Activity 5DView tables using SQL command.

Procedures:

mysql>SHOW tables;mysql> DESCRIBE VENDOR;mysql> DESCRIBE PRODUCT;

14

Page 15: All Labs F3038

LAB 6: Structured Query Language

Learning Outcomes

By the end of this lab, students should be able to:1. Update the structured of tables using SQL command.2. Key in data into created tables.3. View data from the tables using SQL command.

Theory/ Terminologies

Structured Query Languages (SQL) is a main language for relational DBMSs. The main characteristics of SQL are:

- Relatively easy to learn;- Non-procedural - you specify what information you require, rather than how to get it;- Essentially free-format;- Consists of standard English words like SELECT, INSERT, DELETE and UPDATE;- Can be used by range of users.

Activity 6AUpdate the structured of table VENDOR and PRODUCT inside the COMPANY database.

Procedures:

i. Modify

- Change the P_ITEM character length from 35 to 40.

ALTER TABLE PRODUCTMODIFY P_ITEM VARCHAR(40);

- Change the P_HARGA decimal field from 8 to 9:-

ALTER TABLE PRODUCTMODIFY P_HARGA DECIMAL(9,2);

ii. Add

- Add attribute V_AGE into table VENDOR

ALTER TABLE VENDORADD V_AGE INT(3);

15

Page 16: All Labs F3038

iii. Drop

- Delete attribute V_AGE from table VENDOR

ALTER TABLE VENDORDROP V_AGE;

Activity 6BKey in data into table VENDOR and PRODUCT using INSERT command.

Procedures:

INSERT INTO VENDORVALUES ( 21225, ‘Cybit Sdn. Bhd’, 86400, 07-4562166, …);

INSERT INTO PRODUCTVALUES (‘SP7777’, ‘Speaker’, 11, 120.56, 21225, …);

Instruction: Insert all the following data into table VENDOR and PRODUCT

VENDOR

06-343353575343BBK Sdn. Bhd.25595

03-454544623900KK Sdn. Bhd24004

06-454343434800SCP Sdn. Bhd.23119

07-856544387209Images Sdn. Bhd.24288

09-629798832577Digital Sdn. Bhd21344

09-603434556822Zizi Sdn. Bhd21231

09-624344523000Y2K Sdn. Bhd.21226

07-456216686300Cybit Sdn. Bhd21225

V_TELEFONV_POSKODV_NAMAV_KOD

06-343353575343BBK Sdn. Bhd.25595

03-454544623900KK Sdn. Bhd24004

06-454343434800SCP Sdn. Bhd.23119

07-856544387209Images Sdn. Bhd.24288

09-629798832577Digital Sdn. Bhd21344

09-603434556822Zizi Sdn. Bhd21231

09-624344523000Y2K Sdn. Bhd.21226

07-456216686300Cybit Sdn. Bhd21225

V_TELEFONV_POSKODV_NAMAV_KOD

16

Page 17: All Labs F3038

PRODUCT

24004500.9920MonitorMR0042

21231110.8914ModemMM4422

21225120.5611SpeakerSP7777

99.897RAMRM2196

2428869.896KeyboardKB1212

21344980.9914MotherboardMB0044

35.457MouseM02111

21225320.9913PrinterPP0001

2559512.693KabeL printerKP9900

2428810.994Disket 1.4”DK5505

2428812.994Mouse padMP1110

2311950.998Casing CS0222

2311980.505Meja komputerMK0122

2134450.894Kerusia komputerKK0011

21344300.9912ScannerSC0106

2559530.996JoystickJT00033

V_KODP_HARGAP_HANTARP_ITEMP_KOD

24004500.9920MonitorMR0042

21231110.8914ModemMM4422

21225120.5611SpeakerSP7777

99.897RAMRM2196

2428869.896KeyboardKB1212

21344980.9914MotherboardMB0044

35.457MouseM02111

21225320.9913PrinterPP0001

2559512.693KabeL printerKP9900

2428810.994Disket 1.4”DK5505

2428812.994Mouse padMP1110

2311950.998Casing CS0222

2311980.505Meja komputerMK0122

2134450.894Kerusia komputerKK0011

21344300.9912ScannerSC0106

2559530.996JoystickJT00033

V_KODP_HARGAP_HANTARP_ITEMP_KOD

Activity 6CList all the data from table VENDOR and PRODUCT.

Procedures:

mysql> SELECT * FROM VENDOR;mysql> SELECT * FROM PRODUCT;

17

Page 18: All Labs F3038

LAB 7: Structured Query Language

Learning Outcomes

By the end of this lab, students should be able to:1. Use SQL commands to create database, tables and input data into tables.2. Use variety of SELECT commands to view data.3. Use LIKE and ORDER BY function to view data.4. Use SQL commands to manipulate data.

Theory/ Terminologies

Structured Query Languages (SQL) is a main language for relational DBMSs. The main characteristics of SQL are:

- Relatively easy to learn;- Non-procedural - you specify what information you require, rather than how to get it;- Essentially free-format;- Consists of standard English words like SELECT, INSERT, DELETE and UPDATE;- Can be used by range of users.

Activity 7ACreate a database and name it as KERETA. Create tables PELANGGAN, KOMPONEN_KERETA, TUNTUTAN and KOMPONEN_TUNTUTAN and insert all the data into the tables as showed below:

PELANGGAN

NoPelangganNamaPelangga

nNoKereta NoTelefon TarikhDaftar

P0001 David Lim WKM 1234 362887145 12/06/02P0002 Abu Hassan CBA 8999 67788123 23/05/02P0003 Low Ban Huat PFL 3434 42947541 05/01/03P0004 Karigalan BGN 2511 379552833 23/05/02P0007 Kamal Ibrahim WEP 1103 322927894 10/04/99

KOMPONEN_KERETANoKomponen NamaKomponen Harga

KK001 Lampu belakang Iswara 100KK002 Bumper Depan Wira 450KK003 Cermin Depan Iswara 500KK004 Lampu Brek Waja 250KK005 Lampu Depan Wira 350

18

Page 19: All Labs F3038

TUNTUTANNoTuntutan TarikhTuntutan NoPelanggan

T0001 06/06/02 P0002T0002 09/09/02 P0001T0003 03/02/03 P0004T0004 07/08/02 P0002T0005 15/01/03 P0003

KOMPONEN_TUNTUTANNoTuntutan NoKomponen Kuantiti

T0001 KK002 1T0001 KK005 2T0003 KK001 2T0005 KK003 1T0004 KK001 2T0002 KK003 1T0003 KK003 1

Activity 7BUse SELECT commands to display all data from tables.

Procedures:

1. List full details of all PELANGGAN.

i. Write all columns name in the selection statement:

SELECT NoPelanggan, NamaPelanggan, NoKereta, NoTelefon, TarikhDaftar FROM PELANGGAN;

ii. Use * as an abbreviation for ‘all columns’

SELECT * FROM PELANGGAN;

2. Produce a list of information about NoPelanggan, NoKereta and TarikhDaftar.

SELECT NoPelanggan, NoKereta, TarikhDaftar FROM PELANGGAN;

Activity 7CUse DISTINCT commands to eliminate redundancy data.

Procedures:

1. List the TarikhDaftar of all customers.

19

Page 20: All Labs F3038

SELECT TarikhDaftar FROM PELANGGAN;

2. Use DISTINCT to eliminate the redundancy in (i).

SELECT DISTINCT TarikhDaftar FROM PELANGGAN;

Activity 7DUse SQL commands to manipulate data.

Procedures:

1. Pattern Matching

i. Starting with capital ‘W’

SELECT NoPelanggan, NamaPelanggan, NoKereta FROM PELANGGAN WHERE NoKereta LIKE ‘W%’;

ii. Ending with word ‘ra’

SELECT NoKomponen, NamaKomponen, Harga FROM KOMPONEN_KERETA WHERE NamaKomponen LIKE ‘%ra’;

2. Column Ordering

SELECT NoPelanggan, NamaPelanggan, NoTelefon FROM PELANGGAN ORDER BY NamaPelanggan ASC;

20

Page 21: All Labs F3038

LAB 8: Structured Query Language

Learning Outcomes

By the end of this lab, students should be able to:1. Use SQL commands with set of comparison.2. Use EXIST function in SQL.3. Use calculation function in SQL.

Theory/ Terminologies

Structured Query Languages (SQL) is a main language for relational DBMSs. The main characteristics of SQL are:

- Relatively easy to learn;- Non-procedural - you specify what information you require, rather than how to get it;- Essentially free-format;- Consists of standard English words like SELECT, INSERT, DELETE and UPDATE;- Can be used by range of users.

Activity 8AUse SQL commands with set of comparison.

Procedures:

3. Search Conditions

i. Comparison Search Condition

SELECT NoTuntutan, NoKomponen, FROM KOMPONEN_KERETA WHERE Kuantiti>1;

ii. Compound Comparison Search Condition

SELECT * FROM TUNTUTAN WHERE NoPelanggan=‘P0001’ OR NoPelanggan=‘P0003’;

iii. Range Search Condition

SELECT NoKomponen, NamaKomponen, Harga FROM KOMPONEN_KERETA WHERE Harga BETWEEN 200 AND 400;

4. Set Membership.

SELECT NoTuntutan, Kuantiti FROM KOMPONEN_TUNTUTAN WHERE NoKomponen IN (‘KK001’, ‘KK003’);

21

Page 22: All Labs F3038

5. EXIST Function in Search Condition

i. INSERT new data into table TUNTUTAN.

INSERT INTO TUNTUTAN VALUES (‘T0001’, ‘’, ‘P0001’)

ii. Display all the data from table TUNTUTAN that own TarikhTuntutan.

SELECT * FROM TUNTUTAN WHERE TarikhTuntutan EXIST;

Activity 8BUse calculation function in SQL.

Procedures:

1. Calculated Fields

iii. Display the new price for all components if each price is multiplies by 15.

SELECT NoKomponen, NamaKomponen, Harga*15 FROM KOMPONEN_KERETA;

iv. Display the new price for all components if each price is discounts by RM 5. Name the new column as HargaBaru.

SELECT NoKomponen, NamaKomponen, Harga-5 AS HargaBaru FROM KOMPONEN_KERETA;

2. Use Count

i. Display total of KOMPONEN_KERETA where Harga< 300. Name the new column as BilKomponen.

SELECT COUNT(*) AS BilKomponen FROM KOMPONEN_KERETA WHERE Harga< 300;

ii. Display information about NAMAPELANGGAN and total of TUNTUTAN for each PELANGGAN.

SELECT NamaPelanggan, COUNT(NoTuntutan)FROM PELANGGAN, TUNTUTANWHERE Pelanggan.NoPelanggan=Tuntutan.NoPelangganGROUP BY NamaPelanggan;

22

Page 23: All Labs F3038

STUDENTS INFORMATION DATABASE

STUDENT LECTURER

Id_Student Name Age Sex State

1111 Ahmad 18 M Penang2222 Salmah 19 F Johor3333 Zanita 18 F Pahang4444 Ai Ling 18 F Perak5555 Raju 19 M Johor6666 Monaj 20 M Penang7777 Teng Kiat 19 M Sabah8888 Yamin 18 M Kedah

SUBJECT

Id_Lec Lec_Name Subject State Salary

3120 Badariah 3101 Johor 23003333 Zanita 2113 Pahang 40005123 Khairul 0101 Selangor 32001273 Yogess 2211 Johor 18656545 Najwa 3418 Sarawak 50507777 Teng Kiat 3101 Sabah 2300

Id_Student Name LecId

1111 Ahmad 31202222 Salmah 33333333 Zanita 51234444 Ai Ling 12735555 Raju 65456666 Monaj 77777777 Teng Kiat 51238888 Yamin 1273

Subject_Code Subject_Name Lecturer Credit_Hours

0101 C++ Khairul 32113 Fundamental of IT Zanita 23418 Database System Najwa 42211 Computer Hardware Yogess 23101 Network Security Teng Kiat 2

SUPERVISOR

23

Page 24: All Labs F3038

LAB 9: Structured Query Language

Learning Outcomes

By the end of this lab, students should be able to:1. Use SQL commands to create database, tables and input data into tables.2. Apply mechanisms in SQL.3. Use GRANT function to allow user to use certain or all privileges in database.4. Use REVOKE function to cancel certain or all privileges from user.

Theory/ Terminologies

Structured Query Languages (SQL) is a main language for relational DBMSs. The main characteristics of SQL are:

- Relatively easy to learn;- Non-procedural - you specify what information you require, rather than how to get it;- Essentially free-format;- Consists of standard English words like SELECT, INSERT, DELETE and UPDATE;- Can be used by range of users.

There are four types of ISO privileges in database: SELECT, INSERT, DELETE and UPDATE.

Activity 9ACreate a database and name it as STUDENT INFORMATION. Create all the tables and fill all the data into the related tables as shown above:

Activity 9BUse GRANT function to allow user to use certain or all privileges in database.

Procedures:

1. Grand all privileges to Monaj on table SUPERVISOR.

GRANT ALL PRIVILEGESON SUPERVISORTO Monaj WITH GRANT OPTION;

2. Grant select and update to Id_Student=4444 on table STUDENT.

GRANT SELECT, UPDATEON STUDENTTO 4444;

24

Page 25: All Labs F3038

3. Grant delete, insert and update for attribute Subject_Name to Najwa on table SUBJECT.

GRANT DELETE, INSERT, UPDATE (Subject_Name)ON SUBJECTTO Najwa;

4. Grant insert to public on table LECTURER.

GRANT INSERTON LECTURERTO PUBLIC;

Activity 9CUse REVOKE function to cancel certain or all privileges from user.

Procedures:

1. Revoke delete from Monaj on table SUPERVISOR.

REVOKE DELETEON SUPERVISORFROM Monaj;

2. Revoke select and update from Id_Student=4444 on table STUDENT.

REVOKE SELECT, UPDATEON STUDENTFROM 4444;

3. Revoke delete, insert and update for attribute Subject_Name from Najwa on table SUBJECT.

REVOKE DELETE, INSERT, UPDATE (Subject_Name)ON SUBJECTFROM Najwa;

4. Revoke insert from public on table LECTURER.

REVOKE INSERTON LECTURERFROM PUBLIC;

25

Page 26: All Labs F3038

LAB 10: Structured Query Language

Learning Outcomes

By the end of this lab, students should be able to:1. Apply basic data management in SQL (UPDATE & DELETE).2. Use SQL’s numeric functions.3. Use nested queries statement in SQL.

Theory/ Terminologies

Structured Query Languages (SQL) is a main language for relational DBMSs. The main characteristics of SQL are:

- Relatively easy to learn;- Non-procedural - you specify what information you require, rather than how to get it;- Essentially free-format;- Consists of standard English words like SELECT, INSERT, DELETE and UPDATE;- Can be used by range of users.

There are four types of SQL commands can be use for data management process in database: i. SELECT, ii. INSERT, iii. DELETE, iv. UPDATE.

SQL’s numeric functions such as MIN, MAX, SUM and AVG are used as calculation functions for retrieving data from database.

Activity 10AApply basic data management in SQL (UPDATE & DELETE).

Procedures:

1. Making correction.

i. Update value of one attribute.

UPDATE SUBJECT SET Subject_Name = ‘Programming in C++’ WHERE Subject_Code = ‘0101’;

ii. Update values of a lot attributes.

UPDATE STUDENT SET Name = ‘Hasnah’, Sex = ‘F’WHERE Id_Student = ‘1111’;

26

Page 27: All Labs F3038

2. Delete table rows.

DELETE FROM SUPERVISOR WHERE Id_Student = ‘1111’;

Activity 10BUse SQL’s numeric functions.

Procedures:

1. SUM

SELECT SUM(Salary*5) FROM LECTURER;

2. MAX

SELECT MAX(Credit_Hours) FROM SUBJECT;

3. MIN

SELECT MIN(Salary) FROM LECTURER;

4. AVG

SELECT Id_Lec, Lec_Name, State FROM LECTURERWHERE Salary > (SELECT AVG(Salary) FROM LECTURER)ORDER BY Salary DESC;

Activity 10CUse nested queries statement in SQL.

Procedures:

1. Examples of nested queries for data selection.

i. SELECT SUBJECT.Subject_Name, LECTURER.Lec_NameFROM SUBJECT, LECTURERWHERE SUBJECT.Subject_Code In (SELECT LECTURER.Subject From SUBJECT, LECTURERWHERE SUBJECT.Subject = LECTURER.Subject_Code);

ii. SELECT STUDENT.Name, STUDENT.Age LECTURER.Subject, LECTURER.SalaryFROM STUDENT, LECTURERWHERE STUDENT.Id_Student = LECTURER.Id_Lec);

27