Week 3: Advanced SQL and Data Integration Flashcards
REVERSED
CREATE TABLE Students(sid CHAR(20), name CHAR(20), login CHAR(10), age INTEGER, gpa REAL)
How do you create a table in SQL?
REVERSED
DROP TABLE Students
How do you delete an entire table including schema in SQL?
REVERSED
DELETE FROM Students
How do you delete the contents of a table but keep the relation in SQL?
REVERSED
DELETE FROM Students
WHERE …
How do you delete certain rows from a table in SQL?
REVERSED
ALTER TABLE Students
ADD COLUMN firstYear integer
How do you add an empty column to an SQL table?
REVERSED
ALTER TABLE Students
DROP firstYear
How do you delete an attribute from an SQL table?
REVERSED
INSERT INTO Students
VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2)
INSERT INTO Students(name, Sid, login, age, gpa)
VALUES (‘Smith’, 53688, ‘smith@ee’, 18, 3.2)
How do you add a row into an SQL table? (2 ways depending on the order)
REVERSED
UPDATE Students
SET grade = grade*1.2
WHERE …
How do you change the value in an attribute for all or some tuples?
REVERSED
CREATE TABLE test (sid INTEGER PRIMARY KEY, name VARCHAR(30), major VARCHAR(30))
CREATE TABLE test (sid INTEGER, name VARCHAR(30), major VARCHAR(30), PRIMARY KEY (sid))
How do you set a primary key when creating a table in SQL? (2 ways)
REVERSED
CREATE TABLE Works_In(ssn CHAR(11), did INTEGER, since DATE,
PRIMARY KEY (ssn, did),
FOREIGN KEY (ssn)
REFERENCES Employees)
How do you set a foreign key when creating a table in SQL?
REVERSED
organise a group of entity sets into a parent/child hierarchy
What are entity hierarchies?
REVERSED
specify that the children of an entity do/don’t overlap
What are overlap constraints?
REVERSED
instances of the children of an entity include all instances of their parent (ie cover it)
What are covering constraints?
REVERSED
A view is just a relation, but we store a definition, rather than a set of tuples
Views can be used to present necessary information (or a summary) while hiding details in underlying relations
What is a view in SQL and what is it used for?
REVERSED
CREATE VIEW YoungActiveStudents (name, grade)
AS SELECT S.name, E.grade
FROM Students S, Enrolled E
Where S.Sid = E.Sid and S.age <21
How do you create a view in SQL?
REVERSED
DROP VIEW
How can you delete a view in SQL?
REVERSED
a constraint is a relationship among data elements that the DBMS is required to enforce
What is an integrity constraint?
REVERSED
Integrity constraints guard against accidental damage to the database, by ensuring that authorised changes do not result in a loss of data consistency
What is the purpose of integrity constraints?
REVERSED
one that satisfies all specified ICs
What is a legal instance of a relation?
REVERSED
- Primary key
- Foreign key (referential integrity)
- Value-based
- Tuple-based
What are the 4 types of integrity constraints?
REVERSED
Means the primary key cannot be null
What is entity integrity?
REVERSED
a key that could be the primary key but is not classified as the primary key
What is an alternate key?
REVERSED
- NO ACTION - delete/update is rejected
- CASCADE - make the same changes to all tuples that refer to the updated/deleted tuple
- SET NULL / SET DEFAULT - sets foreign key value of referencing tuple to NULL or a default value
What are the three ways of enforcing referential integrity in SQL?
REVERSED
CREATE TABLE Enrolled(sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY(sid,cid), FOREIGN KEY(sid) REFERNCES Students
ON DELETE CASCADE
ON UPDATE SET DEFAULT)
How do you set the method of enforcing referential integrity when creating an SQL table?
REVERSED
it will store the three characters followed by 2 spaces
What happens if you enter 3 characters into an attribute with CHAR(5)?
REVERSED
defines constraints on the values of a particular attributes
What is the check clause?
REVERSED
CREATE TABLE section( semester VARCHAR(6) CHECK(semester IN (‘Fall’, ‘Winter’, ‘Spring’, ‘Summer’)), year NUMERIC(4,0) CHECK(year>1990))
How do you add a check into an SQL table?
REVERSED
CHECK(semester IN (‘Fall’, ‘Winter’, ‘Spring’, ‘Summer’) AND (year>1990))
What is a tuple based check?
REVERSED
CHECK(time_slot_id IN (SELECT time_slot_id FROM time_slot))
What is a complex check clause?