码迷,mamicode.com
首页 > 数据库 > 详细

Basic SQL sentence

时间:2014-06-25 09:43:01      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   code   color   

create database ST;
use st;
create table Student(
Sno varchar(10) primary key ,
Sname varchar(15) ,
Ssex varchar(2) ,
Sage int ,
Sdept varchar(10)
);
create table Course(
Cno varchar(10) primary key ,
Cname varchar(15) not null,
Cpo varchar(10) ,
Ccredit int 
foreign key (Cpo) references Course(Cno),
);
--if  you want to study Cno,you must study Cpo first
--so Cpo must come from Cno ,it`s a foreign  key
create table SC(
Sno varchar(10) ,
Cno varchar(10),
Grade int,
foreign key (Sno) references Student(Sno),
foreign key (Cno) references Course (Cno),
);
insert into Student values(200215121,李勇,,20,CS);
insert into Student values(200215122,刘晨,,19,CS);
insert into Student values(200215123,王敏,,18,MA);
insert into Student values(200215125,张立,,19,IS);


---now can`t insert cpo so cpo is 5n167n6
insert into Course(Cno,Cname,Ccredit) values(1,数据库,4);
insert into Course(Cno,Cname,Ccredit) values(2,数学,2);
insert into Course(Cno,Cname,Ccredit) values(3,信息系统,4);
insert into Course(Cno,Cname,Ccredit) values(4,操作系统,3);
insert into Course(Cno,Cname,Ccredit) values(5,数据结构,4);
insert into Course(Cno,Cname,Ccredit) values(6,数据处理,2);
insert into Course(Cno,Cname,Ccredit) values(7,PASCSL语言,4);



insert into SC values(200215121,1,92);
insert into SC values(200215121,2,85);
insert into SC values(200215121,3,88);
insert into SC values(200215122,2,90);
insert into SC values(200215122,3,80);

--edit table 
alter table student add Sgrade int;
alter table student drop column Sgrade; 
--edit the data type of column
alter table student alter column Sname varchar(15);
--alter varchar(15) to varchar(20)
alter table course alter column Cname varchar(20);
alter table student alter column Sname varchar(20);
--insert 
insert into course(Cpo)values(1);

select Sno,Sname
from Student
where(Ssex=);

update course set Cpo =5 where Cno=1;

select *
from Course

update Course set Cpo=‘‘ where Cno=2
update Course set Cpo=1 where Cno=3
update Course set Cpo = 6 where  Cno=4
update Course set Cpo =7 where Cno=5
--can`t updata null
update Course set Cpo=‘‘ where Cno=6
update Course set Cpo=6 where Cno=7


--lower function 
select Sname,LOWER(Sdept)
from Student;

select Sname,Sdept
from Student

select Sno 
from SC;
--the distinct key can cancel the repeated result,distinct coresponding ALL ,and the default is all
select DIStinct Sno
from SC

select Sname,Sage
from Student
where Sage>=20;


select Sname,Sno
from Student
where Sno=(select distinct Sno
from SC
where Grade<85);

select Grade 
from SC
--between and is a open section it include 19 and 20 
select Sname,Sdept,Sage
from Student
where Sage BEtween 19 and 20;

select Sname,Ssex,Sdept
from Student
where Sdept IN(CS,MA);


select *
from Student;

select *
from Student
Where Sage not between 19 and 20 ;


--LIKE 
--[not] like ‘<String>‘ string  include ? / *
--if  String  did`nt include any wildcard ,it can be instead of by ‘=‘
use st;
go
select *
from Student
where Sno like200215121;
--one chinese word instead of two string position
select Sname
from Student
--where Sname like ‘李_‘
where Sname like 李%;

select Sname
from Student
where Sname not like 李%;

--transforlate _ & to common symbol use escape
-- this sentence instruct that the char follow ‘\‘ is just a common symbol
select Cno
from Course
where Cname like DB\_Designescape \ ;

update SC  set Grade=NULL where Cno=1;
-- null query 
select Sno,Cno
from SC
where Grade is null;

select *
from SC;


select *
from Student
where Ssex= and Sage <21 ;
-- in just equals many ‘or‘

--order by desc ->>down asc ->>up

select *
from Student
--order by Sage DESC ;
order by Sage ASC ;
 
 
 alter table SC  add Constraint chk_Grade check(Grade>0 and Grade <100);
 alter table SC drop constraint chk_Grade ;
 alter table Student add loc int ;
 alter table Student alter  column loc varchar(20) ; 
 alter table Student drop column loc ;
 
 --distinct cancel repeated values 
 
 /*count ->>count the num of row 
count( column) ->> count the num of values in column 
 sum(column ) ->> count 
 */
 select COUNT(*)
 from Student 
 

 

Basic SQL sentence,布布扣,bubuko.com

Basic SQL sentence

标签:des   style   class   blog   code   color   

原文地址:http://www.cnblogs.com/oaks/p/3807185.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!