數據庫知識點回顧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 ‘/’ 表示以_開頭的字符串

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