9. Deciphering a PostgreSQL statement
In this post, we try to understand each line of a simple PostgreSQL statement.
What is PostgreSQL?
Amazon Web Services (AWS) has a good explanation: “PostgreSQL is an advanced, enterprise-class open-source relational database that supports both SQL (relational) and JSON (non-relational) querying. It is a highly stable database management system backed by more than 20 years of community development.
Read more here: What is PostgreSQL?.
The official site is here: postgresql.org
So, let’s look at the statement in question:
CREATE TABLE HOUR_ASSIGNMENTS(
hour_assignment_id serial PRIMARY KEY,
project_id integer NOT NULL,
employee_id integer NOT NULL,
hours decimal (6,2) NOT NULL CHECK (hours > 0),
FOREIGN KEY(project_id) REFERENCES PROJECTS(proj_id) ON DELETE CASCADE,
FOREIGN KEY(employee_id) REFERENCES EMPLOYEES(emp_id) ON DELETE CASCADE);
Let’s break down each line of the SQL statement:
CREATE TABLE HOUR_ASSIGNMENTS(:- This begins the creation of a new table named
HOUR_ASSIGNMENTS.
- This begins the creation of a new table named
hour_assignment_id serial PRIMARY KEY,:- This defines a column named
hour_assignment_idas the primary key for the table. Theserialdata type is typically used for auto-incrementing integer columns in PostgreSQL databases, meaning each new row inserted into the table will automatically generate a unique value for this column.
- This defines a column named
project_id integer NOT NULL,:- This defines a column named
project_idas an integer type, which cannot contain NULL values.
- This defines a column named
employee_id integer NOT NULL,:- This defines a column named
employee_idas an integer type, which cannot contain NULL values.
- This defines a column named
hours decimal (6,2) NOT NULL CHECK (hours > 0),:- This defines a column named
hoursas a decimal type with a precision of 6 digits and a scale of 2 (allowing for values with up to 4 digits before the decimal point and 2 digits after). TheNOT NULLconstraint ensures that this column cannot contain NULL values. TheCHECKconstraint ensures that the value ofhoursmust be greater than 0.
- This defines a column named
FOREIGN KEY(project_id) REFERENCES PROJECTS(proj_id) ON DELETE CASCADE,:- This creates a foreign key constraint on the
project_idcolumn, referencing theproj_idcolumn in thePROJECTStable. TheON DELETE CASCADEoption specifies that if a row in thePROJECTStable is deleted, all corresponding rows in theHOUR_ASSIGNMENTStable with matchingproject_idvalues will also be deleted.
- This creates a foreign key constraint on the
FOREIGN KEY(employee_id) REFERENCES EMPLOYEES(emp_id) ON DELETE CASCADE);:- This creates a foreign key constraint on the
employee_idcolumn, referencing theemp_idcolumn in theEMPLOYEEStable. Similarly, theON DELETE CASCADEoption specifies that if a row in theEMPLOYEEStable is deleted, all corresponding rows in theHOUR_ASSIGNMENTStable with matchingemployee_idvalues will also be deleted.
- This creates a foreign key constraint on the
In summary, this SQL statement creates a table named HOUR_ASSIGNMENTS with columns for assignment IDs, project IDs, employee IDs, and hours worked. It includes constraints to ensure data integrity, such as not allowing NULL values for certain columns and enforcing referential integrity between this table and other tables (PROJECTS and EMPLOYEES) through foreign key constraints with cascading delete behavior.
postgresql
]