A collection of reference examples of SQL DDL (Data Definition Language) statements frequently used in application development to create and alter database tables, synonyms, constraints, indexes etc.
Note: The statements have been tested on Oracle 11G database; most probably should work with other relational databases. Some of the datatypes used in the samples are Oracle-specific and would require some tweaking.
Note: The statements have been tested on Oracle 11G database; most probably should work with other relational databases. Some of the datatypes used in the samples are Oracle-specific and would require some tweaking.
-- Create a table CREATE TABLE TX_HISTORY ( tx_id NUMBER(10, 0) NOT NULL, stock_id NUMBER(10, 0) NOT NULL, tx_date DATE NOT NULL, currency VARCHAR2(5) NOT NULL, price NUMBER(9, 2) NOT NULL, price_2 NUMBER ) / -- Create a table with a PK (Primary Key) on a single column CREATE TABLE STOCK ( stock_id NUMBER(10, 0) NOT NULL, symbol VARCHAR2(5) NOT NULL, stk_exchange VARCHAR2(5) NOT NULL, currency VARCHAR2(5) NOT NULL, -- CONSTRAINT stock_id_pk PRIMARY KEY (stock_id) ) / -- Add a PK (Primary Key) to already existing table ALTER TABLE tx_history ADD CONSTRAINT tx_history_pk PRIMARY KEY (tx_id) / -- Create a table with a FK (Foreign Key) to another table CREATE TABLE STOCK_HISTORY ( rec_id NUMBER(10, 0) NOT NULL, stock_id NUMBER(10, 0) NOT NULL, -- FK to STOCK table trade_date DATE NOT NULL, open_price NUMBER(9, 2) NOT NULL, close_price NUMBER(9, 2) NOT NULL, -- CONSTRAINT stock_history_stock_fk FOREIGN KEY (stock_id) REFERENCES stock (stock_id) ENABLE VALIDATE ) / -- Add a FK (Foreign Key) to already existing table to reference another table ALTER TABLE tx_history ADD CONSTRAINT tx_history_stock_fk FOREIGN KEY (stock_id) REFERENCES stock (stock_id) ENABLE VALIDATE / -- Drop (delete) a table DROP TABLE TX_HISTORY / -- Add a new column to an existing table ALTER TABLE TX_HISTORY ADD QTY NUMBER / -- Drop a column ALTER TABLE TX_HISTORY DROP COLUMN PRICE_2 /