Friday, February 6, 2015

SQL CREATE TABLE


SQL CREATE TABLE


SQL CREATE TABLE statement is used to create table in a database.
If you want to create a table, you should name the table and define its column and each column's data type.
Let's see the simple syntax to create the table.
  1. create table "tablename"  
  2. ("column1" "data type",  
  3. "column2" "data type",  
  4. "column3" "data type",  
  5. ...  
  6. "columnN" "data type");  
The data type of the columns may vary from one database to another. For example, NUMBER is supported in Oracle database for integer value whereas INT is supported in MySQL.
Let us take an example to create a STUDENTS table with ID as primary key and NOT NULL are the constraint showing that these fields cannot be NULL while creating records in the table.
  1. SQL> CREATE TABLE STUDENTS (  
  2. ID INT                           NOT NULL,  
  3. NAME VARCHAR (20) NOT NULL,  
  4. AGE INT                         NOT NULL,  
  5. ADDRESS CHAR (25),  
  6. PRIMARY KEY (ID)  
  7. );  
You can verify it, if you have created the table successfully by looking at the message displayed by the SQL Server, else you can use DESC command as follows:
SQL> DESC STUDENTS;
FIELDTYPENULLKEYDEFAULTEXTRA
IDInt(11)NOPRI
NAMEVarchar(20)NO
AGEInt(11)NO
ADDRESSVarchar(25)YESNULL
4 rows in set (0.00 sec)
Now you have the STUDENTS table available in your database and you can use to store required information related to students.

SQL CREATE TABLE Example in MySQL

Let's see the command to create a table in MySQL database.
  1. CREATE TABLE Employee  
  2. (  
  3. EmployeeID int,  
  4. FirstName varchar(255),  
  5. LastName varchar(255),  
  6. Email varchar(255),  
  7. AddressLine varchar(255),  
  8. City varchar(255)  
  9. );  

SQL CREATE TABLE Example in Oracle

Let's see the command to create a table in Oracle database.
  1. CREATE TABLE Employee  
  2. (  
  3. EmployeeID number(10),  
  4. FirstName varchar2(255),  
  5. LastName varchar2(255),  
  6. Email varchar2(255),  
  7. AddressLine varchar2(255),  
  8. City varchar2(255)  
  9. );  

SQL CREATE TABLE Example in Microsoft SQLServer

Let's see the command to create a table in SQLServer database. It is same as MySQL and Oracle.
  1. CREATE TABLE Employee  
  2. (  
  3. EmployeeID int,  
  4. FirstName varchar(255),  
  5. LastName varchar(255),  
  6. Email varchar(255),  
  7. AddressLine varchar(255),  
  8. City varchar(255)  
  9. );  

No comments:

Post a Comment