Categories
Uncategorized

Create Table

Following is a syntax to create table in oracle database.

CREATE TABLE schema_name.table_name ( column_1 data_type column_constraint, column_2 data_type column_constraint, … table_constraint );

  1. schema_name – Name of the schema table belongs to. It is an optional.
  2. table_name – Name of the table to be created
  3. column_1 – Table may contain multiple columns, it represents name of column 1 and so on. Multiple column schema definitions are seperated by comma.
  4. data_type – Oracle supports various data types, it represents data_type for current column.
  5. column_constraint – Contraint definition for column, if any like primary key, not null etc
  6. table_constraint – Constraints definitions for table.

EXAMPLE –

Lets create Employee column having columns id, firstName, lastName. Following is the sql query for the same.

create table Employee(
employee_id number generated by default as identity,
first_name varchar2(50) not null,
last_name varchar2(50) not null,
primary key(employee_id)
);

Here we have created table named “Employee” having following

  1. Column employee_id – This is unique identifier column with schame type as Number. Column constraint “generated by default as identity” instructs database server to automatically generate values for this column when records are inserted.
  2. Column first_name – This is column with type varchar2 having size 50. That means column can contain string literal with maximum number of characters 50. Also colum constraint “not null” says value for this column cannot be empty.
  3. Colum last_name – same as first_name column
  4. Constraint Primary Key – This is table level constraint to declare that column employee_id would contain unique values to identify each record uniquely.

Leave a comment

Design a site like this with WordPress.com
Get started