赞
踩
目录
- 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系统特权的表空间的配额。
在我们之前创建的JT_CS用户下创建表,一定要有CREATE TABLE权限,不然无法创建数据表。
- --实例建表stuinfo
-
- create table JT_CS.stuinfo
-
- (
-
- stuid varchar2(11) not null,--学号:'S'+班号(7位数)+学生序号(3位数)(不能为空)SC200101001
-
- stuname varchar2(50) not null,--学生姓名(不能为空)
-
- sex char(1) not null,--性别(不能为空)1(男)、2(女)
-
- age number(2) not null,--年龄(不能为空)
-
- classno varchar2(7) not null,--班号:'C'+年级(4位数)+班级序号(2位数)(不能为空)C200101
-
- stuaddress varchar2(100) default '地址未录入',--地址 (不填或为空时默认填入‘地址未录入‘)
-
- grade char(4) not null,--年级(不能为空)
-
- enroldate date,--入学时间
-
- idnumber varchar2(18) default '身份证未采集' not null--身份证(不能为空)
-
- )
-
-
-
- -- Add comments to the table
-
- --comment on table 是给表名进行注释
-
- comment on table JT_CS.stuinfo
-
- is '学生信息表';
-
- -- Add comments to the columns
-
- --comment on column 是给表字段进行注释。
-
- comment on column JT_CS.stuinfo.stuid
-
- is '学号';
-
- comment on column JT_CS.stuinfo.stuname
-
- is '学生姓名';
-
- comment on column JT_CS.stuinfo.sex
-
- is '学生性别';
-
- comment on column JT_CS.stuinfo.age
-
- is '学生年龄';
-
- comment on column JT_CS.stuinfo.classno
-
- is '学生班级号';
-
- comment on column JT_CS.stuinfo.stuaddress
-
- is '学生住址';
-
- comment on column JT_CS.stuinfo.grade
-
- is '年级';
-
- comment on column JT_CS.stuinfo.enroldate
-
- is '入学时间';
-
- comment on column JT_CS.stuinfo.idnumber
-
- is '身份证号';

通过上面Crate Table命令创建了stuinfo学生信息表后,还可以继续给表添加相应的约束来保证表数据的准确性。比如:学生的年龄不能存在大龄的岁数,可能是错误数据、性别不能填入不是1(男)、2(女)之外的数据等。
- --添加约束
-
- --把stuid当做主键,主键字段的数据必须是唯一性的(学号是唯一的)
-
- alter table JT_CS.STUINFO
-
- add constraint pk_stuinfo_stuid primary key (STUID);
-
- -- --给字段年龄age添加约束,学生的年龄只能0-60岁之内的
-
- alter table JT_CS.STUINFO
-
- add constraint ch_stuinfo_age
-
- check (age>0 and age<=60);
-
-
-
- --性别不能填入不是1(男)、2(女)之外的数据
-
- alter table JT_CS.STUINFO
-
- add constraint ch_stuinfo_sex
-
- check (sex='1' or sex='2');
-
- --年级
-
- alter table JT_CS.STUINFO
-
- add constraint ch_stuinfo_GRADE
-
- check (grade>='2000' and grade<='9999');

除了上边Create Table 语句可以创建数据表以外,使用Create Table AS语句一样可以通过复制现有表的列从现有表来创建新表。同时,Create Table 表名 as select 语句也可以实现对select查询的结果进行快速备份。
- --语法:SELECT语句可指定列或添加where条件
-
- CREATE TABLE new_table
-
- AS (SELECT * FROM old_table);
CREATE TABLE JT_CS.STUINFO_01 AS (SELECT * FROM JT_CS.STUINFO);
- 新创建的表命名为STUINFO_02,并具有与STUINFO相同的表字段和记录(性别参数小于2的所有记录);
-
- CREATE TABLE JT_CS.STUINFO_02 AS (SELECT * FROM JT_CS.STUINFO WHERE sex<2);
- -----语法:
-
- CREATE TABLE new_table AS (SELECT column_1, column2, ... column_n FROM old_table);
-
-
-
- -----示例:创建新表,指定STUNAME, SEX,AGE,CLASSNO四列,并选择SEX小于2的记录。
-
- CREATE TABLE scott.STUINFO_03 AS (SELECT stuname, sex,age,classno FROM scott.STUINFO WHERE sex<2);
- -----语法:
-
- CREATE TABLE new_table AS (SELECT column_1, column2, ... column_n FROM old_table_1, old_table_2, ... old_table_n);
-
-
- -----示例:
-
- CREATE TABLE scott.STUINFO_04 AS (SELECT scott.STUINFO.stuname, scott.STUINFO.sex, scott.STUINFO_01.age,scott.STUINFO_01.classno FROM scott.STUINFO,
-
- scott.STUINFO_01 WHERE scott.STUINFO.stuname = scott.STUINFO_01.stuname AND scott.STUINFO.sex < 2);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。