在本教程中,您将学习如何使用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语句来创建新表。