Home > Database > Oracle > Let's talk about how to create a table in Oracle

Let's talk about how to create a table in Oracle

PHPz
Release: 2023-04-04 14:22:47
Original
2974 people have browsed it

Oracle database table creation is one of the most basic operations in the Oracle database. This article will introduce the Oracle table creation operation in detail in terms of table creation syntax, data types, primary keys, foreign keys, etc.

Table creation syntax

Oracle table creation syntax is as follows:

CREATE TABLE <表名>
(
    <列名1> <数据类型1>,
    <列名2> <数据类型2>,
    ……
    <列名n> <数据类型n>
);
Copy after login

Among them, CREATE TABLE means to create a table, the table name is

, in brackets is the column name and data type of the table. The definition format of each column is , which can have one or more columns.

Table creation operation example

Take creating a students table containing three columns as an example, including the student's ID, name and age:

 CREATE TABLE students
(
    ID INTEGER,
    name VARCHAR2(50),
    age INTEGER
);
Copy after login

Data type

In Oracle, each column needs to define a corresponding data type. Commonly used data types are as follows:

  • CHAR(size): fixed-length string, the maximum length is 2000
  • VARCHAR2(size): Variable length string, the maximum length is 4000
  • NUMBER(p,s): Number with precision and scale, p represents numerical precision, s represents the number of decimal places
  • DATE: Date type, supports date and time storage
  • TIMESTAMP: Timestamp type, supports more precise time storage

Primary key

The primary key is a field used to ensure that each row in the data table can be uniquely identified, and the data in it cannot be repeated. In Oracle, primary keys can be used to effectively improve data retrieval efficiency, and can also be used to ensure data integrity. The primary key can be defined through the following statement:

CREATE TABLE students
(
    ID INTEGER PRIMARY KEY,
    name VARCHAR2(50),
    age INTEGER
);
Copy after login

In the above code, we use PRIMARY KEY to define the ID column as the primary key.

Foreign key

Foreign key is a constraint established between relational database tables to ensure consistency between data tables. In Oracle, foreign keys are often used to define relationships between tables. Foreign keys can be defined through the following statement:

CREATE TABLE customers
(
    ID INTEGER PRIMARY KEY,
    name VARCHAR2(50)
);

CREATE TABLE orders
(
    ID INTEGER PRIMARY KEY,
    date_ordered DATE,
    customer_id INTEGER,
    CONSTRAINT fk_customer_id
    FOREIGN KEY (customer_id) REFERENCES customers(ID)
);
Copy after login

In the above code, we define a foreign key in the orders table, That is the customer_id column. It links the primary key ID of another table customer and uses the name "fk_customer_id" to describe this foreign key relationship. These statements will ensure that every record in the orders table is associated with a record in the customers table.

Summary

If you want to create a table in an Oracle database, you must know Oracle's table creation syntax, data types, primary keys, and foreign keys. This article provides detailed table creation operation procedures and examples, I hope it will be helpful to you.

The above is the detailed content of Let's talk about how to create a table in Oracle. For more information, please follow other related articles on the PHP Chinese website!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template