SQL语句基本练习

数据库基本知识i:
1、select、update、delete、insert into
2、create database 创建数据库
alter database 修改数据库
create table 创建新表
alter table 更改数据库表
drop table 删除表
create index 创建索引
drop index 删除索引
3、select * from table_name
select firstname from table
select distinct firstname from table :输出表table中firstname列的值,且对这些值去重(distinct)
select * from Person where City=“Beijing” : 从表Person中搜索出City为beijing的人
select * from Person where Year>1980 :从表Person中搜索出年龄Year大于1980的人
select * from Person where firstname = “aaa” and lastname = “bbb”
select * from Person where firstname = “aaa” or lastname = “bbb”
select company, number from Person order by company: 从表Person中搜索出company,number的值,按company的首字母升序排序
select company,number from Person order by number DESC : 从表Person中搜索出company,number的值,按number降序排列

4、insert into table (列1,列2,列3) values (值1,值2,值3)
insert into Person values (‘li’,‘xj’,‘beijing’,‘1980’) :给所有列插入数据
insert into Person (firstname,lastname,year) values (‘li’,‘xj’,‘1980’) :给指定的 列firstname,lastname,year插入数据

5、update Person set firstname=‘li’ where lastname = ‘xj’ :更新某一行中的一个列
update Person set firstname=‘li’,city=‘beijing’ where lastname=‘xj’

6、 delete from Person where = firstname=‘li’:删除一行
delete from Person :在不删除表的情况下删除所有行

7、select top 2 * from Person :返回2条数据
select top 50 percent * from Person :从Person表中选取50%的数据
select * from Person where city like ‘N%’ : 从Person表中选取以‘N’开始的城市里的人
select * from Person where city like ‘%N’ : 从Person表中选取以‘N’结尾的城市里的人
select * from Person where city not like ‘%lon%’ : 从Person表中选取不包含‘lon’的城市里的人
select * from Person where lastname in (‘li’,‘wang’)
select * from Person where lastname between ‘li’ and ‘wang’ : 包含li和不包含wang之间的人
select * from Person where lastname not between ‘li’ and ‘wang’
select po.order_id,p.firstname,p.lastname from Person As p, Product_Orders As po where p.firstname = ‘li’ and p.lastname = ‘xj’
Person,Product_Orders是两张表,输出李小静的所有订单
select Person.firstname,Person.lastname, Order.OrderNo from Person,Order where Person.Id_p = Order.Id_p :哪些人订购了产品,订购了哪些产品
内连接:inner join
select Person.firstname,Person.lastname, Order.OrderNo from Person inner join Order on Person.Id_p = Order.Id_p : 同上
左连接:left join,会从左表Person中返回所有行,即使右表Order没有匹配的行
select Person.firstname.Person.lastname. Order.OrderNo from Person left join Order on Person.Id_p = Order.Id_p: 列出所有人,以及他们的订单如果有的话
右连接:right join, 会从右表Order中返回所有行,即使左表Person没有匹配的行
select Person.firstname,Person.lastname, Order.OrderNo from Person right join Order on Person.Id_p = Order.Id_p: 列出所有订单,以及订购它们的人如果有的话
全连接:full join, 如果 “Person” 中的行在表 “Order” 中没有匹配,或者如果 “Order” 中的行在表 “Person” 中没有匹配,这些行同样会列出
select Person.firstname,Person.lastname,Order.OrderNo from Person full join Order on Person.Id_p = Order.Id_p: 列出所有人和他们的订单;以及所有的订单和订购它们的人

8、union:用于合并两个或者多个select语句的结果集(重复的结果只显示一次)
select E_name from Emplyees_China union select E_name from Emplyees_USA :列出所有在中国和美国的不同的雇员名(重复的只显示一个)
union all:用于合并两个或者多个select语句的结果集(重复的结果会全部显示)
select E_name from Emplyees_China union all select E_name from Emplyees_USA : 列出所有在中国和美国的不同的雇员名(重复的都会显示)
select * into Person_backup from Person: 备份表Person
select firstname,lastname into Person_backup from Person
select Person.firstname,Order.OrderNo into Person_Order_backup inner join Order on Person.Id_p = Order.Id_p: 创建两个表Person和Order的备份表

9、函数:
select avg(OrderPrice) from Order : 取Order中OrderPrice的平均值
select Customer from Order where OrderPrice > (select avg(OrderPrice) from Order) :搜索OrderPrice大于平均值的客户
select count(*) from Order: 返回表中的记录数目
select count(column_name) from Order : 返回列column_name的值的数目(Null不计入)
select count(distinct(column_name)) from Order
select count(Customer) from Order where Customer=‘lixj’ : 计算顾客为lixj的订单个数
select first(Customer) from Order : 返回一列中第一个记录值
select last(Customer) from Order : 返回一列中最后一个记录值
select max(OrderPrice) from Order : 返回一列中的最大值
select min(OrderPrice) from Order : 返回一列中的最小值
select sum(OrderPrice) from Order : 返回一列数值的总和
select Customer, sum(OrderPrice) from Order group by Customer : 查找每个客户的总金额,并按客户进行分组
select Customer,sum(OrderPrice) from Order group by Customer having sum (OrderPrice)<2000
select ucase(firstname) from Person : 把firstname转化为大写
select lcase(firstname) from Person : 把firstname转化为小写

你可能感兴趣的:(测试)