数据库知识点回顾2

关于表间约束
create table infos(
	id char(5) primary key,
	stu_id char(5) unique,
	name char(10) not null,
	sex char(3) check(sex='男' or se='女'),
	address char(50) default '地址不详'	
);
create table scores(
	id char(5),
	s_id char(5),
	score number(4,1)--表示可以有小数
);

alter table 表名 add constraint 约束名 约束内容;
--主建约束
alter table scores
add constraint pk_scores_id primary key(id);

--唯一
alter table scores
add constraint u_scores_s_id unique(s_id);

--检查
alter table scores
add constraint ck_scores_score check(score>=0 and score <=100);

--外键
alter table scores
add constraint fk_scores_s_id foreign key(s_id) references infos(stu_id);

--非空约束
alter table scores modify score not null;

--默认约束
alter table scores modify score default 0;

--删除约束
alter table 表名 drop constraint 约束名;
--查询当前表的约束名
select * 
from all_constraints 
where table_name='INFOS';

--删除约束
alter table infos drop constraint SYS_00678;

关于查询
select *|列名|表达式
from 表名;

--从emp表中查找sal大于2000的数据,并按照降序排列(asc升序,一般默认)
select *
from emp 
where sal >2000
order by sal desc;
关于别名
--其实表里是只有dept表,和deptno loc列
--能别名显示为部分编号和位置,能通过d.列名来实现表数据访问
select d.deptno 部门编号,d.loc as 位置
from dept d;
关于字符串

关于字符串拼接
microsoft sql server :+
db2 oracle :||
mysql :只能用函数

--查询列中有空的人
select *
from 表名
where 列名 is null;
--查询某列包含1个以上的某个值
select *
from 表 别名
where 别名.列名 in('值1','值2');
--查询区间
select *
from sal
where sal between 1000 and 2000
order by sal desc;

like模糊查询 耗性能
%代表0个或多个任意字符
代表一个任意字符
link ‘衣字符串’ [escape ‘字符’]
/
% escape ‘/’ 表示以_开头的字符串

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章