SQL as a Data Definition Language : CREATE

Create is a DDL command used to create a table or a database. It is used to create tables, databases, views, synonyms, sequences, functions, procedures, packages etc.

Creating A Database :

In RDBMS CREATE command is used to create a database. The CREATE DATABASE statement is used to create a new Database. The basic syntax of the CREATE DATABASE statement is as follows:
 CREATE DATABASE database_name;
 where the database_name is name of the database you want to create.

Example :
 SQL> CREATE DATABASE Library;
The above command will create a database named library.


Creating  A Table :

In RDBMS CREATE command is used to create a table. The CREATE TABLE statement is used to create a new table. We can specify names and datatypes of various columns along the statement. The basic syntax of the CREATE TABLE statement is as follows −
 CREATE TABLE table_name (
   column_name1 datatype,
   column_name2 datatype,
   column_name3 datatype,
   ..............................
   column_nameN datatype,
 PRIMARY KEY( one or more columns )
 );
where the table_name is name of table, and inside the bracket we are defining the columns along with its data type.

Example :
 CREATE TABLE Employee (
     EmpId NUMBER(5),
     EmpName VARCHAR(15),
     HireDate DATE,
     Salary NUMBER(7,2),
     PRIMARY KEY(EmpId)
 );
The above statement creates a table named Employee with four columns : EmpId, EmpName, HireDate and Salay, with EmpId as primary key. We can also check our newly created table by desc command.
 SQL> DESC Employee;
And the output will be :
 Name     Null?    Type         
 -------- -------- ------------ 
 EMPID    NOT NULL NUMBER(5)    
 EMPNAME           VARCHAR2(15) 
 HIREDATE          DATE         
 SALARY            NUMBER(7,2)  


Next Topic : 



No comments:

Post a Comment