How to Create, Alter and Drop a Table using SQL?

Creating a Table

Syntax

CREATE TABLE <tablename>(<columnname> <datatype>(size) <constraints>);

Example

CREATE TABLE customer(cid int primary key, name varchar(50) not null, email varchar(50) not null, mobile char(10));

Note:
Here primary key, not null are called as constraints or rules. If you don't know about constraints first learn about the constraints first learn the constraints from Here

Alternation in the Table

- We can add new columns, change data type and constraints, drop the columns etc.

Example 1
Add another column CITY in customer table.

ALTER TABLE customer ADD city VARCHAR(50);

Example 2
Change the size of name to 100

ALTER TABLE customer MODIFY name varchar(100);

Example 3
Remove the column city
ALTER TABLE customer DROP COLUMN city;

Removing a table
Syntax
DROP TABLE <tablename>;

Example
DROP TABLE customer;

Comments

Popular posts from this blog

What is template?

Can we access private members of an instance?

What are wrapper classes in Java?