Purpose:
You may make a copy of a worksheet and complete this activity, or simply type your answers in any text editor.
You may work alone or with at most 2 other students (max size=3) in this course.
Answer the following questions / complete the following tasks.
(a) value in column is a string (b) value in column is not null (c) value in column is unique in that column of the table (d) value in column is the key of a tuple in the table (e) value in column is a positive integer (f) values in columns are different (g) date values in column is not in the past (h) all of the above (i) none of the above
Users(uid, uname, city) Picture(pid, author, size, pdf) Users stores all users; uid is the key. Picture stores their pictures; pid is the key; author is the uid of the picture's author; size represents the size of the picture in bytes; pdf is the actual pdf content of the picture. uid, pid, author, size are integers; uname, city, pdf are text.
create table Users ( uid int, uname text not null, city text not null, primary key(uid) ); create table Picture ( pid int, author int not null references Users(uid), size int not null, pdf text, primary key(pid) ); Alternatively, for Picture, we can specify foreign key (author) references Users(uid)
ALTER TABLE Picture ADD CONSTRAINT checkSize CHECK (size <= 3000000);
insert into Users values(1, 'Minnie', 'Charlottesville'); insert into Users values(2, 'Mickey', 'Charlottesville'); insert into Users values(3, 'Goofy', 'Richmond'); insert into Users values(4, 'Daisy', 'Charlottesville'); insert into Users values(5, 'Donald', 'Fairfax'); insert into Picture values(10, 1, 1500000, 'some pdf abc content'); insert into Picture values(20, 1, 1500000, 'some pdf def content'); insert into Picture values(30, 2, 1500000, 'some pdf ghi content'); insert into Picture values(40, 1, 1500000, 'some pdf jkl content'); insert into Picture values(50, 2, 1500000, 'some pdf mno content'); insert into Picture values(60, 5, 500000, 'some pdf pqr content'); insert into Picture values(70, 5, 3000000, 'some pdf stu content'); insert into Picture values(80, 5, 4000000, 'some pdf vwx content'); -- this should not be added due to checkSize Note: the order of insertion -- insert into the Users table before the Product table
select U.uid, U.uname from Users U, Picture P1, Picture P2 where U.uid = P1.author and U.uid = P2.author and P1.size > 1000000 and P2.size <= 1000000;
select U.uid, U.uname from Users U where not exists (select * from Picture P where P.size > 1000000 and U.uid = P.author); -- OR -- select U.uid, U.uname from Users U where U.uid not IN (select P.author from Picture P where P.size > 1000000); -- OR -- select U.uid, U.uname from Users U where 1000000 > ALL (select P.size from Picture P where P.author = U.uid);
Boats (bid, bname, color) Sailors (sid, sname, rating, age) Reserves (sid, bid, day)
To test your SQL, you should insert some data. The following SQL commands are provided for your convenience.
INSERT INTO Reserves VALUES (22, 102, '2023-03-13'); INSERT INTO Reserves VALUES (22, 103, '2023-03-13'); INSERT INTO Reserves VALUES (22, 105, '2023-03-13');
For each sailer, there is no boats that this sailor has not reserved (note: double negation) SELECT sname FROM Sailors WHERE NOT EXISTS (SELECT bid FROM Boats WHERE NOT EXISTS (SELECT Reserves.bid FROM Reserves WHERE Reserves.bid = Boats.bid AND Reserves.sid = Sailors.sid) );
Released under the CC-BY-NC-SA 4.0 license.
Last updated 2024-02-27 20:49