常用SQL語(yǔ)句查詢分享
--創(chuàng)建數(shù)據(jù)庫(kù)(文件:主要數(shù)據(jù)文件mdf==1,次要數(shù)據(jù)文件ndf>=0,日志文件ldf>=1)
--文件組:當(dāng)1mdf,5個(gè)ndf(1,2,2),10個(gè)ldf(3,3,4),將它們分成多個(gè)組存放
CREATE database studb;
--創(chuàng)建表teacher,student
create table teacher ( tid int(10) primary key auto_increment, tname varchar(20), tage int(10) ); use studb;
create table student ( sid int(10) primary key auto_increment, sname varchar(20), sage int(10), tid int(10) REFERENCES teacher(tid) );
--外鍵約束:你問張三的老師是誰??
--select teacher.tname from teacher,student where student.sname = '張三' select t.tname from teacher t,student s where s.sname = '張三' and t.tid = s.tid
--創(chuàng)建課程表
create table course ( cid int(10) primary key, cname varchar(20), tid int(10) REFERENCES teacher(tid) );
--創(chuàng)建分?jǐn)?shù)表
create table sc ( scid int(10) primary key, sid int(10) REFERENCES student(sid), cid int(10) REFERENCES course(cid), score int(10) );
--聯(lián)合查詢:等值查詢
--1..
select c.cname from course c,student s,sc where s.sname = '小張' and s.sid = sc.sid and c.cid = sc.cid;
--2..
select sname from student s,course c,sc where c.cname='android' and sc.score>=60 and s.sid = sc.sid and c.cid = sc.cid;
--3..
--子查詢:當(dāng)條件也要查詢的時(shí)候,我只知道學(xué)號(hào),我不知道"小張"這個(gè)字段,那你知道小張的學(xué)號(hào) 嗎
delete from sc where sid = (select sid from student where sname = '小張');
--子查詢中間的符號(hào)一定是父查詢與子查詢兩張表關(guān)聯(lián)的字段(一般是主外鍵)
--4..
update sc set score=score+5 where cid=????; select tid from teacher where tname='李老師' ==1 select cname from course where tid = 1 ==課程名字,李老師教的 select cid from course where cname='android' ==課程ID update sc set score=score+5 where cid= ( select cid from course where cname= ( select cname from course where tid = ( select tid from teacher where tname='李老師' ) ) );
相關(guān)文章
sqlserver數(shù)據(jù)庫(kù)大型應(yīng)用解決方案經(jīng)驗(yàn)總結(jié)
對(duì)于一個(gè)大型的互聯(lián)網(wǎng)應(yīng)用,每天百萬級(jí)甚至上億的PV無疑對(duì)數(shù)據(jù)庫(kù)造成了相當(dāng)高的負(fù)載。對(duì)于系統(tǒng)的穩(wěn)定性和擴(kuò)展性造成了極大的問題2013-10-10檢測(cè)SqlServer數(shù)據(jù)庫(kù)是否能連接的小技巧
這篇文章主要介紹了檢測(cè)SqlServer數(shù)據(jù)庫(kù)是否能連接的小技巧,本文使用UDL文件的形式實(shí)現(xiàn)這個(gè)小方法,需要的朋友可以參考下2015-02-02SQL Server無日志恢復(fù)數(shù)據(jù)庫(kù)(2種方法)
SQL Server數(shù)據(jù)庫(kù)中的日志文件可能會(huì)由于一些突發(fā)事件或者失誤造成丟失的嚴(yán)重后果,大家都知道,SQL Server數(shù)據(jù)庫(kù)中日志文件是很重要的,所以要及時(shí)的將丟失的日志文件給找回來。下文就為大家介紹一種恢復(fù)數(shù)據(jù)庫(kù)日志文件的方法。2015-08-08SQL?Server?數(shù)據(jù)庫(kù)基礎(chǔ)編程詳解
這篇文章主要為大家介紹了SQL?Server?數(shù)據(jù)庫(kù)基礎(chǔ)編程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01SQL 超時(shí)解決方案 有時(shí)并不是設(shè)置問題
SQL 超時(shí)解決方案 有時(shí)并不是設(shè)置問題,大家可以參考下。2010-05-05