Home >
Article > Database > Let’s talk about how to create a table in Oracle
Let’s talk about how to create a table in Oracle
PHPzOriginal
2023-04-04 14:00:072925browse
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.
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
);
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
);
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:
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!
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