Home > Database > Oracle > body text

How to create a table in oracle using code

下次还敢
Release: 2024-04-18 21:42:16
Original
1177 people have browsed it

Answer: Yes, you can create a table by using the CREATE TABLE statement. Syntax: CREATE TABLE table_name (column_name data_type, ...); specify column name and data type; optional constraints (for example, NOT NULL, UNIQUE, PRIMARY KEY); Example: Create customers table (id, name, address); execute statement (For example, in SQLPlus: SQL> CREATE TABLE customers (id NUMBER PRIMARY KEY, name V

How to create a table in oracle using code

Oracle Create table using code

In Oracle, tables can be created through code using SQL statements. The following steps illustrate how to do this:

1. Use the CREATE TABLE statement

The syntax for creating a table is as follows:

<code>CREATE TABLE table_name (
  column1_name data_type,
  column2_name data_type,
  ...
  columnn_name data_type
);</code>
Copy after login

2. Specify the column name and data type

  • table_name is the name of the table to be created .
  • column_name is the name of each column to be created.
  • data_type is the data type of each column (for example, NUMBER, VARCHAR2, DATE. etc.).

##3. Optional constraints

In addition to column names and data types, column constraints can also be specified, for example:

  • NOT NULL: Null columns are not allowed
  • UNIQUE: Guaranteed that the values ​​in the column are unique. PRIMARY KEY: Identifies the column as the primary key, used to uniquely identify each row in the table
  • ##4. Example
The following example creates the name. A table for

customers with three columns: id

,

name, and address:

<code>CREATE TABLE customers (
  id NUMBER PRIMARY KEY,
  name VARCHAR2(255) NOT NULL,
  address VARCHAR2(255)
);</code>
Copy after login
5. Execute statementYou can use SQL

Plus or other database tools to execute the CREATE TABLE statement. For example, in SQL

Plus:

.

<code>SQL> CREATE TABLE customers (
     id NUMBER PRIMARY KEY,
     name VARCHAR2(255) NOT NULL,
     address VARCHAR2(255)
  );

Table created.</code>
Copy after login
Now the table named customers is created and ready to insert data into it .

The above is the detailed content of How to create a table in oracle using code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template