The CREATETABLE statement allows you to create and define a table. This is the first step in database creation. Let's take a look at the detailed instance.
The create table statement allows you to CREATE and define a TABLE. This is the first step in database creation. Let's take a look at the detailed instances.
The basic syntax of the create table statement is:
| The Code is as follows: |
|
Create table table_name
(Column1 datatype null/not null,
Column2 datatype null/not null,
...
); |
Each column must have a data type. The column should be defined as "null" or "not null". If this value is null, the default value is "null ".
Instance
| The Code is as follows: |
|
|
Create table suppliers
(Supplier_id number (10) not null,
Supplier_name varchar2 (50) not null,
Contact_name varchar2 (50)
); |
Exercise 1:
Create a customer table to store the customer ID, name, and address information. The customer ID should be the primary key of the table.
Solution:
The create table statement for the user TABLE is:
| The Code is as follows: |
|
|
Create table MERs
(Customer_id number (10) not null,
Customer_name varchar2 (50) not null,
Address varchar2 (50 ),
City varchar2 (50 ),
State varchar2 (25 ),
Zip_code varchar2 (10 ),
CONSTRAINT customers_pk primary key (customer_id)
); |
Exercise #2:
Create an employee table store employee ID, employee name, department, and salary information based on the department table. The primary key of the Employees table should be the number of Employees. Create a Department employee table in the DEPARTMENT_ID field of a foreign key reference table.
| The Code is as follows: |
|
|
Create table orders ments
(Department_id number (10) not null,
Department_name varchar2 (50) not null,
CONSTRAINT departments_pk primary key (department_id)
); |
The create table statement for the employees table is:
| The Code is as follows: |
|
|
Create table employees
(Employee_number number (10) not null,
Employee_name varchar2 (50) not null,
Department_id number (10 ),
Salary number (6 ),
CONSTRAINT employees_pk primary key (employee_number ),
CONSTRAINT fk_ments ments
Foreign key (department_id)
REFERENCES parameters (department_id)
); |