1 -- Example table
2 CREATE TABLE accounts (
3 user_id serial PRIMARY KEY,
4 username VARCHAR ( 50 ) UNIQUE NOT NULL,
5 password VARCHAR ( 50 ) NOT NULL,
6 email VARCHAR ( 255 ) UNIQUE NOT NULL,
7 created_on TIMESTAMP NOT NULL,
8 last_login TIMESTAMP
9);
1CREATE TABLE cities (
2 city_id serial PRIMARY KEY,
3 city_name VARCHAR (255) NOT NULL,
4 population INT NOT NULL CHECK (population >= 0)
5);
6
7
1-- Basic table creation
2CREATE TABLE table_name(
3 column1 datatype,
4 column2 datatype,
5 column3 datatype,
6 .....
7 columnN datatype,
8 PRIMARY KEY( one or more columns )
9);
10
1CREATE TABLE table_name (
2 column_name TYPE column_constraint,
3 table_constraint table_constraint
4) INHERITS existing_table_name;
1CREATE TABLE films_recent AS
2 SELECT * FROM films WHERE date_prod >= '2002-01-01';
3
1CREATE TABLE table_name (
2 column_name1 col_type (field_length) column_constraints,
3 column_name2 col_type (field_length),
4 column_name3 col_type (field_length)
5);
6