Week 5 Flashcards

1
Q

How do you create a database schema?

A

CREATE SCHEMA Company;

Where Company is the schema name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are examples of attribute domains?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are different types of value & domain constraints?

A
  1. 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you declare a primary key?

A

Name; Type; NOT NULL
PRIMARYKEY (Name)

Eg.,Dnumber INT NOT NULL
PRIMARY KEY(Dnumber)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how do you declare the candidate key?

A

CKName, Type, NOT NULL,
UNIQUE(Name)

Dname VARCHAR(15) NOT NULL
UNIQUE(Dname);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you declare the foreign key in a relation?

A

FOREIGN KEY(FKName) REFERENCES (PKName)

Eg.,
FOREIGN KEY(Super_SSN) REFERENCES Employee(SSN)

PK: SSN
FK: Super_SSN
Relation: Employee

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What do you do when a tuple is deleted or updated?

A

ON UPDATE CASCADE updates all of the foreign keys that refer to the PK tuple
Similarly, ON DELETE CASCADE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are constraint labels and why do we use them?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens to the foreign key if the primary key is deleted?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Retrieve the name and address of all employees who work for the ‘Research’ department.

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s birth date.

A

SELECT Pnumber, Dnum, Lname, Address, Bdate
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum = Dnumber AND Mgr_ssn = Ssn AND Plocation = ‘Stafford

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the AS operator used for?

A

The AS command is used to rename a column or table with an alias.

Eg., SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

For each employee, retrieve the employee’s first and last name and the first and last name of their supervisor.

A

SELECT E.Fname, E.Lname, S.Fname, S.Lname
FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.Super_ssn=S.Ssn;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When can u use an asterisk (*)?

A

To list all of the attributes in the SELECT clause

eg.,
SELECT *
FROM EMPLOYEE
WHERE Dno=5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a set?

A

only unique elements, e.g., S = {a, b, c}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a mutliset?

A

might have duplicates, e.g., M = {a, a, a, b, c, c}

17
Q

What are operators?

A

UNION, EXCEPT, INTERSECT

18
Q

How do you retrieve all of the distinct values of an attribute?

A

SELECT DISTINCT

eg.,,
SELECT DISTINCT Salary
FROM EMPLOYEE

19
Q

How do you use the UNION operator?

A

split this into two sub-queries and then use the set UNION operator over the partial results.