DDL
Data Definition Language:数据定义语言
-
create:创建;drop:删除;alter:修改;rename:重命名;truncate:截断
--根据逻辑分析,几张表,表名,表中的内容,字段的约束 --满足三范式 --创建表 --create table 表名( -- 字段名 类型 (约束), -- 字段名.. --); --注意:表中的字段添加校验要求等等 1)直接加在表结构中,约束 2)在java代码中进行校验 --用户表 create table tb_user( userid number(5), username varchar2(15), --可变长字符串 ()默认指字节数 如果想要表示字符数 3 char userpwd varchar2(5 char), --()中的数字 如果想要表示字符数 3 char->3个字符 age number(3,1), --精确匹配3位数字,其中一位是小数 gender char(3), --定长字符串 ()中的数字 如果想要表示字符数 3 char->3个字符 email varchar2(30), tegtime date ); select * from tb_user; --删除表 drop table tb_user; --插入数据 insert into tb_user values (01, '张三', '你好啊', 18.12, '女', '123456@qq.com', sysdate); insert into tb_user values (02, '欧阳李四', '12345', 19.12, '女', '123456@qq.com', to_date('2019-11-02 15:15:15', 'yyyy-mm-dd hh24:mi:ss')); --定义表 --学生表 从表|子表 --添加约束1 字段后直接添加约束 没有约束名字,比较简单,但是不便于后期维护 --添加约束2 字段后直接添加约束并指定约束名字 便于后期维护,不满足约束条件的时候会提示约束名字 --添加约束3 字段后,创建表结构结束前添加约束,制定约束名字 --添加约束4 创建表结构结束后追加约束 create table temp_student( --主键约束 --sid number(5) primary key, sid number(5) constraint pk_sid primary key, --非空 sname varchar2(4 char) not null, --检查约束 gender char(1 char) constraint check_gender check(gender in('男','女')), --检查约束 age number(3) ,--check(age>=1 and age<=150), --默认 entryDate date default(sysdate), --唯一 sqq number(15) ,--unique, --所属班级 --外键约束 关联班级表主键字段值 cid cid number(6) ,--references temp_class(cid), --添加约束3 -- on delete set null 删除主表数据的同时为从表中引用的那些数据的外键字段值设置为null -- on delete cascade 删除主表数据的同时也删除掉从表中引用的那些数据 -- 不写 默认先删除从表引用了主表要删除数据的那些从表数据,再删除主表数据 constraint check_age check(age>=1 and age<=150), constraint fk_cid foreign key(cid) references temp_class(cid) on delete cascade ); --追加约束4 alter table temp_student add constraint haha unique(sqq); --班级表 主表|父表 create table temp_class( cid number(6) primary key, cname varchar2(30) unique not null ); --加入注释 comment on table temp_student is '学生表'; comment on column temp_student.sid is '学生编号'; --删除表 --主从表关系的两张表 --默认先删除从表,再删除主表 --删除主表的同时级联删除主从表的约束 drop table temp_student; drop table temp_class cascade constraint; --删除约束 alter table temp_student drop constraint haha; insert into temp_student values(1,'张三','女',150,sysdate-3,123456,1001); insert into temp_student values(2,'王五','男',19,sysdate,123456,1002); insert into temp_student(sid,sname) values(3,'lisi'); insert into temp_class values(1001,'java32期'); insert into temp_class values(1002,'java33期'); select * from temp_student; select * from temp_class; --删除数据 --主从表关系 --删除主表数据 --1 默认 不写 默认先删除从表引用了主表要删除数据的那些从表数据,再删除主表数据 --2 on delete set null 删除主表数据的同时为从表中引用的那些数据的外键字段值设置为null --3 on delete cascade 删除主表数据的同时也删除掉从表中引用的那些数据 delete from temp_student where cid = 1001; delete from temp_class where cid = 1001; --根据已有的表进行拷贝 --只拷贝表结构,不拷贝原表中的数据 --create table 表名 as select 字段列表 from 已有表 where 1 != 1; --拷贝表结构+数据 --create table 表名 as select 字段列表 from 已有表; create table emp_copy as select * from emp where deptno = 30; select * from emp_copy;
序列
-
一种工具使用
-
生成有规律的序号数字,为表中主键,或者类似主键的字段(设置值用的)
--创建序列 --create sequence 序列名 start with 起始值 increment by 步进; create sequence seq_user_id start with 10000 increment by 3; --序列名.nextval 获取这个序列工具的下一个值,无法后退 --序列名.currval 获取这个序列工具所表示的当前值 --如果第一次使用,一定先使用nextval select seq_user_id.nextval from dual; select seq_user_id.currval from dual; insert into t_user values(seq_user_id.nextval,'java99期'); --删除 drop sequence 序列名 删除序列以后,不影响表或者表中已有的数据 drop sequence seq_user_id; insert into t_user values(seq_user_id.nextval,'haha'); create table t_user( id number(6) primary key, name varchar2(30) )