0% found this document useful (0 votes)
128 views3 pages

Lab 1.2 PDF

The document describes creating and populating tables in a SQL database to represent university buildings and rooms. It includes the SQL code to: 1) Create two tables with constraints - one for buildings and one for rooms 2) Insert data into the tables, showing an error when trying to insert an invalid capacity of 0 3) Add a foreign key constraint linking the building code between the two tables 4) Create a third table for courses with constraints
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views3 pages

Lab 1.2 PDF

The document describes creating and populating tables in a SQL database to represent university buildings and rooms. It includes the SQL code to: 1) Create two tables with constraints - one for buildings and one for rooms 2) Insert data into the tables, showing an error when trying to insert an invalid capacity of 0 3) Add a foreign key constraint linking the building code between the two tables 4) Create a third table for courses with constraints
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Using SQL, create a table called university_building, including all relevant constraints, using the
following specification:
a) A column named “code” of type varchar, with a maximum size of 2, which will be the primary
key
b) A column named “status” of type varchar, with a maximum size of 20
c) Status must not be NULL

CREATE TABLE university_building(


code VARCHAR(2),
status VARCHAR(20),
CONSTRAINT pk_university_building PRIMARY KEY (code),
CONSTRAINT check_status CHECK (status is NOT NULL));

2. Using SQL, create a table called building_room, including all relevant constraints, using the
following specification:
a) A column named “building_code” of type varchar, with a maximum size of 2
b) A column named “room_number” of type integer
c) A column named “capacity” of type integer
d) Both building_code and room_number form the composite primary key.
e) Capacity must be greater than 0

CREATE TABLE building_room(


building_code varchar(2),
room_number integer,
capacity integer,
CONSTRAINT pk_building_room PRIMARY KEY(building_code, room_number),
CONSTRAINT check_capacity CHECK(capacity > 0));

3. Try to insert the following data using SQL. Explain if there was any error, and why.
a) A building with code “TI” and status “active”
INSERT INTO university_building VALUES ('TI', 'active');

b) A building with code “MP” and status “in construction”


INSERT INTO university_building VALUES ('MP', 'in construction');

c) A room in building “TI”, with room number 2121, and capacity 1


INSERT INTO building_room VALUES ('TI', 2121, 1);

d) A room in building “MP”, with room number 1002, and capacity 60


INSERT INTO building_room VALUES ('MP', 1002, 60);

e) A room in building “MP”, with room number 1004, and capacity 0


INSERT INTO building_room VALUES ('MP', 10042, 0);
We get an error while inserting the data because capacity should not be 0.
4. Using the pgAdmin interface, add the following constraint:
a) A foreign key named fk_room_building, specifying that column building_code in table
building_room refers to column code in table university_building

b) Find out in the pgAdmin interface the corresponding SQL code for the constraint.
ALTER TABLE building_room
ADD CONSTRAING fk_room_building
FOREIGN KEY (building_code)
REFERENCES university_building(code);

c) Insert a new room in building “TD”, with room number 1005, and capacity 60. Do you get an
error? Why?
INSERT INTO building_room VALUES ('TD', 1005, 60);
We get an error because the building with code ‘TD’ is not present in the university_building
table.

d) Insert a new building TD (which is active) and try again


INSERT INTO building_room VALUES ('TD', 1005, 60);

5. Using SQL, create a new table courses in which to represent courses taught at a university.
Define at least three columns for the table. One of these columns must store the room in which
the course is taught. The definition must include a primary key, a foreign key and a check
constraint.
CREATE TABLE courses(
course_code varchar(7),
student_number integer,
room integer,
CONSTRAINT pk_courses PRIMARY KEY(course_code),
CONSTRAINT fk_courses FOREIGN KEY(room) REFERENCES
building_room(room_number),
CONSTRAINT check_student_number CHECK(student_number > 0));
6. (Optional) Try the following queries. Refer to the theory materials if necessary.
a) What is the total capacity of all rooms?
SELECT SUM(capacity)
FROM building_room;
The total capacity of all rooms is 121.

b) What is the total capacity of all rooms in building TI?


SELECT SUM(capacity)
FROM building_room
WHERE building_code = 'TI';
The total capacity of all rooms in building TI is 1.

c) List all the rooms with capacity greater than 50, ordering by capacity.
SELECT room_number, capacity
FROM building_room
WHERE capacity > 50
ORDER BY capacity DESC;

d) List all rooms in active buildings.


SELECT B.room_number FROM building_room B
JOIN university_building U ON U.code = B.building_code
Where U.status= 'active';

e) Update all buildings with status “in construction” to “not active”


UPDATE university_building
SET status='not active'
WHERE status='in construction';

You might also like