Ads Here

Tuesday, 9 January 2018

Create Table

As we discussed previously for work with a Relational database we always need tables. Table is a database object created for store data. In database when we create a table it have some columns and each column must have a datatype. So, in oracle SQL first you need to know some basic datatype that we use for creating a table.
Datatypes:
DATA TYPE
DESCRIPTION
Number
Store number value
Char
Store Character value
VarChar
Store long character value
Date
Store a date
Interval Year
Store time period as years and months
Interval Day
Store time period as days, hours, minutes, and seconds
Timestamp
Store date, time with fractional seconds
Timestamp with Time Zone
Store date and time with a time zone
BFILE
Store binary data in an external file
RAW
Store RAW binary value
Long RAW
Store lone RAW binary value

Now, in oracle database for creating a table we have SQL statement. So, Let see the step by step process for creating a table.
1) Open Start Database & Run SQL Commands Line from the Oracle database 11g express edition folder in all apps.
2) Put the cursor in Run SQL Commands and connect to the user. Every time when you want to work with Oracle SQL first you have to connect to the user. If you don't know how to connect to database user then click here for check the previous post.
3) After you have successfully connected to your database user now you can create a table. The basic syntax for create a table is this -
Syntax:

In the above syntax the CREATE TABLE keyword is used to telling the database system that you want to create a table. After that you put the name of the table. Now, you have to declare the columns of the table and the datatype for each column. The columns are entered between two first brackets, separate by coma symbol and at last semicolon for complete the SQL statement. When you execute this SQL statements, it will show you Table Created, which means you have successfully created your table
4) Now, after creating a table if you want to check the table you can use this SQL statement -
Syntax:

The DESC keyword used for view details of the table which name you enter after this keyword. When you execute this statement it will show you the table field or column names with the datatypes.Now, let see an example of how you can implement this.
Example:
In this example, I will create a table named Learn_World with some columns.
Syntax:

So, from the above syntax you can see my table name is Learn_World. After that i declared the columns with there datatypes.
Coloumn1 is ID & its datatype is number, which means it can store number values only. Column2 is First_Name & its datatype is char(10), char(10) means it can store 10 characters. Similarly i have Last_Name char(10), Department char(10) & salary number.
When you execute this statement or press enter it will show you that Table Created, which means you have succesfully created the table.
Now, after creating a table if we want to view the table then we have to use the DESC keyword. Before run this SQL statement i have run the cle scr statement for clear the screen. In my case it will be like this.
Syntax:

So, when we execute this statement, it will show our table column names with there datatypes.
SO, as you can see when i execute this SOL statement it showing me my table column names with there datatypes. Now, the SQL command box showing that our table is created, but is the table is actually created in our database? So, lets check the table is actually created in out database or not. For check follow the steps below.
1) Go the database homepage you created form browser.
2) Now, login to your database and when you login you see this kine of interface.
3) In this page click on SQL Workspace and then it take you to this page.
4) From SQL Workspace click on Object Browser.  When you click on Object Browser it will take you to the database page where you find all the Objects of database. In the left side of the page you find a list of objects with a search bar. Now, above the search bar you must choose the type of object you want to see. In this case we select Table. 
5) Now, you can scroll the side and or you can search for your table by its name. I have searched and you can see there it showing a table named Learn_World.
6) Now, simply click on the table and it will show your table with all Columns with there datatypes.
So, now you have successfully created table on your database using SQL. If anyone have any question or face any problem then ask me in the comment section below, thank you.

No comments:

Post a Comment