Oracle新建数据表的两种方法

简介: Oracle新建数据表的两种方法

常用的Oracle列字段的数据类型:

  1. CREATE TABLE
CREATE TABLE schema_name.table_name (
    column_1 data_type column_constraint,
    column_2 data_type column_constraint,
    ...
    table_constraint
 );
--实例建表stu_info
create table SCA_CS.stu_info
(
  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 --身份证
)
--stu_info存储的表空间是users,storage表示存储参数:区段(extent)一次扩展64k,最小区段数为1,最大的区段数不限制。
tablespace USERS 
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table 
comment on table SCA_CS.stu_info is '学生信息表';
-- Add comments to the columns 
comment on column SCA_CS.stu_info.stuid is '学号';
comment on column SCA_CS.stu_info.stuname is '学生姓名';
comment on column SCA_CS.stu_info.sex is '学生性别';
comment on column SCA_CS.stu_info.age is '学生年龄';
comment on column SCA_CS.stu_info.classno is '学生班级号';
comment on column SCA_CS.stu_info.stuaddress is '学生住址';
comment on column SCA_CS.stu_info.grade is '年级';
comment on column SCA_CS.stu_info.enroldate is '入学时间';
comment on column SCA_CS.stu_info.idnumber is '身份证号';
  • 添加约束
--添加约束
--把stuid当做主键,主键字段的数据必须是唯一性的(学号是唯一的)
alter table SCA_CS.stu_info
  add constraint pk_stuinfo_stuid primary key (STUID);
-- --给字段年龄age添加约束,学生的年龄只能0-60岁之内的
alter table SCA_CS.stu_info
  add constraint ch_stuinfo_age
  check (age>0 and age<=60);
--性别不能填入不是1(男)、2(女)之外的数据
alter table SCA_CS.stu_info
  add constraint ch_stuinfo_sex
  check (sex='1' or sex='2');
--年级
alter table SCA_CS.stu_info
  add constraint ch_stuinfo_GRADE
  check (grade>='2000' and grade<='9999');
  1. CREATE TABLE AS
--语法:SELECT语句可指定列或添加where条件
CREATE TABLE new_table  
AS (SELECT * FROM old_table);


相关文章
|
4月前
|
SQL Oracle 关系型数据库
Oracle之替代OR的另一种方法
Oracle之替代OR的另一种方法
75 0
|
7月前
|
存储 Oracle Java
[亲测可用]hibernate调用Oracle存储过程|Spring Data JPA调用Oracle存储过程方法
[亲测可用]hibernate调用Oracle存储过程|Spring Data JPA调用Oracle存储过程方法
|
7月前
|
SQL Oracle 关系型数据库
Oracle数据库优化的总结及优化方法
Oracle数据库优化的总结及优化方法
56 0
|
10月前
|
SQL Oracle 关系型数据库
一种SqlServer数据迁移到Oracle的方法总结
一种SqlServer数据迁移到Oracle的方法总结
377 0
|
10月前
|
存储 SQL Oracle
Oracle 存储过程和方法全攻略:实战详解调用技巧与注意事项
Oracle 存储过程和方法全攻略:实战详解调用技巧与注意事项
382 0
|
10月前
|
存储 Oracle 关系型数据库
|
11月前
|
Oracle 关系型数据库 数据库
新建Oracle 数据库的Shell+SQL脚本
不用图形界面,也不用DBCA的静默方式,下面是用Shell+SQL创建Oracle数据库的脚本。
134 0
|
SQL Oracle 关系型数据库
【Oracle学习】—新建数据库连接,超详细
按键盘上的windows键,搜索Database Configuration Assistant,单击运行即可
【Oracle学习】—新建数据库连接,超详细
|
SQL Oracle 关系型数据库
Oracle文本数据导出方法总结
Oracle文本数据导出方法总结
680 0

推荐镜像

更多