SQL databases Flashcards
What does Second Normal Form mean?
Non-key attributes are dependent on the primary key
What does First Normal Form mean?
All data values in the data are atomic
There are no repeating columns
What does Third Normal Form mean?
Non-key attributes are non-transitively dependent on primary key
Define a table in a relational database
a set of data elements organised by columns and rows
Define a record in a relational database
a row of data in a table consisting of a value from each column in the table
Arrange the following SQL keywords in the correct syntax order
ON SELECT WHERE JOIN ORDER BY GROUP BY FROM
SELECT FROM JOIN ON WHERE GROUP BY ORDER BY
What are the five supported data types in SQLite
NULL INTEGER REAL TEXT BLOB
How are booleans represented in SQLite?
SQLite represents booleans as integers (0 = False, 1 = True)
When is a junction table required?
Junction tables are needed to represent many-to-many relationships in a relational database
Write SQL code to create a junction table to join the following two tables:
Student (studentID, class, …)
CCA(ccaID, teacher_in_charge, …)
studentID and ccaID are the PKs of the tables.
Assume the PK of the junction table is a composite key and PKs are INTEGERs
CREATE TABLE StudentCCA (
studentID INTEGER,
ccaID INTEGER,
PRIMARY KEY (studentID, ccaID),
FOREIGN KEY (studentID) REFERENCES Student(studentID),
FOREIGN KEY (ccaID) REFERENCES CCA(ccaID)
)
How should quotes be used in SQL?
Double-quotes (“) for identifiers (table and column names)
Single-quotes (‘) for literals (strings, values to match in a WHERE clause, etc)
What is the syntax to create an SQL table?
CREATE TABLE ( Col1 TYPE, Col2 TYPE, ..., PRIMARY KEY (Col1) )
The following SQL code raises an error. Why?
CREATE TABLE Student ( ID INTEGER, Name TEXT, Class TEXT, PRIMARY KEY (ID), );
Last line (PRIMARY KEY) should not have comma
The following SQL code raises an error. Why?
CREATE TABLE 'Student' ( 'ID' INTEGER, 'Name' TEXT, 'Class' TEXT, PRIMARY KEY ('ID') );
Table and column names are identifiers and should be double-quoted (“), not single-quoted (‘)
What is a primary key? State its properties.
A primary key is a candidate key that is most appropriate to become the main key for a table.
Uniquely identifies each record in a table
Should not change over time
What is a composite key? State its properties.
A composite key is a combination of two or more fields in a table that can be used to uniquely identify each record in a table
Uniqueness is only guaranteed when the fields are combined
When taken individually, the fields do not guarantee uniqueness
What is a foreign key?
A foreign key is a field in one table that refers to the primary key in another table.
When is a variable Y functionally dependent on a variable X?
Y is functionally dependent on X if for every valid instance of X, the value of X uniquely determines the value of Y.
i.e one-one relationship.
What is data redundancy?
Data redundancy refers to the same data being stored more than once