oracle creat table,Oracle创建表(create table as)

使用CREATE TABLE AS 创建新表
本文介绍如何使用SQL语句CREATE TABLE AS从现有表创建新表。文章提供了三种示例:复制全部列、复制部分列及从多个表中复制列,并详细解释了语法结构。

CREATE TABLE AS语句用于通过复制现有表的列从现有表来创建新表。

注意:如果以这种方式创建表,则新表将包含现有表中的记录。

语法:

CREATE TABLE new_table AS (SELECT * FROM old_table);

创建表示例1:复制另一个表的所有列

在此示例中,我们通过复制现有表Customers中的所有列来创建newcustomers表。

CREATE TABLE newcustomers AS (SELECT * FROM customers WHERE customer_id < 5000);

新创建的表命名为newcustomers并具有与customers相同的表字段和记录(编号小于5000的所有记录)。

创建表示例2:复制另一个表的选定列

语法:

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。

创建表示例3:从多个表复制选定的列

语法:

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);

¥ 我要打赏   纠错/补充 收藏

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值