Monday 24 August 2015

How to create Table in oracle?

Hi friend, once again i am here with new topic "Working with table". Working with table is nothing but in this post we will see, how to create table.

Table:

In oracle database data organized in the form of  table which is a combination of rows and column. For creating table there is a syntax that is given bellow:
Syntax:
CREATE TABLE <table_name>
(
         column_name1   datatype(size),
         column_name2   datatype(size),
         column_name3   datatype(size),
         column_name4   datatype(size),
         column_name5   datatype(size),
          ----------------
          ----------------
); 

There is some rules that must be follow before creating table:
  • Table name should start with alphabets. For example:
            emp123           correct
            emp12ex         correct
            123emp          incorrect 
  • Table should not contain space and special symbols (except: _,$,#. These special symbols are allowed). For example:
            emp  123        incorrect
            emp*123        incorrect
            emp@123      incorrect
            emp_123        correct
            emp$123        correct
            emp#123        correct
  • Name of the table can have maximum up-to 30 character.
  • A table can have maximum 1000 columns.
First three record is followed every where for declaring any identifiers. Like if we declaring a column name these three rules must be followed. If we declaring a user name these three rules must be followed. If we declaring any view, trigger, package etc then these three rules must be followed. Now lets create a table.

Q. Create a table with following structure:
      emp
              empno
              ename
              job
              sal
              hiredate

Ans. ==>
        CREATE TABLE emp
        (
                 empno    NUMBER(4),
                 ename    VARCHAR2(20),
                 job          VARCHAR2(20),
                 sal           NUMBER(7,2),
                 hiredate  DATE
        );

Now lets run this query.

Creating emp table
Creating emp table

Now, here one questing arises that we are created table and forget the structure of table then what to do? For that there is a command that is:

DESCRIBE command

This command is used to see the table data definition or we can say structure. This is also SQLPlus command. Now question is that what is SQLPlus command? what is the different between SQL command and SQLPlus command? Now lets see:
  • SQL commands end with semi-clone(;) but in SQLPlus commands there is no need to terminate with semi-clone(;).
  • SQL commands can't be abbreviated but SQLPlus commands can be abbreviated. Abbreviation means nothing but sorting the command. For example: We can write "DESCRIBE" command as "DESC".
  • SQL command perform operation over database but SQLPlus commands set the environment. 
Now lets see the syntax for "DESCRIBE" commands. We use this as "DESC" here:
Syntax:
        DESC <table_name>

Lets see an example:
            DESC emp

DESC command
See the structure of table

We can use both either "DESC" or "DESCRIBE" as we like. For that's all. In next post we will see how to insert data in table. For more keep visiting this blog......

No comments: