6.08.2009

DDL

CREATE TABLE:

Statement Pattern

CREATE TABLE [IF NOT EXISTS] table_name(
column_list
) type=table_type

Example-1

CREATE TABLE employees (
employeeNumber into(11) NOT NULL,
lastName varchar(50) NOT NULL,
officeCode varchar(10) NOT NULL,
reportsTo int(11) default NULL,
PRIMARY KEY (employeeNumber)
);

Example-2: Defining Duplicate Primary Key

CREATE TABLE payments (
customerNumber int(11) NOT NULL,
checkNumber varchar(50) NOT NULL,
paymentDate datetime NOT NULL,
amount double NOT NULL,
PRIMARY KEY (customerNumber,checkNumber)
);

Example-3: Defining Storage Engine

CREATE TABLE database_name.table_name(
column1 NOT NULL AUTO_INCREMENT ,
column2 VARCHAR( 20 ) NOT NULL ,
column3 VARCHAR( 20 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL ,
PRIMARY KEY ( column1)
) ENGINE = MYISAM ;

Example-4: Create table from another table

CREATE TABLE new_table_name
AS (select * from old_table);

CREATE TABLE new_table_name
AS (select col1, col2 from old_table where cond1);



DESCRIBE TABLE: DESCRIBE table_name;

SHOW TABLES: SHOW TABLES
This will show all the tables.

More..