SQL 命令学习

1.Select
Select * from table
2.select distinct 取列中不同的值
Select distinct 列名称 from 表名称
3.Where
Where操作运算符:=,<>,>,<,>=,<=,BETWEEN(在某个范围内),LIKE(搜索某种模式)
Select 列名称 from 表名称 where 列 运算符 值
例:Select * from persons where city=‘北京’
4.And,or
例:Select * from persons where city=‘北京’ or city=‘上海’
例:select *from persons where (city=‘北京’ or city=‘上海’)and city=‘天津’
5.Order by 对结果集进行排序 默认为升序,降序关键字 DESC
例:Select company,ordernumber from orders order by company,ordernumber
例:Select company,ordernumber from orders order by company,ordernumber desc
6.Insert into 向表格中插入新的行
Insert into 表名 values(值1,值2)
Insert into table_name(列1,列2)values(值1,值2)
Insert into persons values(‘gates’,‘bill’,‘xuanwumen’,‘beijing’)
Insert into persons (11,22) values(‘11’,‘22’)
7.Update 修改表内数据
Update 表名称 set 列名称=新值 where 列名称=某值
8.Delete 删除表中的行
Delete from person where lastname=‘111’ 输出列lastname=111的行
子句
1.Top 规定要返回的记录的数目
语法:select top number|percent column_name(s) from table_name
例:select top 5 * from persons
例:select top 50 percent * from persons 表中选取50%的记录
2.Like 用于在where子句中搜索列中的指定模式
例:select * from persons where city like ‘n%’ “%” 可用于定义通配符(模式中缺少的字母)
例:select * from persons where city like ‘%g’ 搜索以g结尾的城市
例:select * from persons where city like ‘%lon%’ 搜索包含‘lon’的城市
例:select * from persons where city not like ‘%lon%’ 搜索不包含‘lon’的城市
3.通配符
Sql通配符必须与like运算符一起使用
% 替代一个或多个字符
_ 仅替代一个字符
[charlist] 字符列中的任何单一字符
[^charlist]或[!charlist] 不在字符列中的任何单一字符
4.In 操作符允许在where子句中规定多个值
Select column_name(s) from table_name where column_name in(value1,value2)
例:select * from persons where lastname in(‘111’,‘222’)取出111和222的列
5.Between 包含 not between不包含
例:select * from persons where lastname between ‘111’and‘222’
6.Alias 为列名称或表名指定别名 根据AS位置的不同确定 列或表
例:Select po.orderid,p.lastname,p.firstname
from persons AS p,product_orders AS po
where p.lastname=’adams’ and p.firstname=’john’
例:select lastname as family,firstname as name from persons
Family Name
Adams John
Bush George
Carter Thomas

7.Join,inner join 根据两个或多个表中的列之间的关系,从这些表中查询数据
引用两个表:select persons.lastname,persons.firstname,orders.orderno from persons,orders where persons.id_p=order.id_p
从persons表,orders表中取出id_p数值相等的lastname,firstname,orderno列
包含join的使用方法
Select persons.lastname,persons.firstname,orders.orderno from persons inner join orders on person.id_p=orders.id_p order by persons.lastname
8.left join ,right join(与left join 相反)从左表返回所有的行,即使右表没有匹配的行。
9.Full join 只要某个表中存在匹配,关键字就会返回行
10.Union 合并两个或多个select语句的结果集
例 select column_name from table_name1 union select column from table_name2
Union 默认选取不同的值,如需要重复值 使用union all
11.primary key 主键
例CREATE TABLE Persons
(
Id_P int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
12.Foreign key 外键
13.Check check(id>0) 只允许id列值大于0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值