Lesson 13
A relational database consists of multiple interlinked tables holding specific data. Defining tables appropriately is crucial for robust data analysis. SQL provides a flexible CREATE TABLE statement for this. This lesson covers its syntax, components like columns and constraints as well as common data types for modeling table schemas effectively.
Tables are created using:
CREATE TABLE table_name (
column1 data_type,
column2 data_type,
...
);
Constraints and defaults can be defined during creation for concise schemas.
The IF NOT EXISTS clause tells SQL to only create the table if it does not already exist. If the table does exist, then no action will be taken and no error will occur.
Here is an example of using the IF NOT EXISTS clause when creating a table in SQL:
CREATE TABLE IF NOT EXISTS customers (
id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100)
);
Some key points about using IF NOT EXISTS:
For example, you may have a database restore script that truncates and recreates tables with test data. Using IF NOT EXISTS handles cases where the tables may or may not be there already without failing.
It also handles situations where multiple developers are running the same table create scripts, avoiding duplication errors.
The check is only on the table name. The same CREATE TABLE IF NOT EXISTS statement can be used to create a table with the same name but different column definitions without issue.
Key components while defining schemas:
Column Names: Descriptive identifiers like user_id, amount_due
Data Types: Constrain stored values like INTEGER, VARCHAR, DATE
Constraints: Restrictions like NOT NULL, UNIQUE and FOREIGN KEYS
Default Values: Pre-populated column values if unspecified
Appropriate schema definition ensures data integrity.
Common types include:
Numerical
Strings
Date/Time
Large Objects
Each database defines additional types like monetary, geospatial etc. For more information about these types visit Data types.
Constraints ensure data validity:
A primary key is a special relational database constraint that uniquely identifies each record in a table.
The main characteristics of a primary key are:
A table can only have one primary key. The primary key can consist of a single column or multiple columns (composite key).
Some examples:
Single column primary key
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(50)
);
Here the id column is the primary key.
Multi-column primary key
CREATE TABLE employee (
emp_id INT,
department_id INT,
PRIMARY KEY (emp_id, department_id)
);
Here the combination of emp_id and department_id forms the primary key.
Primary keys are an important concept in the relational model. They allow uniquely identifying tuples/rows in a table and form the basis for relationships between tables.
A foreign key in a database table is a field that links to the primary key of another table. It helps establish relationships between tables and enforce referential integrity.
The main features of a foreign key:
For example:
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(id)
);
Here customer_id
in orders table is a foreign key referring to the id
primary key in the customers
table.
This ensures:
customer_id
matches id
in customers
customer_id
to exist in customers
The foreign key establishes a defined relationship between the two tables promoting discipline and integrity across records. Enforcing foreign key constraints is vital for accurate interconnected data.
Carefully defining constraints prevents invalid data insertion.