常用SQL語句查詢分享_MsSql

來源:互聯網
上載者:User

--建立資料庫(檔案:主要資料檔案mdf==1,次要資料檔案ndf>=0,記錄檔ldf>=1)
--檔案組:當1mdf,5個ndf(1,2,2),10個ldf(3,3,4),將它們分成多個組存放

CREATE database studb;

--建立表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

--建立課程表

create table course(cid int(10) primary key,cname varchar(20),tid int(10) REFERENCES teacher(tid));

--建立分數表

create table sc(scid int(10) primary key,sid int(10) REFERENCES student(sid),cid int(10) REFERENCES course(cid),score int(10));

--聯集查詢:等值查詢
--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>=60and s.sid = sc.sid and c.cid = sc.cid;

--3..
--子查詢:當條件也要查詢的時候,我只知道學號,我不知道"小張"這個欄位,那你知道小張的學號 嗎

delete from sc where sid = (select sid from student where sname = '小張'); 

--子查詢中間的符號一定是父查詢與子查詢兩張表關聯的欄位(一般是主外鍵)

--4..

update sc set score=score+5 where cid=????;select tid from teacher where tname='李老師' ==1select cname from course where tid = 1 ==課程名字,李老師教的select cid from course where cname='android' ==課程IDupdate 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='李老師')));

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.