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);
    
    在这里插入图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

讓丄帝愛伱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值