Constraints
Constraints
Postgres SQL
Installing Postgres SQL
Go to link below and download the Windows Installer
https://www.postgresql.org/download/windows/
Create a table
CREATE TABLE users ( id SERIAL PRIMARY KEY, name VARCHAR(50), email VARCHAR(50),
age INTEGER);
To change the name of a column in a table, we can use the ALTER TABLE
statement with the RENAME COLUMN option. For example, to change the name
of the email column to user_email, we can use the following query:
This will rename the email column to user_email in the users table.
Alter Query
Adding a new column to a table
To add a new column to a table, we can use the ALTER TABLE statement with the
ADD COLUMN option. For example, to add a new column named phone of data
type VARCHAR(20) to the users table, we can use the following query:
To modify the data type of an existing column in a table, we can use the ALTER
TABLE statement with the ALTER COLUMN option. For example, to change the
data type of the age column from INTEGER to SMALLINT, we can use the
following query:
This will change the data type of the age column in the users table from INTEGER
to SMALLINT
Alter Query
Deleting a column from a table
To delete a column from a table, we can use the ALTER TABLE statement with the
DROP COLUMN option. For example, to delete the phone column from the users
table, we can use the following query:
This will delete the phone column from the users table.
Database Constraints in PostgreSQL
Database constraints are rules that restrict the values that can be inserted,
updated, or deleted in a database table.
Example:
Suppose we have a table called employees with columns employee_id, employee_name, and employee_salary.
We want to ensure that a value is always provided for the employee_name" and employee_salary columns.
We can add a NOT NULL constraint to these columns using the following SQL command:
Now, any attempt to insert a row into the "employees" table without providing a value for the "employee_name" or
"employee_salary" column will result in an error.
UNIQUE Constraint
The UNIQUE constraint ensures that the values in a column are unique across all rows in the table. This means that no
two rows can have the same value in the column with the UNIQUE constraint.
Example:
Suppose we have a table called students with columns student_id and email. We want to ensure that each student has a
unique email address. We can add a UNIQUE constraint to the email column using the following SQL command:
Now, any attempt to insert a row into the "students" table with a duplicate email address will result in an error.
PRIMARY KEY Constraint
The PRIMARY KEY constraint is a combination of a UNIQUE constraint and a NOT NULL constraint. It ensures that the
values in a column are unique across all rows in the table, and that the column cannot have a null value.
Example:
Suppose we have a table called orders with columns order_id, customer_id, and order_date. We want to ensure that
each order has a unique order ID. We can add a PRIMARY KEY constraint to the order_id column using the following SQL
command:
Now, the order_id column cannot have a null value, and any attempt to insert a row into the orders table with a duplicate
order ID will result in an error.
FOREIGN KEY Constraint
The FOREIGN KEY constraint ensures that the values in a column match the values in another table's column. It creates
a relationship between two tables based on the values in one or more columns.
Example:
The customers table has columns customer_id and customer_name, while the orders table has columns order_id,
customer_id, and order_date.
We want to ensure that the customer_id column in the orders table matches a valid customer_id in the customers table.
We can add a FOREIGN KEY constraint to the customer_id column in the orders table using the following SQL
command:
Now, any attempt to insert a row into the "orders" table with a "customer_id" value that does not exist in the "customers"
table will result in an error.
CHECK Constraint
The CHECK constraint ensures that the values in a column meet a specific condition. This condition can be a simple
comparison or a more complex expression.
Example:
Suppose we have a table called students with columns student_id and age.
We want to ensure that each student is at least 18 years old. We can add a CHECK constraint to the "age" column using
the following SQL command:
ALTER TABLE students ADD CONSTRAINT check_age CHECK (age >= 18);
Now, any attempt to insert a row into the "students" table with an age value less than 18 will result in an error.
Constraints are an important tool for ensuring the accuracy and consistency of data in a PostgreSQL database. By using
constraints, you can prevent invalid data from being inserted, updated, or deleted, and ensure that your data remains
reliable and trustworthy.
Thank You