SQL基本练习

Posted yuanninesuns

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL基本练习相关的知识,希望对你有一定的参考价值。

1.sql对大小写不敏感
2.sql执行顺序select--from--where--group by--having--order by 
3.SQL 使用单引号来环绕文本值(大部分数据库系统也接受双引号)。如果是数值,请不要使用引号。
4.ORDER BY 语句默认按照升序对记录进行排序。


select name from persons
select * from persons
select name,sex from persons
select distinct name from persons

select * from persons where city=beijing
select * from persons where year>1996

select * from persons where firstname=thoma and lastname=carter
select * from persons where firstname=thoma or lastname=carter
select * from persons where (firstname=thoma or firstname=willian) and lastname=carter

select company,ordernumber from orders order by company
select company,ordernumber from orders order by company,ordernumber
select company,ordernumber from orders order by company desc
select company,ordernumber from orders order by company desc, ordernumber asc

insert into table_name values (1,2,3)
insert into table_name (name,sex,id) values (yuanxu,1,2)

update persons set firstname=fred where lastname=willian
UPDATE Person SET Address = Zhongshan 23, City = Nanjing WHERE LastName = Wilson

delete from persons where name=bob
delete from table_name
delete * from table_name

select * from persons limit 2
select top 2 * from persons
select top 50 percent * from persons

select * from persons where city like %lon%
select * from persons where city not like %l

select * from persons where lastname like C_r_er 
我们希望从上面的 "Persons" 表中选取居住的城市以 "A" 或 "L" 或 "N" 开头的人
select * from persons where city like [ALN]%
select * from persons where city like [!ALN]%
select * from persons where city like [^ALN]%

select * from persons where lastname in (adams,carter)

select * from persons where lastname between adams and carter
select * from persons where lastname not between adams and carter

别名
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

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons, Orders
WHERE Persons.Id_P = Orders.Id_P 

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.Id_P = Orders.Id_P
ORDER BY Persons.LastName

UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。
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

备份表
select * into persons_back from persons
select * into persons in backup.mdb from persons
select lastname,firstname into persons_backup from persons
select lastname,firstname into persons_backup from persons where cirt=beijing

SELECT Persons.LastName,Orders.OrderNo
INTO Persons_Order_Backup
FROM Persons
INNER JOIN Orders
ON Persons.Id_P=Orders.Id_P

create database my_db
create table persons {ip int, name varchar(255)}

create index personindex on person(lastname desc)
create index personindex on person (lastname, firstname)

CREATE VIEW [Current Product List] AS
SELECT ProductID,ProductName
FROM Products
WHERE Discontinued=No

 

以上是关于SQL基本练习的主要内容,如果未能解决你的问题,请参考以下文章

SQL的基本语法练习

PL/SQL练习基本的PL/SQL语句

sql sql里面的代码片段

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

SQL Prompt7.2下载及破解教程

Microsoft SQL Server 代码片段收集