📜  oracle ddl - SQL (1)

📅  最后修改于: 2023-12-03 14:44:55.409000             🧑  作者: Mango

Oracle DDL - SQL

DDL (Data Definition Language) is a set of SQL statements used to manage the objects in a database. In this article, we will focus on Oracle DDL - SQL.

Table Creation

To create a new table in Oracle, we use the CREATE TABLE statement followed by the table name and the list of column definitions.

CREATE TABLE employees (
   employee_id  NUMBER(6),
   first_name   VARCHAR2(20),
   last_name    VARCHAR2(20),
   email        VARCHAR2(30),
   hire_date    DATE,
   salary       NUMBER(8,2),
   CONSTRAINT   pk_employee PRIMARY KEY (employee_id)
);

In the above example, we created a table called "employees" with six columns. The "employee_id" column is of type NUMBER and has a maximum length of 6 digits. The "first_name" and "last_name" columns are of type VARCHAR2 and have a maximum length of 20 characters. The "email" column is of type VARCHAR2 and has a maximum length of 30 characters. The "hire_date" column is of type DATE, and the "salary" column is of type NUMBER with 8 digits in total, with 2 decimal places.

The last line of code is to define the primary key for the table. The primary key is used to uniquely identify each record in the table.

Table Alteration

We can use the ALTER TABLE statement to modify an existing table in Oracle.

ALTER TABLE employees ADD department_id NUMBER(4);

In the above example, we added a new column called "department_id" to the "employees" table.

ALTER TABLE employees MODIFY salary NUMBER(10,2);

In the above example, we modified the "salary" column of the "employees" table to have a maximum length of 10 digits, with 2 decimal places.

Table Rename and Deletion

We can use the RENAME TABLE statement to rename an existing table in Oracle.

RENAME employees TO staff;

In the above example, we renamed the "employees" table to "staff".

We can use the DROP TABLE statement to delete an existing table in Oracle.

DROP TABLE staff;

In the above example, we deleted the "staff" table.

Conclusion

In this article, we have seen the basics of Oracle DDL - SQL. We have learned how to create a new table, modify an existing table, rename a table, and delete a table. These are the core concepts of DDL in Oracle.