0% found this document useful (0 votes)
7 views4 pages

Practical WK 6

Uploaded by

saugatpaudel824
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Practical WK 6

Uploaded by

saugatpaudel824
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Tutorial - week 6

Part A… Design continued from last week


Remember that you need to upload your work to VUWS no later than 2 days
after class day.

Discussion on Restaurant GRD from last week:

Teacher will discuss this and provide feedback on your work from last week…
Part B: SQL
Creating tables and Inserting records using SQL

PS: you will have to download the SQL (MySQL Workbench) software on your own laptops to work more
efficiently.

Tutor will demonstrate this part…

CREATE TABLE Album (


albumID INT PRIMARY KEY,
albumTitle VARCHAR(25) NOT NULL,
producer VARCHAR(35)
);
CREATE TABLE Song (
songID INT PRIMARY KEY,
songTitle VARCHAR(45),
yearPublished INT,
singer VARCHAR(40),
albumID INT,

CHECK(yearPublished >= 1800 AND yearPublished <=2200),


FOREIGN KEY(albumID) REFERENCES Album(albumID)
);

Insert these records into the tables:


INSERT INTO Album VALUES (1,"Thriller","Quincy Jones");
INSERT INTO Album VALUES (2,"Arrival","Benny Andersson, Bejorn
Ulvaeus");
INSERT INTO Album VALUES (3,"Thriller","Impersonator");

INSERT INTO Song VALUES (2,"Beat it",1982,"Michael Jackson",1);


INSERT INTO Song VALUES (3,"Dancing Queen",1976,"Abba",2);
INSERT INTO Song VALUES (4,"The girl is mine",1982,"Michael Jackson,
Paul McCartney",1);
INSERT INTO Song VALUES (5,"Gloomy Sunday",1935,"Pai Kalmar",NULL);

To check what records you have in your tables:

Type the SQL command: SELECT * FROM tablename to check what is in each table

 Now create a query that will list all the records in each of the tables.
 List all the song records for those not published in 1982
 List all the Albums that are ‘Thriller’.

Part C: SQL
1. Produce the following tables using the CREATE TABLE SQL statement.
CREATE TABLE Department (
depId char(1) PRIMARY KEY,
depName varchar(30)
);

CREATE TABLE Employee (


empId nvarchar(6) PRIMARY KEY,
lName nvarchar(20),
fName nvarchar(20),
depId char(1) REFERENCES Department
);

The attribute depId in Employee serves as a foreign key to table Department


(therefore must be a primary key there). The values of depId will still be
duplicated among some of the records but they are the necessary keys and are
thus not counted as data duplication.

2. We now enter some records to the tables. The INSERT statement is used to add
records to tables. Because the Employee contains a foreign key referencing the
table Department, we need to first add data to table Department
INSERT INTO Department VALUES ('G', 'general');
INSERT INTO Department VALUES ('H', 'hardware');

We then add a few records to Employee as follows.


INSERT INTO Employee VALUES (101, 'News', 'John', 'G');
INSERT INTO Employee VALUES (102, 'Edmond', ‘Sally’, 'H');
INSERT INTO Employee VALUES (103, 'Senior', 'David', 'H');

3. The SELECT statement is used to retrieve records from tables: create a SELECT
statement to display all records in the Employee table.
SELECT * FROM Employee;

4. Your turn to finish filling the tables so that you have about 4 records in each
table.

a. Add 3 more records to the Employee table using the fName 'David' for at
least one record.
b. Retrieve the fName and lName for all records for those employees with
an fName value of 'David'.
c. List all the employees who work in department ‘H’.

5. Use the INSERT statement to add the following record to the Employee table:
INSERT INTO Employee VALUES (102, 'Wang', 'Sam', 'H');

What happens when you try to add this record? Why?

6. How would you add a new column (attribute) for employee contact in the table
employee? Do this as SQL statement.
7. In the employee table, we need to change the name ‘John’ to ‘Johnson’. Apply
this change via an SQL statement.
8. Remove Sally’s record from the table.

Refer to Quick SQL REFERENCE document.


Another useful reference: https://www.w3schools.com/sql/sql_alter.asp

Save all your work along with screenshots for each question and answer including the
results table.

You might also like