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);
1# Creates a Simple User table
2# Uses an auto-incrementing primary key as userId
3
4CREATE TABLE user (
5 userId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
6 username VARCHAR(100),
7 password VARCHAR(100)
8) ENGINE=InnoDB;
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
1CREATE TABLE COMPANY(
2 ID INT PRIMARY KEY NOT NULL,
3 NAME TEXT NOT NULL,
4 AGE INT NOT NULL,
5 ADDRESS CHAR(50),
6 SALARY REAL,
7 JOIN_DATE DATE
8);
9
10INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (1, 'Paul', 32, 'California', 20000.00,'2001-07-13');
11INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,JOIN_DATE) VALUES (2, 'Allen', 25, 'Texas', '2007-12-13');
12INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (3, 'Teddy', 23, 'Norway', 20000.00, DEFAULT );
13INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00, '2007-12-13' ), (5, 'David', 27, 'Texas', 85000.00, '2007-12-13');
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;