修改字段格式的sql语句: alter table tablename alter column colname newDataType 比如:alter table mytable alter column mycol1 int ; 修改字段名 sp_rename 'made.[chegnji]', 'xingming', 'COLUMN'; 其中made是表格名,chegnji是字段名,xingming是新字段名字,COLUMN表示的是列的意思;

1.创建表格;

create table Customer (name,old,address,sex) values(varchar(50),int,char(50),int);

2.增加一列,也就是增加栏位名字、关键字)

alter table Customer  add Sales varchar(50);

3.删除一列;

alter table Customer drop sex;

4.复制表格;

1)只复制表格结构

第一种:select top 0 *into guanqiong from bb;

第二种: select name ,sex into newtable from bb where 1=2;或者select *into newtable from bb where 1=2;

2)复制表格结构和数据;

select name ,sex into newtable from bb where 1=1;

或者select *into newtable from bb where 1=1;

5.查找前几条记录 用top n(表示前几条);

select top 10 *from newtable where Sales > 980;

6.加入想把1个数据结构相同的的表复制另外一个表中;

insert into aaa select * from  Customer;

一节、数据表的查询(select)

select 字段列表 [as 别名], * from 数据表名

[where 条件语句]

[group by 分组字段]

[order by 排序字段列表 desc]

[LIMIT startrow,rownumber]

1、Select 字段列表 From 数据表

例:①、select id,gsmc,add,tel from haf (* 表示数据表中所有字段)

②、select 单价,数量,单价*数量 as 合计金额 from haf (As 设置字段的别名)

2、Select … from … Where 筛选条件式

筛选条件式:①、字符串数据: select * from 成绩单 Where 姓名='李明'

②、万用字符: select * from 成绩单 Where 姓名 like '李%'

select * from 成绩单 Where 姓名 like '%李%'

select * from 成绩单 Where 姓名 like '%李_'

③、特殊的条件式:

⑴= / > / < / <> / >= / <=

⑵AND(逻辑与) OR(逻辑或) NOT(逻辑非)

⑶Where 字段名称 in(值一,值二)

⑷Where 字段名称 Is Null / Where 字段名称 Is Not Null

3、Select … from … group by 字段

SQL函数:

SELECT sex,count(id) as women from `user` group by 'sex';

函数名描述函数名描述

AVG平均值Count计数

MAX最大值MIN最小值

Sum求和

4、Select … from … Order by 字段列表 desc(倒,如果直接写为顺序)

5、Select … from … LIMIT ".$start_rowno.",".($pagesize+1)

第二节 SQL语句实例应用

数据库说明:

student(学生表):

stdid int(11) id号

son char(5) 学号

sname char(20) 姓名

ssex tinyint(1) 性别

sage char(3) 年龄

sdept char(20) 所在系

course(课程表):

couid int(11) id号

cno char(5) 课程号

cname char(20) 课程名

cpno char(6) 选修课号

ccredit char(50) 学分

sc(学生选课表):

scid int(11) id号

cno char(5) 课程号

grade float 成绩

sno char(5) 学号

单表查询:

一、选择表中的若干字段:

查询指定列:

1、查询全体学生的学号与姓名;

select son,sname from student

2、查询全体学生的姓名、学号、所在系;

select sname,son,sdept from student

3、查询全体学生的详细记录;

select * from student

查询经过计算的值:

4、查全体学生的姓名及其出生年份

select sname,year(now())-sage as '出生年份' from student

5、查询全体学生的姓名、出生年份和所有系,要求用大(小)写字母表示所有系名

select sname as '姓名','出生与',year(now())-sage as '出生年份',UPPER(sdept) as '系别' from student

select sname as '姓名','出生与',year(now())-sage as '出生年份',lower(sdept) as '系别' from student

二、选择表中的若干记录:

消除取值重复的行:

6、查询选修了课程的学生学号

select distinct sno from sc

查询满足条件的记录:

比较大小:

7、查询计算机全体学生的名单

select sname from student where sdept='cs'

8、查询所有年龄在20岁以下的学生姓名及其年龄

select sname,sage from student where sage<20

9、查询考试成绩小于90分的学生的学号

select distinct sno from sc where grade<90

确定范围:

10、查询年龄在18-20岁之间的学生的姓名、系别和年龄。

select sname,sdept,sage from student where sage between 18 and 20

11、查询年龄不在19-20岁之间的学生的姓名、系别和年龄。

select sname,sdept,sage from student where sage not between 19 and 20

确定集合:

12、查询信息系(is)、数学系(ma)和计算机科学系(cs)学生的姓名和性别。

select sname,ssex from student where sdept in('is','ma','cs')

13、查询不是信息系(is)、数学系(ma)的学生的姓名、系别和年龄。

select sname,ssex from student where sdept not in('is','ma')

字符匹配(like '<匹配串>' %代表任意长度(长度可以为0)的字符串 ; _代表任意单个字符,汉字得用两个"__"):

14、查询学号为95001的学生的详细情况

select * from student where son like '95001'

15、查询所有姓名李的学生的姓名、学号和性别。

select sname,son,ssex from student where sname like '李%'

16、查询姓名是两个字学生的姓名、学号和性别。

select sname,son,ssex from student where sname like '____'

17、查询所有不姓李的学生姓名。

select sname from student where sname not like '李__'

涉及空值的查询:

18、某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩,查询缺少成绩的学生的学号和相应的课程号。

select sno,cno from sc where grade is null

19、查询所有有成绩的学生学号和课程号。

select sno,cno from sc where grade is not null

多重条件查询(and or):

20、查询计算机系年龄在20岁的学生姓名。

select sname from student where sdept='cs' and sage=20

21、查询信息系(is)、数学系(ma)和计算机科学系(cs)学生的姓名和性别。

select sname,ssex from student where sdept='is' or sdept='ma' or sdept='cs'

三、对查询结果排序:

22、查询选修了3号课程的学生的学号及其成绩,查询结果按分数的降序排列。

select sno,grade from sc where cno='3' order by grade desc

23、查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。

select * from student order by sdept,sage desc

四、使用集函数:

24、查询学生总人数。

select count(*) as '总人数' from student

25、查询选修了课程的学生人数。

select count(distinct sno) as '人数' from sc

26、计算1号课程的学生平均成绩

select format(avg(grade),2) as '平均成绩' from sc where cno='1'

27、查询选修1号课程的学生最高分数。

select max(grade) from sc where cno='1'

五、对查询结果分组:

28、求各个课程号及相应的选课人数。

select cno as '课程号',count(sno) as '人数' from sc group by cno

29、查询选修了3门以上课程的学生学号。

select sno from sc group by sno having count(*)>2

注:where 子句与 having 短语的区别在于作用对象不同,where 子句作用于基本表或视图,从中选择满足条件的记录,having短语作用于组,从中选择满足条件的组。

多表查询

同时查询两个以上的表,称为连接查询。

等值连接:当连接运算符为=时,为等值连接。

1、查询每个学生及其选修课程的情况(等值连接)。

select student.*,sc.* from student,sc where student.son=sc.sno

自然连接:在等值连接中把目标列中重复的属性列去掉。

2、查询每个学生及其选修课程的情况(自然连接)。

最新文章

  1. Dynamics CRM 2015-如何修改Optionset Default Value
  2. iOS中获取当前时间,设定时间,并算出差值
  3. day11_API第一天
  4. 【AdaBoost算法】强分类器训练过程
  5. Eclipse和MyEclipse 手动设置 Java代码 注释模板
  6. Cookie操作
  7. win7 64 下安装ubuntu14.04
  8. 加载驱动模块时Device or resource busy的解决方法
  9. 共有49款Windows GUI开发框架开源软件 【转】
  10. [Swift]LeetCode348. 设计井字棋游戏 $ Design Tic-Tac-Toe
  11. Spring Boot(三):AOP&amp;日志操作&amp;异常处理
  12. gzip对字符串的压缩和解压
  13. 第一次使用eclipse出现的问题
  14. java利用线程池处理集合
  15. SQL-41 构造触发器
  16. Alpha冲刺10
  17. 图解 Java 内存模型
  18. centos7安装maven,git
  19. core Animation之CATransition(转场动画)
  20. ExtJS 4.2 教程-08:布局系统详解

热门文章

  1. Rocket - decode - Inst Decode
  2. [译]深入理解JVM Understanding JVM Internals
  3. Java 第十一届 蓝桥杯 省模拟赛 19000互质的个数
  4. (Java实现) 洛谷 P1387 最大正方形
  5. PAT 到底买不买
  6. PAT 旧键盘打字
  7. mysql基础之-mysql查询缓存(九)
  8. 嵌入式Linux内核开发工程师必须掌握的三十道题
  9. HDU-3033 I love sneakers! 题解
  10. Redis学习笔记(二十一) 事务