Tables, Normalization, Relationships Flashcards
table constraints
column attributes that restrict data
table constraints : PRIMARY KEY
a column whose values uniquely identify each row or particular entry in a table (for a table of persons a social security number would be a good primary key because they should be unique for each person)
-Primary Key columns must have a value in each row, and those values must be unique to the table
table constraints : FOREIGN KEY
a column in one table that references a column in another table
Composite key
If a group of columns are combined to uniquely identify a row, this is called a composite key.
-it is two or more columns acting together as a primary key
table constraints : UNIQUE KEY
a column whose values must be unique in each row.
-example: email addresses for people. not two people can have the same email addresses.
table constraints : NOT NULL
The column must have a value in each row
What is the difference between a primary key and a unique key?**
Primary key will not accept NULL or duplicate values whereas Unique key can accept one NULL value. A table can have only primary key whereas there can be multiple unique key on a table. A Clustered index automatically created when a primary key is defined whereas Unique key generates the non-clustered index.
table constraints : CHECK
the column values must meet some criteria
ex: a requirement that the ages of all rows in a table of people be above 0
What is the difference between a primary key and a foreign key?**
primary key is a column whose values uniquely identify each row or particular entry in a table
-no nulls or duplicates allowed
a foreign key is a column in one table that references a column in another table
Database normalization
Database normalization is the process of designing your database in such a way that
you have no composite columns,
little to no redundant data in a table, and that your data dependencies/relationships make sense.
There are as many as six or seven stages of normalization, but generally a database is acceptable if it reaches the 3rd stage, called Third Normal Form
Join Table**and what are the types?
a join table or an association table
A JOIN is a means for combining columns from one (self-join) or more tables by using values that are common to each. ANSI-standard SQL specifies five types of JOIN : INNER , LEFT OUTER , RIGHT OUTER , FULL OUTER and CROSS .
Normalization
-it is driven by business requirements
TCL : Transaction Control Language
-The process of making a change to your data permanent is called committing, and most databases have a feature called autocommit turned on by default
Transaction Control Language : COMMIT, SAVEPOINT, ROLLBACK
Use SAVEPOINT command to save a “snapshot” of your database at any point while executing a series of commands.
You can have multiple save points with different names, or you could “move” your existing save point by reusing it.When you realize that a mistake has been made, you can use the ROLLBACK TO command to revert any changes that have been made to a database since a specified save point.
You cannot roll back to a period before a save point was made, and you cannot rollback or undo a rollback action.
Additionally, you cannot rollback past a commit statement
Joins and Unions
-Joining two tables requires that each table have a column that tracks the same data.
(inner joins, left join, right join, outer join)
- Unions: a combination of two distinct select statements in a single result set.
- the first column of your first SELECT statement will be lined up with the first column of your second SELECT statement, and so on.For this reason, your UNION statement must obey two rules: First, the number of columns returned by both SELECT statements must be the same.Second, the data types of each column in the first SELECT statement must match the data types of each column in the second.