Week 5 Flashcards
How do you create a database schema?
CREATE SCHEMA Company;
Where Company is the schema name
What are examples of attribute domains?
INT REAL or DECIMAL(n,m) where m is the number of decimal place. CHAR(n), where n is the number of chars VARCHAR(5): variable length from 0 to 5. BIT(n) BIT VARYING(n) Boolean (TRUE/FALSE/UNKOWN) Date: YYYY-MM-DD TIMESTAMP: HH:MM:SS
What are different types of value & domain constraints?
- Constraint on the default value of an attribute.
DEFAULT{value}
or
NOT NULL
2.Domain range constraint that checks an attribute.
CHECK
eg., DNumber INT NOT NULL CHECK (Dnumber >0 AND Dnumber<21);
How do you declare a primary key?
Name; Type; NOT NULL
PRIMARYKEY (Name)
Eg.,Dnumber INT NOT NULL PRIMARY KEY(Dnumber)
how do you declare the candidate key?
CKName, Type, NOT NULL,
UNIQUE(Name)
Dname VARCHAR(15) NOT NULL UNIQUE(Dname);
How do you declare the foreign key in a relation?
FOREIGN KEY(FKName) REFERENCES (PKName)
Eg., FOREIGN KEY(Super_SSN) REFERENCES Employee(SSN)
PK: SSN
FK: Super_SSN
Relation: Employee
What do you do when a tuple is deleted or updated?
ON UPDATE CASCADE updates all of the foreign keys that refer to the PK tuple
Similarly, ON DELETE CASCADE
What are constraint labels and why do we use them?
CONSTRAINT
PRIMARYKEY(PKname)
CONSTRAINT EMPPK PRIMARY KEY (Ssn)
EMPPK is the label in this case. It’s used for more control and to mitigate mistakes of things being deleted accidentally)
What happens to the foreign key if the primary key is deleted?
Often in the relation of the PK, a default value is defined.
We can say ON DELETE SET DEFAULT.
We must also say ON UPDATE CASCADE (so that FK are updated when PK are modified)
However, we don’t always want to CASCADE so we might:
SET DEFAULT
NOT NULL
Here, the database analyst is making sure that when something is updated/deleted it isn’t transferred to an inconsistent state.
Retrieve the name and address of all employees who work for the ‘Research’ department.
SELECT Fname, Lname, Address
FROM EMPLOYEE, DEPARTMENT
WHERE Dname = ‘Research’ AND Dnumber = Dno;
- Dname = ‘Research’ is a selection condition
- Dnumber = Dno; is a join condition
For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s birth date.
SELECT Pnumber, Dnum, Lname, Address, Bdate
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum = Dnumber AND Mgr_ssn = Ssn AND Plocation = ‘Stafford
What is the AS operator used for?
The AS command is used to rename a column or table with an alias.
Eg., SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;
For each employee, retrieve the employee’s first and last name and the first and last name of their supervisor.
SELECT E.Fname, E.Lname, S.Fname, S.Lname
FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.Super_ssn=S.Ssn;
When can u use an asterisk (*)?
To list all of the attributes in the SELECT clause
eg.,
SELECT *
FROM EMPLOYEE
WHERE Dno=5
What is a set?
only unique elements, e.g., S = {a, b, c}