zoukankan      html  css  js  c++  java
  • Oracle之约束条件1:主键约束

    --建立一张用来存储学生信息的表:字段包含学号、姓名、性别,年龄、入学日期、班级,email等信息
    
    --学号是主键
    --姓名不能为空
    --性别默认值是男
    --年龄范围20---80岁
    --Email唯一
    
    --主键一个表中只能允许存在一个
    drop table student purge;
    create table student(
           sno number(6),
           sname varchar2(15),
           age number(2),
           sex   char(2)  default '',
           enterdate date,
           clazz varchar2(10),
           email varchar2(20),
           constraints PK_STUDENT primary key (sno),--表级别设置主键
           constraints PK_STUDENT1 primary key (email)
    );
    insert into student values (1,'SuperDrew',21,null,sysdate,'1001','XXXXXX@XXXX.XXXX');
    insert into student values (1,'Alia',99,null,sysdate,'1001','XXXXXX@XXXX.XXXX');
    insert into student values (1,'Bob',25,null,sysdate,'1001','XXXXXX@XXXX.XXXX');
    insert into student values (1,null,11,null,sysdate,'1001','XXXXXX@XXXX.XXXX');
    
    --第二种方式设定主键  
    drop table student purge;
    create table student(
           sno number(6) primary key ,--列级别设置主键 简化的操作
           sname varchar2(15),
           age number(2),
           sex   char(2)  default '',
           enterdate date,
           clazz varchar2(10),
           email varchar2(20)
    );
    
    
    --组合式主键
    --组合主键 只有指定的主键的列 数据都一样 才不能写入表中
    drop table student purge;
    create table student(
           sno number(6),
           sname varchar2(15),
           age number(2),
           sex   char(2)  default '',
           enterdate date,
           clazz varchar2(10),
           email varchar2(20),
           constraints PK_STUDENT primary key (sno,sname)--表级别设定主键
    );
    insert into student values (1,'KING',18,null,sysdate,'1002','XXXXXX@XXXX.XXXX');
    insert into student values (3,'Geny',18,null,sysdate,'1002','XXXXXX@XXXX.XXXX');
    insert into student values (2,'Tidy',18,null,sysdate,'1002','XXXXXX@XXXX.XXXX');
    
    
    select * from student;
    
    
    --联合主键只能通过表级别设定,列级别是不能设定联合主键
    drop table student purge;
    create table student(
           sno number(6) primary key,
           sname varchar2(15) primary key,
           age number(2),
           sex   char(2)  default '',
           enterdate date,
           clazz varchar2(10),
           email varchar2(20)
    );
  • 相关阅读:
    7、cad图纸打印
    对账
    练习10—去掉图片文字
    6、cad里面如何快速将图纸的线条全部变成黑色
    5、cad如何快速选中图纸上所有文字
    4、如何修改cad的背景色为白色
    练习九—快速修饰面部光影
    iOS alloc&init探索
    export default / export const
    Vue2.0+组件库总结
  • 原文地址:https://www.cnblogs.com/superdrew/p/8058204.html
Copyright © 2011-2022 走看看