| Lesson 13 | Using SQL to Create Tables |
| Objective | Explain how to use SQL to create Database Tables |
CREATE TABLE TableName
( Column01Name Column01DataType Column01Constraints,
Column02Name Column02DataType Column02Constraints,
...
PRIMARY KEY (ColumnXX),
FOREIGN KEY (ColumnYY) REFERENCES OtherTable(OtherColumn)
);
VARCHAR, DATE, INTEGER, or DECIMAL.NOT NULL, DEFAULT, CHECK, UNIQUE, or foreign keys.Modern DBMS systems often support schema-qualified names. For example:
CREATE TABLE COMPANY.EMPLOYEE (
...
);
This explicitly places the table in the COMPANY schema. Without qualification:
CREATE TABLE EMPLOYEE (
...
);
the table is created in the active schema selected by the user.
CREATE TABLE are physical relations stored on disk. These are distinct from virtual tables (views), which may not correspond to a physical file.
SQL enforces entity integrity (primary keys), referential integrity (foreign keys), and domain constraints (data types and limits). However, care must be taken when declaring constraints that reference tables not yet created. For example:
To avoid circular references or missing dependencies, you can delay some foreign key declarations and add them afterward via ALTER TABLE.
CREATE TABLE EMPLOYEE
( Fname VARCHAR(15) NOT NULL,
Minit CHAR(1),
Lname VARCHAR(15) NOT NULL,
Ssn CHAR(9) NOT NULL,
Bdate DATE,
Address VARCHAR(30),
Sex CHAR(1),
Salary DECIMAL(10,2),
Super_ssn CHAR(9),
Dno INT NOT NULL,
PRIMARY KEY (Ssn),
FOREIGN KEY (Super_ssn)
REFERENCES EMPLOYEE(Ssn),
FOREIGN KEY (Dno)
REFERENCES DEPARTMENT(Dnumber)
);
CREATE TABLE DEPARTMENT
( Dname VARCHAR(15) NOT NULL,
Dnumber INT NOT NULL,
Mgr_ssn CHAR(9) NOT NULL,
Mgr_start_date DATE,
PRIMARY KEY (Dnumber),
UNIQUE (Dname),
FOREIGN KEY (Mgr_ssn)
REFERENCES EMPLOYEE(Ssn)
);
These examples demonstrate fundamental SQL DDL (Data Definition Language) concepts that apply across modern enterprise DBMS platforms.