You are on page 1of 3

CT042-3-1-Introduction to Databases SQL

Lab 5

Table: Publisher
PublisherID Name Address
P01 Pearson Bukit Jalil
P02 Deitel Puchong
P03 Rainbow Subang
P04 MacHill Kuala Lumpur

Note: PublisherID is a primary key in Publisher Table

Table: Book
BookID Name Author Price PublishedDate PublisherID
B01 Maths J.Wenton 50.60 10 Jan 2016 P01
B02 Science S.Hanson 100.00 12 Feb 2016 P01
B03 English K.Vince 89.30 9 March 2016 P02
B04 Biology K.Vince 150.80 24 April 2016 P03
B05 Computing J.Denzin NULL NULL NULL

Note: BookID is a primary key in Book Table


Note: PublisherID is a foreign key in Book Table

1. Using Ms SQL Server, create a new database Lab5


2. Write query to create the tables given above (you may copy them from lab2)
3. Write query to add each row of data into the tables (you may copy them from lab2)

Attributes Data Type


PublisherID nvarchar(50)
Name nvarchar(50)
Address nvarchar(50)

Attributes Data Type


BookID nvarchar(50)
Name nvarchar(50)
Author nvarchar(50)
Price decimal(10,2)
PublishedDate date
PublisherID nvarchar(50)

Reference:
https://www.w3schools.com/sql/default.asp

Using Where Clause and SQL ‘Like’ operator


4. Display a list of Publishers where publisher name starts with the alphabet ‘r’
5. Display a list of Publishers where publisher name ends with the alphabet ‘n’

Level 1 Asia Pacific University of Technology & Innovation Page 1 of 3


CT042-3-1-Introduction to Databases SQL

6. Display a list of Books where book name contains the alphabet ‘a’ in the second
position
7. Display a list of Books where book name begins with the alphabet ‘b’ and are at least
2 characters in length
8. Display a list of Books where book name contains the alphabet ‘i’ in any position
9. Display a list of Books where book name begins with the alphabet ‘e’ and end with ‘h’

Using Where Clause and multiple operators: Like, Order By


10. Display a list of Books where the author’s name contains the alphabet ‘n’ in any
position, order the result in ascending order by author name and descending order by
price.
11. Display a list of Books where author name ends with the alphabet ‘e’ and are at least 3
characters in length, order the result in descending order by book name.

Using Where Clause and SQL ‘In’ operator


12. Display a list Publishers whose address is Puchong or Subang
13. Display a list of Books which price is 50 or 100
14. Display a list of Books which name is Maths or Science or English, order the result in
ascending order by price

Using Where Clause and SQL ‘Update’ Statement


15. Publisher ‘Pearson’ had changed its address from Bukit Jalil to Serdang, update this.
16. The price of the English Book written by K.Vince had changed from 89.30 to 99.30,
update this.
17. Change the PublishedDate of Science book written by ‘S.Hanson’ to ‘3 September
2017’
18. Update the price and published date of English book to ‘98’ and ’29 April 2019’

Using SQL ‘Alter Table’ Statement


19. Change the data type of Book name column to varchar(50)
*cannot simply change the data type if there are existing data in the table
20. Add another column to Publisher table, name it as ‘Telephone’, use integer as data
type
21. Change the newly added column in Publisher table, change it from ‘Telephone’ to
‘ContactNumber’, use integer as data type. (Hint: use sp_rename)
22. Delete the newly added column ‘ContactNumber’

Using SQL ‘Delete’ Statement


23. Delete a record, for the biology book written by ‘K.Vince’.
24. Remove the Maths book written by ‘J.Wenton’

Level 1 Asia Pacific University of Technology & Innovation Page 2 of 3


CT042-3-1-Introduction to Databases SQL

Using SQL ‘Drop’ Statement


25. Create a new table ‘Customer’, put ID and Name as attribute (use suitable data type),
then use the ‘Drop’ statement to delete the whole table.
26. Create a new database ‘MyDatabase’, then use the ‘Drop’ statement to delete the
whole Database.

Level 1 Asia Pacific University of Technology & Innovation Page 3 of 3

You might also like