Oracle第三课课后作业

Oracle第三课课后作业

-- 一个系统有一个用户注册的功能,用户的信息有编号,用户名,年龄,性别,住址,备注,联系方式,密码的信息,其中用户名和密码及联系方式不能为空,强制要求,如果为空则报错,性别和年龄也不能为空,但是可以给默认值,0:男,年龄可以给值.

  1. 在建表时添加约束

    -- 创建用户表
    create table t_user(
    id number(5) primary key,
    username char(5) not null,
    psw varchar(10) not null,
    age number(3)  default 0 not null,
    sex number(1)  default 0 not null,
    address varchar(10),
    remark varchar(100),
    phone char(11) not null
    )
    ;
  2. 建好表之后添加约束

    create table t_user(
    id number(5),
    username char(5),
    psw varchar(10),
    age number(3),
    sex number(1),
    address varchar(10),
    remark varchar(100),
    phone char(11)
    );
    -- 给id设置主键
    alter table t_user add constraint t_pk primary key(id);
    -- 给username设置非空约束
    alter table t_user modify username not null;
    -- 给psw设置非空约束
    alter table t_user modify psw not null;
    -- 给age设置非空约束
    alter table t_user modify age not null;
    -- 给sex设置非空约束
    alter table t_user modify sex not null;
    -- 给phone设置非空约束
    alter table t_user modify phone not null;
    -- 给age设置默认值
    alter table t_user modify age default 0;
    -- 给sex设置默认值
    alter table t_user modify sex default 0;
  3. 测试数据

    -- 插入数据
    INSERT INTO t_user(id,username,psw,phone) VALUES(1,'tom',123,17735746651);
    INSERT INTO t_user(id,username,psw,phone) VALUES(2,NULL,NULL,NULL);
    -- 查询t_user表
    select * from t_user;
    -- 删除用户表
    drop table t_user;

     



软件下载提取码:qwer
原文地址:https://www.cnblogs.com/ty0910/p/14282437.html