【Oracle】创建表
在Oracle数据库中,创建表的基本语法如下:
CREATE TABLE table_name (
column1 datatype [NULL | NOT NULL],
column2 datatype [NULL | NOT NULL],
...
columnN datatype [NULL | NOT NULL],
[CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... columnN)]
[CONSTRAINT constraint_name FOREIGN KEY (column1, column2, ... columnN)
REFERENCES parent_table (column1, column2, ... columnN)]
[CONSTRAINT constraint_name UNIQUE (column1, column2, ... columnN)]
);
以下是一个创建表的示例代码:
CREATE TABLE Employees (
EmployeeID NUMBER(6) NOT NULL,
FirstName VARCHAR2(20),
LastName VARCHAR2(25) NOT NULL,
Email VARCHAR2(50),
PhoneNumber VARCHAR2(15),
HireDate DATE,
JobID VARCHAR2(10) NOT NULL,
Salary NUMBER(8, 2),
CommissionPct NUMBER(2, 2),
ManagerID NUMBER(6),
DepartmentID NUMBER(4),
CONSTRAINT Employees_pk PRIMARY KEY (EmployeeID),
CONSTRAINT Employees_uk UNIQUE (Email),
CONSTRAINT Employees_fk FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
);
在这个例子中,我们创建了一个名为Employees
的表,包含了一系列的列和约束。其中EmployeeID
是主键,Email
是唯一约束,DepartmentID
是一个外键,它引用了另一个表Departments
。
评论已关闭