在本教程中,您将学习如何使用oracle create table
语句在oracle数据库中创建一个新表。
要在oracle数据库中创建一个新表,可以使用create table
语句。下面说明了create table
语句的基本语法:
create table schema_name.table_name (
column_1 data_type column_constraint,
column_2 data_type column_constraint,
...
table_constraint
);
在上面这个语法中,
create table
子句中,指定新表所属的表名和模式名称。number
,varchar2
和列约束,如not null
,主键,约束检查等。请注意用户权限问题,必须具有
create table
系统特权才能在模式中创建新表,并使用create any table
系统特权在其他用户的模式中创建新表。除此之外,新表的所有者必须具有包含新表或unlimited tablespace
系统特权的表空间的配额。
以下示例显示如何在ot
模式中创建一个名称为persons
的新表:
-- oracle 12c写法
create table ot.persons(
person_id number generated by default as identity,
first_name varchar2(50) not null,
last_name varchar2(50) not null,
primary key(person_id)
);
在这个例子中,persons
表有三列:person_id
,first_name
和last_name
。
person_id
是标识表中唯一行的标识列。 person_id
列的数据类型是number
。 子句generated by default as identity
指示 oracle生成一个新的整数,并在列中插入新行时将其用于列。
first_name
列的数据类型为varchar2
,最大长度为50
。这意味着不能在first_name
列中插入长度大于50
的字符。 另外,not null
列约束防止first_name
列具有null
值。
last_name
列具有与first_name
列相同的特征。
primary key
子句将person_id
列指定为主键列,用于标识个人表中的唯一行。
在本教程中,您已学习如何使用oracle create table
语句来创建新表。