好久不见,今天分享的是通过SQL语句创建与管理数据表,还有实现简单的单表查询。
一、通过SQL语句创建与管理数据表
1. 创建表格 create tablexx表名(列名 数据类型 列级完整性约束)
create table student (
sno char(8) not null primary key,
sname varchar(8) not null ,
sex char(2) not null default('男'),
birth smalldatetime not null,
classno char(3) not null,
entrance_date smalldatetime not null,
home_addr varchar(40) ,
);
create table course(
cno char(3) not null primary key,
cname varchar(20) not null,
total_perior smallint check(total_perior > 0), /*属性列的check 约束*/
credit tinyint check(credit > 0 and credit <= 6) ,
);
create table sc2(
sno char(8) not null ,
cno char(3) not null ,
grade tinyint check(grade >= 0 and grade <= 100),
primary key(sno,cno),
foreign key (sno) references student(sno),/*设置外键*/
foreign key (cno) references course(cno),
);
2.增加列 alter table 表名 add 列名 属性
alter table student add
sdept char(20) not null,
postcode char(6) check(postcode like '[0-9][0-9][0-9][0-9][0-9][0-9]') ;
alter table student add constraint ck_entrance_date_ check (entrance_date > birth);
alter table sc2 add constraint ck_grade_ default(0) for grade;/*加约束*/
alter table sc2 drop constraint ck_grade_;/*删约束*/
3.插入数据
insert
into student(sno,sname,sex,birth,classno,entrance_date ,home_addr,sdept,postcode)
values('20110001','张虹','男','1992/09/11','051','2011/09/01','南京','计算机系','200413'),
('20110002','林红','女','1991/11/12','051','2011/09/01','北京','计算机系','100010'),
('20110103','赵青','男','1993/05/11','061','2011/09/01','上海','软件工程','200013');
insert
into course(cno,cname,total_perior,credit)
values('001','高数',96,6),
('002','C语言程序设计',80,5),
('003','Java语言程序设计',48,3),
('004','Visual_Basic',48,4);
insert
into sc2(sno,cno,grade)
values('20110001','001',89),
('20110001','002',78),
('20110001','003',89),
('20110001','004',95),
('20110002','002',60),
('20110103','001',80);
insert into student(sno,sname,sex)
values('20101101','赵青','男');
insert into sc2
values('20110103','005',80);
5.修改数据
update course
set total_perior = 64, credit = 4
where cno = '002';
update sc2
set grade = grade*0.8
where cno = '002';
最终得到的student表,course表,sc2表如下:
二、单表查询
1. 基本查询
(1)查询全部信息,select * from xx表
(2)查询某几列信息,select 列1,列2 from xx表
select * from student;
select sno, sname from student
2. 查询时改变列标题的显示select 列1 标题名1 , 列2标题名2 from xx 表
select sno 学号,sname 学生,home_addr 家庭地址
from student;
3. 条件查询 where 语句
select sno,cno ,grade from sc2
where grade > 80;
select * from sc2
where grade >=75 and grade <=80;
select sno from sc2
where cno = '002' and grade > 80;
select sno,cno from sc2
where grade is null;
4. 基于IN子句的数据查询(或者的意思)
select * from course where cname in ('高数','C语言程序设计');
5. between….and (相当于≥70 and ≤80)
select * from sc2 where grade between 70 and 80;
6. like语句(实现模糊查询)
select * from student where sname like '张%';/* %表示任意长度的字符串*/
select * from student where sname like '_红%' or sname like '_虹%';/*_(下横线代表任意的单个字符)*/
select credit from course where cname like 'Visual\_Basic' escape '\'; /*escape ‘\’ 表示’\’为换码字符,跟在’\’后面的’_’不再具有通配符的含义,转化为普通字符*/
7. top 子句(topn 表示输出前n条记录,top n percent 表示输出的占结果总记录的n%)
select top 3 * from course ;
select top 20 percent * from course ;
8. 消除重复行(distinct)
select distinct cno from sc2;
9. 查询经过计算的值
select sname , DATEDIFF(YEAR,birth,getdate()) 年龄 from student;
10. 用orderby 对查询结果进行排序
select * from student order by classno,sno ;
select sname , DATEDIFF(YEAR,birth,getdate()) age from student order by age desc;
11. 用聚集函数
select count(distinct sno) 学生总人数 from student;
select AVG(grade) 平均成绩, max(grade) 最高分,min(grade) 最低分 from sc2 where cno = '002';
12. 用group by 分组
select classno,COUNT(*) 班级总人数 from student group by classno;
select sno,SUM(grade) 总成绩 from sc2 group by sno
having SUM(grade)>150;
select cno, count(*) 选课人数 from sc2 group by cno ;
13. Compute 和 compute by 查看明细,分组聚集
select sno , grade from sc2 order by sno compute sum(grade) by sno;
select sno , grade from sc2 order by sno compute sum(grade), max(grade), min(grade) by sno;
后面可能会分享复杂查询,视图,存储过程,触发器等,以上,全部。