数据库08笔记(设计表)

--create table 表名(
--字段名 类型(长度) 约束,
--...其他字段....
--..约束........
--);

create table tb_user(
       userid number(5),
       username varchar2(30),
       userpwd varchar2(20),
       gender char(3)  
)

select * from tb_user;

--加入注释
comment on table tb_user is '用户表';
comment on column tb_user.userid is '流水号,主键';
comment on column tb_user.username is '用户名';
comment on column tb_user.userpwd is '密码';
comment on column tb_user.gender is '性别';

--已有表中拷贝结构
--create table 表名 as select 字段列表 from 已有表 where 1!=1;  
create table copy_emp as select * from emp where 1!=1;  -- 只拷贝结构不拷贝数据
create table copy_emp2 as select empno,ename,sal from emp;  --结构和数据全部拷贝
select * from copy_emp;
select * from copy_emp2;

--删除表
drop table copy_emp2;
--drop table 表名 cascade constraints



--创建表
--学生表
create table tb_student(
       --学号  主键  唯一+非空
       sid number(5),
       --学生名字不能为空
       sname varchar2(30),
       --年龄不能超过18~150
       sage number(3),
       --不是男就是女
       sgender char(3),
       --唯一的
       sqq varchar2(30)
)
--表+约束 (1.字段后直接给约束|字段后指定约束名字添加约束  2.在创建表结构的最后指定约束)  3.表结构之后追加约束
create table tb_student(
       --学号  主键  唯一+非空
       --sid number(5) primary key,
       --sid number(5) constraints pk_student_sid primary key,
       sid number(5),
       --学生名字不能为空
       --sname varchar2(30) not null,
       sname varchar2(30)constraints sname_notnull not null,
       --年龄不能超过18~150
       sage number(3) check(sage between 18 and 150),
       --不是男就是女
       sgender char(3) check(sgender='男' or sgender = '女'),
       --唯一的
       sqq varchar2(30) unique
)

--表结构最后定义约束
create table tb_student(
       --学号  主键  唯一+非空
       sid number(5),
       --学生名字不能为空
       sname varchar2(30),
       --年龄不能超过18~150
       sage number(3),
       --不是男就是女
       sgender char(3),
       --唯一的
       sqq varchar2(30),
       classid number(3),
       
       --定义约束  指定名字,指定约束 ,指定字段
       constraints pk_student_sid primary key(sid),
       constraints sname_notnull check(sname is not null),
       constraints ck_sage check(sage between 18 and 150),
       constraint fk_student_classid_classid foreign key(classid) references clazz(classid) on delete set null --on delete cascade 
)
--表结构后面追加约束
alter table tb_student add constraints student_sqq unique(sqq);
--删除约束
alter table tb_student drop constraints student_sqq;

--班级表
create table clazz(
       classid number(3) primary key,
       classname varchar2(18) not null,
       subject varchar2(15) not null
)

insert into clazz values(01,'27期','java');
insert into clazz values(02,'28期','java');
select * from clazz;
drop table clazz cascade constraints; --主表

delete from clazz where classid=02 ;

drop table tb_student;
select * from tb_student;
insert into tb_student(sid,sname,sage,sgender,sqq,classid) values(1,'杨建秋',23,'男',700707070,02);
insert into tb_student(sid,sname,sage,sgender,sqq,classid) values(2,'杨',23,'男',7007070702,03);
insert into tb_student(sid,sname,sage,sgender,sqq) values(2,'hah',23,'男',700707070);
insert into tb_student(sid,sname,sage,sgender,sqq) values(3,'hah',23,'男',700707070);
delete from tb_student where sid=1;

--存在主外键关系:
--删除数据:1.先删除从表中引用主表中当前这条数据的那些数据,然后再删除主表中的数据  ---默认
         --2.直接删除主表中的数据,一起把从表中引用了当前要删除的主表中的数据的这些子记录删除--on delete cascade 级联删除     
         --3.在删除主表中数据的时候,从表中引用了这个数据的子记录设置为null 
--删除主表:1.先删除从表,再删除主表
         --2.直接删除主表,级联删除主从表之间约束     
         
         
--序列         
--create sequence 序列名 start with 起始值 increment by 步进;
create sequence seq_classid start with 100 increment by 2;
--1)、currval :当前值
--2)、nextval:下个值
select seq_classid.nextval from dual;
select seq_classid.currval from dual;
insert into clazz values(seq_classid.nextval,'27期','java');

--drop sequence 序列名
drop sequence seq_classid;
                       

--insert into 表名 values(和表结构顺序和个数和类型一致的数据可以手写也可以从别的表中获取的);
insert into  tb_student values(4,'hah',18,'男',21324324,null);                    

insert into tb_student(sid,sname) values(seq_classid.nextval,(select ename from emp where sal=800));              

--insert into 表名 select 查询列 from 源表 where 过滤数据;
insert into copy_emp select * from emp;
select * from copy_emp;
                                                   
--update 表名 set 字段=值 [,....] where 过滤行记录;
update clazz set subject='大数据';                       
update clazz set subject='py' where classid=106; 

--delete [from] 表名 where 过滤行记录
delete from clazz;
delete tb_student where sid=3;


--删除数据  delete会开启事务
delete from tb_student;
delete from clazz;
--数据截断  truncate  如果存在主从表关系,检查整个表结构是否有被引用,如果有结构上的引用就不能删除  不会开启事务
truncate table tb_student;
truncate table clazz;

--事务
insert into clazz values(03,'29期','java');
insert into clazz values(05,'31期','java');
delete from clazz where classid=1;

create table haha(
       id number(2)
)

select * from clazz;

--事务的开启: 还行DML语句时候 insert update delete
--事务的结束: 提交 1.ddl语句  2.正常执行完 DCL 语句 C 3.正常退出客户端  4.commit
         --   失败: 1.rollback 2.非法退出客户端
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值