Follow us: Subscribe via RSS Feed Connect on YouTube Connect on YouTube

Pages

Thursday, 30 March 2017

SQL Create Table




SQL Create Table


 SQL Create statement allows you create database table

Syntax:

CREATE TABLE < table_name>
            (Col1 datatype1,
              Col2 datatype2 …coln datatypen);

Example:
CREATE TABLE Student
(
   no      NUMBER (2),
   name    VARCHAR (10),
   marks   NUMBER (3)
);


Create Table By Using Select Statement


We can create table by existing table or we call it is as copy of existing table by using select statement 

Syntax:

CREATE TABLE < New_table_name> AS
    SELECT  Col1,Col2 …Coln  WHERE ….;


Example:

By following example we can create table along with data.

CREATE TABLE student_copy AS
           SELECT  NO,
                     NAME,
                   MARKS
            FROM student;


Example:

By following example we can create table without data.

CREATE TABLE student_copy AS
           SELECT  NO,
                     NAME,
                   MARKS
            FROM student WHERE 1=2;

Create Table By Using Constrains


 Example:
 
CREATE TABLE emp
(
   empno       NUMBER PRIMARY KEY,
   ename       VARCHAR2 (50) NOT NULL,
   job         VARCHAR2 (10),
   mgr         NUMBER,
   hiredate    DATE DEFAULT (SYSDATE),
   sal         NUMBER,
   hrly_rate   NUMBER,
   comm        NUMBER,
   deptno      NUMBER NOT NULL 
);

Note: To know more about constrains >SQL Constraints
Share this article :

0 comments:

Post a Comment