sql数据库(查询)

一、创建数据库、数据表

1、点击数据库-新建数据库  保存数据库地址尽量修改 (默认c盘)

2、点击数据库-点开-表-新建数据表  

注意:数据库和数据表的名字设置需注意命名规范。首字母大写。也可以通过create 来创建

二、sql中常用的数据类型

1、 char (10):一个中文两个字符,定长。

2、nchar(10):放十个中文字符,定长。

3、varchar(10):不定长 5个中文字符

4、nvarchar(10):不定长10个中文字符

三、常用的select查询(注意选择当前查询的数据库)

 1、查询全部         select * from StuInfor(表名字)

 2、查询某个字段   select 字段 *from StuInfor(表名字)

 3、筛选查询  (where...and)    select *from StuInfor where Country='中国'    select *from StuInfor where Country='中国' and Sex='女'

 4、筛选查询  (where...or)      select *from StuInfor where Country='中国' and (ClassName='一班'or ClassName='二班')  

                                             select *from StuInfor where Country='中国' and ClassName in ('一班','二班')

 5、排序(order by)  select *from StuInfor order by age desc (降序)

 6、前几个用Top  : select top 3  * from StuInfor order by age desc 

                            select top 3 stuName from stuInfor order by age desc              

 7、区间查询: select * from stuInfor where age >=21 and age <=28

                    select * from stuInfor where age between 21 and 28

8、记录数查询 count(*)  select count(*) as stuCount from stuInfor where className='一班'

9、分组查询 group by   select count(*) as stuCount,country from stuInfor group by country

10、group by...having:  select count(*) as stuCount,country  from stuInfor group by country having  count(*)>2

11、模糊匹配 like:select * from stuInfor where stuName like '小%'  以...开头

                          select * from stuInfor where stuName like '%小'  以...结尾

                           select * from stuInfor where stuName like '%小%'  中间包含

12、求和sum: select sum(age)from stuInfor

13、最大max、最小min:select max(age)from stuInfor  select min(age)from stuInfor

14、平均值:select * from stuInfor where age >(select avg(age)from stuInfor ) 

15、联合查询:select SI.StuId,SI.StuName,SI.Age,SI.Country,SI.Sex,PE.EnglishScore,PE.ChineseScore from StuInfor SI left join Score PE on SI.StuId = PE.StuId  (right join  /  inner join 常用

原文地址:https://www.cnblogs.com/minguofeng/p/4893268.html