
+-+-+-ĭblink | 1.2 | public | connect to other PostgreSQL databases from within a databaseĮdbspl | 1.0 | pg_catalog | EDB-SPL procedural languageĢ. In postgres we call achieve this using foreign data wrappers.
CREATE DATABASE POSTGRES CODE
REFERENCES roles (role_id) Code language: SQL (Structured Query Language) ( sql ) The role_idcolumn references the role_idcolumn in the roles table, we also need to define a foreign key constraint for the role_idcolumn. REFERENCES accounts (user_id) Code language: SQL (Structured Query Language) ( sql ) PRIMARY KEY (user_id, role_id) Code language: SQL (Structured Query Language) ( sql )īecause the user_idcolumn references to the user_idcolumn in the accounts table, we need to define a foreign key constraint for the user_idcolumn: FOREIGN KEY (user_id) The primary key of the account_roles table consists of two columns: user_id and role_id, therefore, we have to define the primary key constraint as a table constraint. The following statement creates the account_roles table that has three columns: user_id, role_id and grant_date. The following statement creates the roles table that consists of two columns: role_id and role_name: CREATE TABLE roles( The following statement creates the accounts table: CREATE TABLE accounts ( We will create a new table called accounts that has the following columns: Table constraints are similar to column constraints except that they are applied to more than one column. Unlike the primary key, a table can have many foreign keys.

When you use the IF NOT EXISTS option and the table already exists, PostgreSQL issues a notice instead of the error and skips creating the new table.

The IF NOT EXISTS option allows you to create the new table only if it does not exist. Second, creating a table that already exists will result in a error.First, specify the name of the table after the CREATE TABLE keywords.) Code language: SQL (Structured Query Language) ( sql ) The following illustrates the basic syntax of the CREATE TABLE statement: CREATE TABLE table_name (Ĭolumn1 datatype( length) column_contraint,Ĭolumn2 datatype( length) column_contraint,Ĭolumn3 datatype( length) column_contraint, To create a new table, you use the CREATE TABLE statement. Tables allow you to store structured data like customers, products, employees, etc. PostgreSQL CREATE TABLE syntaxĪ relational database consists of multiple related tables.
CREATE DATABASE POSTGRES HOW TO
Summary: in this tutorial, you will learn how to use the PostgreSQL CREATE TABLE statement to create new a new table.
