create table as
语句用于通过复制现有表的列从现有表来创建新表。
注意:如果以这种方式创建表,则新表将包含现有表中的记录。
语法:
create table new_table
as (select * from old_table);
在此示例中,我们通过复制现有表customers
中的所有列来创建newcustomers
表。
create table newcustomers
as (select * from customers where customer_id < 5000);
新创建的表命名为newcustomers
并具有与customers
相同的表字段和记录(编号小于5000
的所有记录)。
语法:
create table new_table
as (select column_1, column2, ... column_n
from old_table);
下面来看另一个例子:
create table newcustomers2
as (select customer_id, customer_name
from customers
where customer_id < 5000);
上面的例子将创建一个名为newcustomers2
的新表。 此表包含customers
表中指定的两列:customer_id
和customer_name
。
语法:
create table new_table
as (select column_1, column2, ... column_n
from old_table_1, old_table_2, ... old_table_n);
下面来看一个例子:假设已经创建了两个表:regularcustomers
和irregularcustomers
。
regularcustomers
表有三列:rcustomer_id
,rcustomer_name
和rc_city
。
create table "regularcustomers"
( "rcustomer_id" number(10,0) not null enable,
"rcustomer_name" varchar2(50) not null enable,
"rc_city" varchar2(50)
)
/
第二个表:irregularcustomers
也有三列:ircustomer_id
,ircustomer_name
和irc_city
。
create table "irregularcustomers"
( "ircustomer_id" number(10,0) not null enable,
"ircustomer_name" varchar2(50) not null enable,
"irc_city" varchar2(50)
)
/
在下面的示例中,将创建一个表名:newcustomers3
,从两个表复制指定列。
示例:
create table newcustomers3
as (select regularcustomers.rcustomer_id, regularcustomers.rc_city, irregularcustomers.ircustomer_name
from regularcustomers, irregularcustomers
where regularcustomers.rcustomer_id = irregularcustomers.ircustomer_id
and regularcustomers.rcustomer_id < 5000);