Introduction SQL

Basic SQL

SQL Constraints

Advance SQL

SQL Joins

SQL Function

SQL CREATE TABLE Statement / DROP TABLE Statement


SQL CREATE TABLE statement to create new table. SQL TABLE columns name must be unique with specified built-in data type. TABLE COLUMN support various attributes such as column data type, NULL, NOT NULL, CONSTRAINTS, DEFAULT VALUE et cetera.


SQL CREATE TABLE Statement

Following is SQL CREATE TABLE syntax, but now you just learn basic CREATE TABLE syntax. Once you know SQL CONSTRAINT concept you can learn this syntax.

Syntax

CREATE TABLE [ IF NOT EXISTS ] table_name(
	column_name datatype[(size)] [ NULL | NOT NULL ],
	column_name datatype[(size)] [ NULL | NOT NULL ],
	[ CONSTRAINT constraint_name 
		PRIMARY KEY ( col1, col2, ... ) |
		FOREIGN KEY ( col1, col2, ... ) REFERENCES table_2 [ ( col1, col2, ... ) 
			[ ON UPDATE | ON DELETE 
				[ NO ACTION | SET NULL | SET DEFAULT | CASCADE ] 
			] 
		] |
		UNIQUE ( col1, col2, ... ) |
		CHECK ( expression )
	]
	...
);

Basic Syntax :

CREATE TABLE table_name(
	column_name1 datatype(size),
	column_name2 datatype(size)
	...
);

Keep in Mind... : You can not create new table if table already exist in selected database. and another things table name must have unique within database.

Example

Here we create new table in SQL. 4 columns specify with data type and size.

SQL> CREATE TABLE users_info(
 	no NUMBER(3,0),
 	name VARCHAR(30),
	address VARCHAR(70),
	contact_no VARCHAR(12)
);

Table created.

Run it...   »


SQL DROP TABLE Statement

SQL DROP TABLE statement to delete the table structure, table data, defined constraints or index.

Syntax

DROP TABLE table_name;

Example

SQL> DROP TABLE users_info;

Table dropped.