CREATE TABLE
defines a new table
CREATE TABLE Students(
sid INT PRIMARY KEY,
cnp CHAR(13),
lastname VARCHAR(50),
firstname VARCHAR(50) DEFAULT 'TBA',
age INT
CHECK (age >= 18)
)
restrictions associated with a column:
restrictions associated with a table:
possible actions for a foreign key:
CREATE TABLE Programs
(id SMALLINT PRIMARY KEY,
name VARCHAR(70))
CREATE TABLE Students
(program SMALLINT REFERENCES Programs(id),
reg_number CHAR(10),
last_name CHAR(30),
first_name CHAR(30),
cnp CHAR(13) UNIQUE,
PRIMARY KEY(program, reg_number))
CREATE TABLE Groups
(acad_year SMALLINT,
program SMALLINT REFERENCES Programs(id),
year_of_study SMALLINT,
id CHAR(10),
PRIMARY KEY (acad_year, program, id))
CREATE TABLE Courses
(id CHAR(10) PRIMARY KEY,
name VARCHAR(70))
CREATE TABLE Trajectory
(id INT PRIMARY KEY IDENTITY(1,1),
acad_year SMALLINT,
program SMALLINT,
reg_number CHAR(10),
sgroup CHAR(10),
FOREIGN KEY(program, reg_number) REFERENCES
Students(program, reg_number),
FOREIGN KEY(acad_year, program, sgroup)
REFERENCES Groups(acad_year, program, id))
CREATE TABLE Results
(id INT PRIMARY KEY IDENTITY(1,1),
program SMALLINT,
reg_number CHAR(10),
acad_year SMALLINT,
semester SMALLINT,
course CHAR(10) REFERENCES Courses(id),
no_credits DECIMAL(3,1),
grade SMALLINT,
FOREIGN KEY(program, reg_number) REFERENCES
Students(program, reg_number))
CREATE TABLE Universities
(UnivID SMALLINT PRIMARY KEY IDENTITY(1,1),
UnivName NVARCHAR(100) NOT NULL,
Country NVARCHAR(40))
CREATE TABLE ResearchTeams
(RTID SMALLINT IDENTITY(1,1),
RTName NVARCHAR(100) UNIQUE,
CONSTRAINT PK_ResearchTeams PRIMARY KEY(RTID))
CREATE TABLE ResearchTeams_Universities
(RTID SMALLINT FOREIGN KEY REFERENCES
ResearchTeams(RTID) ,
UnivID SMALLINT REFERENCES Universities(UnivID),
PRIMARY KEY(RTID, UnivID))
CREATE TABLE SensorTypes
(STID TINYINT PRIMARY KEY IDENTITY(1,1),
STName NVARCHAR(50) UNIQUE,
STBriefDescription NVARCHAR(300) DEFAULT 'TBW')
CREATE TABLE Birds
(BID INT PRIMARY KEY IDENTITY(1,1),
BNickName NVARCHAR(50),
Species VARCHAR(100),
RTID SMALLINT,
CONSTRAINT FK_Birds_ResearchTeams FOREIGN KEY(RTID)
REFERENCES ResearchTeams(RTID))
CREATE TABLE Sensors
(SensorID INT PRIMARY KEY IDENTITY(1,1),
STID TINYINT REFERENCES SensorTypes ON DELETE NO ACTION
ON UPDATE CASCADE,
BirdID INT REFERENCES Birds(BID))
CREATE TABLE SensorData
(SDID INT PRIMARY KEY IDENTITY(1,1),
SensorID INT REFERENCES Sensors(SensorID),
SensorDataValue REAL,
SensorDataTime DATETIME2)
CREATE TABLE Researchers
(RID SMALLINT PRIMARY KEY IDENTITY(1,1),
RFName NVARCHAR(90) NOT NULL,
RLName NVARCHAR(90) NOT NULL,
RTID SMALLINT REFERENCES ResearchTeams(RTID))
CREATE TABLE PublishedPapers
(PID INT PRIMARY KEY IDENTITY(1,1),
Title NVARCHAR(200),
Conference NVARCHAR(200))
CREATE TABLE Researchers_PublishedPapers
(RID SMALLINT REFERENCES Researchers(RID) ,
PID INT REFERENCES PublishedPapers(PID),
PRIMARY KEY(RID, PID))
ALTER TABLE
changes the structure of a defined table
ALTER TABLE Students
ADD FavSymphony VARCHAR(50)
possible operations (differences among DBMSs)
DROP TABLE
destroys a tablE
DROP TABLE table_name
DROP TABLE Students
INSERT
adding records
INSERT INTO table_name[(column_list)] VALUES (value_list)
INSERT INTO table_name[(column_list)] subquery, -- where subquery refers to a set of records (generated with the SELECT statement)
INSERT INTO Students (sid, cnp, lastname, firstname, age)
VALUES (1, '123456789012', 'Popescu', 'Maria', 20)
UPDATE
changing records
the command changes the records in the table that satisfy the condition in the WHERE clause
if the WHERE clause is omitted, all the records in the table are changed; the values of the columns specified in SET are changed to the associated expressions' values
UPDATE table_name
SET column_name = expression [, column_name=expression] ...
[WHERE condition]
UPDATE Students
SET age = age + 1
WHERE cnp = '123456789012'
DELETE
removing records
the command deletes the records in the table that satisfy the condition in the WHERE clause
if the WHERE clause is omitted, all the table's records are deleted
DELETE FROM table_name
[WHERE condition]
DELETE
FROM Students
WHERE lastname = 'Popescu'
[NOT] BETWEEN valmin AND valmax
[NOT] LIKE pattern ("%" - any substring, "_" - one character)
IS [NOT] NULL
[NOT] IN (value [, value] ...)
[NOT] IN (subquery)
[NOT] EXISTS (subquery)
NOT
conditionAND
condition2OR
condition2truth values: true, false, unknown
SELECT
StatementStudents[sid, sname, email, age, sgroup]
Courses[cid, cname, credits]
Exams[sid, cid, grade]
-- Q1. Find the 21-year-old students in the Students table.
SELECT *
FROM Students S
WHERE S.age = 21
-- Q2. Find the names and email addresses of all the 21-year-old students in the Students table. Eliminate duplicates.
SELECT DISTINCT S.sname, S.email
FROM Students S
WHERE S.age = 21
FROM
clause (to solve the ambiguity)Pirates [PID, Name, Reputation, β¦ ]
Treasures [TID, Name, Description, Category, β¦ ]
Shares [PID, TID, Value]
SELECT *
FROM Pirates P, Shares AS S
WHERE P.PID == S.PID AND S.Value > 10
SELECT * -- Name, Reputation
FROM Pirates P
WHERE P.reputation > 1 AND P.reputation < 4
-- Q3. Find the "10" grades (student name, course id).
SELECT S.sname, E.cid
FROM Students S, Exams E
WHERE S.sid = E.sid AND E.grade = 10
SELECT sname, cid
FROM Students, Exams
WHERE Students.sid = Exams.sid AND grade = 10
LIKE
operatorAS
and =
can be used to name fields in the result setLIKE
operator is used for string pattern matching:-- Q4. For all students whose name starts and ends with B and has at least 3 characters, retrieve the following data: student age, student age β 18, student age * 2.
SELECT S.age, age1 = S.age-18, 2*S.age AS age2
FROM Students S
WHERE S.sname LIKE 'B_%B'
SELECT *
FROM Pirates AS P
WHERE P.Name LIKE '%_ _ _ escu'
SELECT P.PID, Pension = P.Reputation * 10 + 5 -- must be scaler
FROM Pirates AS P
WHERE P.Name LIKE '%_ _ _ escu'
UNION
(or)UNION ALL
doesn't eliminate duplicates-- Q5. Find the ids of students who are older than 20 or have a grade in the Alg1 course.
SELECT S.sid
FROM Students S
WHERE S.age > 20
UNION
SELECT E.sid
FROM Exams E
WHERE E.cid = 'Alg1'
--UNION ALL doesn't eliminate duplicates
-- Names of the pirates that have shares in treasure 1 or treasure 2
SELECT P.Name
FROM Pirates P, Shares S, Treasures T
WHERE P.PID == S.PID AND T.TID == S.TID AND T.Category = 1
UNION
SELECT P2.Name
FROM Pirates P2, Shares S2, Treasures T2
WHERE P2.PID == S2.PID AND T2.TID == S2.TID AND T2.Category = 2
INTERSECT
(and)-- Q6. Find the ids of students who received a grade in both a 4 credits course and a 5 credits course.
SELECT E.sid
FROM Exams E, Courses C
WHERE E.cid = C.cid AND C.credits = 4
INTERSECT
SELECT E2.sid
FROM Exams E2, Courses C2
WHERE E2.cid = C2.cid AND C2.credits = 5
-- Names of the pirates that have shares in treasure 1 or treasure 2
SELECT P.Name
FROM Pirates P, Shares S, Treasures T
WHERE P.PID == S.PID AND T.TID == S.TID AND T.Category = 1
INTERSECT
SELECT P2.Name
FROM Pirates P2, Shares S2, Treasures T2
WHERE P2.PID == S2.PID AND T2.TID == S2.TID AND T2.Category = 2
EXCEPT
(but)-- Q7. Find the ids of students who received a grade in a 4 credits course, but have no grades in 5 credits courses.
SELECT E.sid
FROM Exams E, Courses C
WHERE E.cid = C.cid AND C.credits = 4
EXCEPT
SELECT E2.sid
FROM Exams E2, Courses C2
WHERE E2.cid = C2.cid AND C2.credits = 5