How to create a table in SQL | SQL tutorial for beginners |SQL create table

How to create a table in SQL

In this tutorial, we will learn how to create a table in SQL. For creating a new oracle table we will use CREATE TABLE statement in the oracle database. It must be noted that we should have privilege to CREATE TABLE in sql schema. Also the sql table should have quota for table space.

SYNTAX for CREATE TABLE
CREATE TABLE schema_name.table_name (
    column_1 data_type column_constraint,
    column_2 data_type column_constraint,
    ...
    ...
 );

In the above SQL code we can see

  • CREATE TABLE : It is the main clause under which other information is put.
  • schema_name.table_name : We will mention schema name and then table name which we wanted to create. There are some naming convention which we need to follow.
  • column_xx: This is the column name that we wanted to create it will follow data type of pl/sql e.g number, varchar2 and it will followed by constraint if any like NOT NULL, PRIMARY KEY or any CHECK that we wanted to put.

We should be using comma if there is more than one sql column and after the last column use close parenthesis.

Oracle CREATE TABLE statement example
CREATE TABLE schema_name.employee(
    employee_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
    first_name VARCHAR2(40) NOT NULL,
    last_name VARCHAR2(40) NOT NULL,
    PRIMARY KEY(employee_id)
);

In the above sql query we tried to CREATE TABLE employee with columns name employee_id, first_name , last_name . we can also see we used employee_id as PRIMARY KEY. Column employee_id is an auto generated number. columns first_name and last_name has data types of VARCHAR2 with length of 40 and it should not have NULL values.

How to create a table in SQL
How to create a table in SQL

You can read more about this topic from this link.

3 thoughts on “How to create a table in SQL | SQL tutorial for beginners |SQL create table”

  1. Pingback: Insert query in SQL | SQL insert statement in Oracle - DataWitzz

  2. Pingback: Pivot and Unpivot in SQL || How to convert rows to Columns in sql - DataWitzz

  3. Pingback: How to use row_number in SQL | SQL RANK() function | dense_rank SQL - DataWitzz

Leave a Comment