首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

Oracle入门指南_三

2013-10-28 
Oracle入门指南_3select sysdate from dual/*dual为临时表,为了凑够select语句而设,后面还会多次使用到*/

Oracle入门指南_3
select sysdate from dual;/*dual为临时表,为了凑够select语句而设,后面还会多次使用到*/

?

创建表:
/*创建t_student表,没有设置主键*/create table t_student(       stuid number(10) not null,       stuname nvarchar2(20) not null,       age number(2) not null,       address nvarchar2(200),       codenum nvarchar2(18));/*创建t_score表,主键为scoreid*/create table t_score(       scoreid number(10) primary key,       score number(3,1) not null,       stuid number(10));
    ?给t_student表添加主键
    alter table t_student add constraint pk_t_student primary key(stuid);/*pk_t_student是主键约束的名称*/
      ?给t_student表中的age添加检查约束
      alter table t_student add constraint ck_t_student_age check(age>=18 and age<=25);--给student表中的age字段添加检查约束,年龄必须在18-25之间,否则存不进去,修改不成功
        ?给t_student表中的address添加默认约束,默认地址为'China'
        alter table t_student modify (address nvarchar2(200) default 'China');--把address字段重新定义了
          ?给t_student表中的codenum添加唯一约束
          alter table t_student add constraint un_t_student_codenum unique(codenum);--un_t_student_codenum是约束的名字,在这个表中这一项是唯一的,即没有重复的
            ?给t_score表添加外键约束,分数是属于哪一个学生的
            alter table t_score add constraint fk_t_score_t_student foreign key(stuid) references t_student(stuid);--t_score表中的stuid是外键,连接着t_student表
              ?删除约束
              alter table t_score drop constraint fk_t_score_t_student;--删除刚才给t_score表中建立的外键
              修改表:添加列
              alter table t_student add(sex nvarchar2(2));--给t_student表添加一列:sex
              ?修改列
              alter table t_student modify (codenum nvarchar2(20));--修改codenum的长度为20字节
                ?重命名列
                alter table t_student rename column sex to xingbie;--将sex重命名为xingbie
                  ?删除列
                  alter table t_student drop column xingbie;--删除xingbie列
                    ?重命名表
                    rename t_student to xuesheng;--重命名t_student为xuesheng表
                    ?删除表
                    drop table t_student;--删除t_student表,记得先把xuesheng表的名称改过来
                    总结:Oracle中SQL语句的组成Oracle中数据类型,数值型统一为Number表的创建,表中的约束的添加及删除对表进行修改,删除

热点排行