SQL Databases Flashcards
What does First Normal Form mean?
All data values in the data are atomic
There are no repeating columns
What does Second Normal Form mean?
Non-key attributes are fully dependent on the primary key.
(If a non-key attribute is dependent on some but not all of the columns in a composite primary key, the table violates 2NF.)
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 organized into columns and rows
Define a record in a relational database
a row of data in a table consisting of single value in 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, …)
Assume the first column is the PK 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 (‘)