0% found this document useful (0 votes)
8 views

Lab 2

The document outlines a lab exercise for creating a database named Lab2, including the creation of Publisher and Book tables with specified attributes and data types. It details SQL operations such as inserting data, and the constraints related to foreign keys that prevent certain operations like deleting publishers with related books. Additionally, it provides examples of SQL syntax for creating tables and inserting data.

Uploaded by

宫崎 羽い
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lab 2

The document outlines a lab exercise for creating a database named Lab2, including the creation of Publisher and Book tables with specified attributes and data types. It details SQL operations such as inserting data, and the constraints related to foreign keys that prevent certain operations like deleting publishers with related books. Additionally, it provides examples of SQL syntax for creating tables and inserting data.

Uploaded by

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

CT042-3-1-Introduction to Databases SQL

Lab 2

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 PublishedDat PublisherID
e
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 Computi J.Denzin NULL NULL NULL
ng

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 Lab2


2. Write query to create the tables given above
3. Write query to add each row of data into the tables

Attributes Data Type


PublisherID nvarchar(5
0)
Name nvarchar(5
0)
Address nvarchar(5
0)

Attributes Data Type


BookID nvarchar(5
0)
Name nvarchar(5
0)
Author nvarchar(5
0)
Price decimal(10,
2)
PublishedDat date
e
PublisherID nvarchar(5
0)

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


CT042-3-1-Introduction to Databases SQL

*for decimal data type, we can only insert 10 digits (including decimal point)
Correct: 12345678.91
Wrong: 123456789.91
However if we insert 3 decimal point (eg: 12345678.456)
It will automatically round off to 2 decimal points

4. Try to insert a new row in Book Table where PublisherID does not exist in
Publisher table. Can or cannot? Why?
Cannot PublisherID in Book Table is foreign key
While we cannot insert more than 1 value in the fk column even though the
values are valid

5. Try to delete the row for Publisher named Deitel. Can or cannot? Why
Cannot
Because there is related data in the same row (‘P02’) which is used in the FK
of Book Table

6. Try to delete the row for Publisher named MacHill. Can or cannot? Why
Can
Because its PK (‘P04’) is not used in the FK of Book Table

7. Try to delete any row for a book. Can or cannot? Why


Can
Because the BookID is not use as FK in another related table

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

CREATE TABLE table_name (


column1 datatype,
column2 datatype,
column3 datatype,
....
);

CREATE TABLE Persons (


ID int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);

CREATE TABLE Orders (


OrderID int NOT NULL PRIMARY KEY,
OrderNumber int NOT NULL,

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


CT042-3-1-Introduction to Databases SQL

PersonID int FOREIGN KEY REFERENCES Persons(PersonID)


);

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

You might also like