一、建表
建表语法:
create table 表名(
字段名 数据类型【默认值】【约束】,
字段名 数据类型 【default值】【constraint约束名】【约束类型】,
。。。。。
)
1. 合法标识符
2. 数据类型
3. 约束类型
4. 案例
-- 创建 team 表,有 id 和 name 两个字段create table team(id number( 3 ) constraint team_id_pk primary key,name varchar2( 15 ) not null);-- 创建 student 表,有字段( id name mobile , email , married 婚否, tid 外键)create table student(id number( 7 ) primary key,name varchar2( 15 ) not null ,mobile char ( 11 ) constraint student_mobile_nn not null constraint student_mobile_uk unique,email varchar2( 30 ) unique check( email like '%_@_%' ),birthday date default sysdate,married number( 1 ) default 0 check(married in ( 0 , 1 )), -- 应对 java 中的 boolean 类型数据tid number( 3 ) references team(id));
二. DML命令 ---- insert update delete
1. insert命令 ----- 插入
1)全表插入
语法:insert into 表名 values(值1,值2,......)
要求:
1.values后值的个数,类型数量必须与表里的字段保持一致
2.如果不方便给定值的字段(可以为空字段)必须使用null值占位
insert into student values ( 1 , 'baizhi' , '13810020030' , 'baizhi@123.com' ,sysdate);insert into student values ( 2 , 'luxw' , '12345678901' , 'luxw@123.com' , null );
2)选择插入
语法:
insert into表名(字段1,字段2,....)values(值1,值2,....);
要求:
1.value 后值的个数,类型,数量必须与表名后面给定的字段保持一致
2.对于表里‘非空’并且‘没有默认值’的字段必须出现
insert into student(id,name,mobile,email) values ( 3 , ' 翠花 ' , '12345678909' , 'cuihua@123.com' );
2. update 命令 ---- 更新
语法:
updayte表名 set 字段1=新值,字段2=新值,....where条件;
update student set name= 'baizhi3' ,mobile= '23456789012' where id= 3 ;
3. delete命令 ---- 删除数据
语法
delete from 表名 ....where条件;
三、数据库里的其他对象--序列 视图 索引
select seq1.nextval from dual;insert into team123 values(seq1.nextval,'aaa');
注意:如果在一条 sql 命令里多次调用 nextval,只会得到一个值。
2. 视图(View)
-- 1. 简化查询 , 提高查询语句的复用性-- 2. 帮助做表的权限管理-- 1 ) create view emp as select employee_id,last_name,…. from employees;-- 2 ) create view boss as select * from employees;-- 3 )禁掉 employees 表的访问权限 , 对外提供 emp 和 boss;
4)删除: drop view v1;
四. 事务(transaction) 【重点】
1. 概念
是操作数据库数据的最小单位,是由一组不可再分的 sql 命令组成的集合 ,事务的大小由实际的业务决定。
2. 原理
3. 边界
4. 数据安全 ---- 每一行记录都有一把锁(“行级锁”)
-- 查询时获取所有数据,并针对结果里的行进行加锁select … from … for update ;-- 对查询结果加锁时,以 nowait 方式加锁(如果无法加锁,直接返回)select … from … for update nowait ;-- 对查询结果加锁时,如无法直接加上,则等待 num 秒select … from … for update wait num;
5. 事务的四大特性(ACID)