CREATE TABLE employees
: This command tells PostgreSQL to create a new table named employees
.employee_id SERIAL PRIMARY KEY
:
employee_id
is the name of the column.SERIAL
is a special data type that automatically generates a unique identifier for each row (an auto-incrementing integer).PRIMARY KEY
means this column uniquely identifies each row in the table.first_name VARCHAR(50)
:
first_name
is the name of the column.VARCHAR(50)
specifies that this column can store up to 50 characters.last_name VARCHAR(50)
and email VARCHAR(100)
follow the same pattern.ALTER TABLE
statement.
ALTER TABLE employees
: This command specifies the table (employees
) to be modified.ADD COLUMN date_of_birth DATE
:
ADD COLUMN
specifies that a new column is being added.date_of_birth
is the name of the new column.DATE
is the data type for the new column, which stores date values.ALTER TABLE
statement:
ADD PRIMARY KEY (employee_id)
:
ADD PRIMARY KEY
specifies the addition of a primary key.(employee_id)
indicates that the employee_id
column is being set as the primary key.departments
table:
employees
Table:department_id
column to the employees
table:ALTER TABLE employees
: This command specifies the table (employees
) to be modified.ADD COLUMN department_id INTEGER
:
ADD COLUMN
specifies that a new column is being added.department_id
is the name of the new column.INTEGER
is the data type for the new column, which stores integer values.ADD CONSTRAINT fk_department FOREIGN KEY (department_id) REFERENCES departments(department_id)
:
ADD CONSTRAINT fk_department
names the new constraint fk_department
.FOREIGN KEY (department_id)
specifies that the department_id
column in the employees
table is a foreign key.REFERENCES departments(department_id)
indicates that this foreign key references the department_id
column in the departments
table.departments
and employees
, with the employees
table having a foreign key that references the departments
table. Each table and column is defined with appropriate data types and constraints to ensure data integrity and relationships.