/    /  SQL CREATE TABLE

SQL Create TABLE

In this tutorial, we will discuss about how to create a table

How to create a table?

A table is nothing but a combination of rows and columns with valid syntax.

The syntax for creating a table includes the list of column or field names and associated data types, constraints.

We can derive 2 more things out of this table. They are view and synonym

  •  A view is a logical representation of the table.
  • A synonym is nothing but another name for a table.

In this table you need to mention 3 things;

  1. Table name and schema name to which the new table belongs to the CREATE TABLE clause.
  2. List all columns of the table within the parentheses and a table has multiple columns, you need to separate them by commas (,) and by its data type e.g., NUMBER, VARCHAR2,
  3. Add table constraints if applicable e.g., primary key, foreign key, check.

Mostly it will be created for a view or a table, so likewise a sequence, or a function, procedure, likewise we do have different objects so out of which that we can use this create command to any of this kind of object.

In the previous tutorial we have made a connection to the HR account and you could see once you extract a particular HR Database and you can see the list of tables which are already existing.

Now we will be creating a new set of tables. You are going to mention the table name as users.

Syntax:

Create table schema_name.table_name ( 
column_1 data_type column_constraint, 
column_2 data_type column_constraint, 
... 
table_constraint
 );

Example:

Create table users ( 
user_id number(5) primary key,
user_name VARCHAR2(20), 
age number, 
email varchar2(30)
)

Click on CTRL+ENTER

Let me explain what exactly this primary key will do, it will allow you only the Unique data, so one time data, and also the primary key will not make up any sort of duplicate data and it is going to allow only the unique data.

When you look at this in the HR column on the left-hand side, it is not showing you that table, so now let me refresh this one. So you should be able to see this users table.

Output:

Where you clearly see the table has been created with the respective fields.