![]() Now, our comments table will look like the following. We have ON DELETE CASCADE ON UPDATE CASCADE which means if the employeeId in the employee table is updated or deleted then it will be reflected to the comments table. Now we will add the foreign key constraint. It will be the foreign key and will refer to the employeeId column of the employee table. Let's add a new column employeeId to the comments table. CREATE TABLE "comments" (ĬONSTRAINT "commentId_comments_pk" PRIMARY KEY ("commentId") In the CREATE Table tutorial we created the comments table which looks like the following at the moment. ![]() To add a foreign key to a table we use the ADD CONSTRAINT. In the following example we are dropping the idx_firstName_employee index from the firstname column. To drop an index from a column we use the DROP INDEX command. In the following example we are adding an index to the firstname column. We can add index to table columns to speed up search. To drop a unique key constraint we use the DROP CONSTRAINT command.įor example, if we want to drop the UNIQUE constraint from the email column by the symbol email_employee_unique we will run the following command. CREATE TABLE employee (ĬONSTRAINT email_employee_unique UNIQUE (email), Now, our table will look like the following. alter table employeeĪdd constraint "email_employee_unique" unique("email") In the following example we are adding a new column email to the employee table. To ensure that a column contains distinct values we add the UNIQUE constraint. Now we will drop the temp column from the table. Right now the table looks like the following. I added the temp column using the following command. Let the temp column be of boolean data type. Let's say we have a temp column in the employee table and we want to drop it. To drop a column from a table we use the DROP COLUMN command. Let's rename the dateOfBirth column back to birthday and modify the column type from TIMESTAMP to DATE. Let's rename the birthday column to dateOfBirth. To rename a column in PostgreSQL we use the RENAME COLUMN command. Let's modify the birthday column that we added to the employee table by changing its type from DATE to TIMESTAMP. ![]() To modify column in PostgreSQL we use the ALTER COLUMN command. Now our table will look like the following. Let's add a new column birthday of date data type. CREATE TABLE employee (ĬONSTRAINT "employeeId_employee_pk" PRIMARY KEY ("employeeId") This is how our employee table looks now. In the following tutorial we will work with the employee table that we created in the CREATE Table tutorial. In this tutorial we will cover the following. We use the ALTER TABLE table_name command in PostgreSQL to alter tables. In this tutorial we will learn to alter table in PostgreSQL.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |