您的位置:首页 > 数据库

SQl 学习阶段的总结

2016-01-30 15:09 429 查看
SQL

注:SQL中 字段是用放单引号里面 连接字符 并且或者:and /or 真假:1/0 不等于<>/ !==

where 1=1 全部选择中 where 1=2 表示全部都不选

1.创建数据库/删除数据库

Create database databasename

Drop database databasename

2.创建表/删除表

Create table tablename

表字段 表的类型/长度 表的约束

(create table KxUser userid int identity(1,1) not null,

username nvarchar(20) not null)

Drop table tablename

3.表中增加字段

Alter table 表名 add 添加的字段 字段的类型 字段的约束 ,添加的字段 字段的类型 字段的约束

列增加后将不能删除。数据类型也不能改变,唯一能改变的是增加nvarchar类型的长度

4.SQL 的查询、插入 、删除、修改、求和、平均数、最大值、最小值

查询:select * [查询的字段名] from [tablename] where [condition]

插入:insert into [tablename](field1,field2,field3)values(value1,value2,value3)

insert into [tablename] values(value1,value2,value3) [注意:这里必须添加tablename表的所有字段的值]

删除:delete from [tablename] where [condition]

修改:update [tablename] set [field]=[fieldvalue] where [condition]

求和:select count(*)[field] from [tablename] where [condition]

平均数:select avg(field) from [tablename] where [condition]

最大值:select max(field) from [tablename] where [condition]

最小值:select min(field) from [tablename] where [condition]

select * from tablename where [condition] city like ‘%value%’ 通配符

group by field 注:分组

order by field (asc/desc)默认 asc 注:排序

通配符

%:代替一个或者多个字符

-:代替一个字符

[clist] 字符列中的任何单一字符 列如:[a_z] a 到z 中任何一个都匹配

[^clist] /[!clist] 不在字符中的任何单一字符 列如:[^a_z] 不在a 到z 中任何一个都匹配

查找10到15的记录

select top 5 * from (select top 15 from user ) userinfo order by userid desc

5.SQL中 操作符 top number/percent(百分比) in between ……and as

select top 5 userid from KxUser

select top 50 percent userid from Kxuser

in

select userid from Recharge where useid in (select userid from userinfo )

select userid from user where userid in(‘201302246’,’201302217’)

between……and :

select * from mark where score (not) between 60 and 100

as :

select count (*)as 人数的统计 from user whew 性别=‘男’

inner join 内连接(userid 在Recharge 没有匹配就没有行)

select userid username from user inner join Recharge on user.userid=Recharge.id

full join 全连接(userid 在Recharge 没有匹配 就会根据条件全列出来)

select userid username from user full join Recharge on user.userid=Recharge.id

left join 左连接(左连接会从左里返回所有行,即使右表没有匹配的中)

select userid username from user left join Recharge on user.userid=Recharge.id

right join 右连接(右连接会从左里返回所有行,即使左表没有匹配的中)

select userid username from user right join Recharge on user.userid=Recharge.id

union(联合)select 语句必须拥有相同数量列,列必须拥有相似的类型,顺序必须相同

注释union 操作符选择不重复的值/union all 选择重复的值

select E-name from Employees_China

union

select E-name from Employees_USA

select E-name from Employees_China

union all

select E-name from Employees_USA
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据库 sql