SQL 入门

简介

SQL: 结构化查询语言, 是访问和操作数据库中的数据的标准数据库编程语言。

  • SQL 指结构化查询语言,全称是 Structured Query Language(是最初由IBM开发)。
  • SQL 是关系数据库系统的标准语言。
  • SQL 是一种 ANSI(American National Standards Institute 美国国家标准化组织)标准的计算机语言。

历史

  • 由IBM的Donald D. Chamberlin和Raymond F. Boyce 于1970年开发
  • 首先,开发版本被称为SEQUEL(结构化英语查询语言)
  • 关系软件于1979年发布了第一个叫做System / R的商业产品。
  • 由于商标冲突问题,SEQUEL首字母缩略词后来更改为SQL。
  • 后来IBM基于System / R的原型开始在SQL上开发商业产品。

SQL是一种标准, 但是

虽然SQL 是一门ANSI (American National Standards Institute 美国国家标准化组织) 标准的计算机语言,但是仍然存在着多种不同版本的 SQL 语言。

然而,为了与 ANSI 标准相兼容,它们必须以相似的方式共同地来支持一些主要的命令(比如 SELECT、UPDATE、DELETE、INSERT、WHERE 等等)。

语法

SQL 语法规则

  • 总以关键字开始
  • 以分号结尾
  • 不区分大小写,意味着update 和UPDATE相同

数据库表

数据库通常包含一个或多个表。每个表都用一个名称标识

选择

select column1, column2 from table_name

从Customers 表中选取 CustomerName 和City

select CustomerName, City from Customers;

选取所有列

select * from Customers;

选择不同

distinct

从Customers 表中的Country 列中选择distinct值

select distinct Country from Customers;

where子句

where 子句用于提取满足指定标准的记录

从Customers 表中选择其国家为Mexico 的所有客户

select * from Customers where Country="Mexico";

文本字段和数值字段

文本值周围使用单引号(大多数数据库系统也接收双引号)

数值字段不需要使用引号

select * from Customers where CustomerID=1;

逻辑关系

where 子句可以和and,or not 运算符结合使用

从 Customers 表中选择其国家为 Germany、 城市为 Berlin 所有客户

select * from Customers where Country='Germany' and City='Berlin';

选择城市为Berlin 或Munchen的Customers 所有字段

select * from Customers where City='Berlin' or City='Munchen';

选择国家不是Germany 的customers 所有字段

select * from Customers where not Country='Germany';

结合and & or

从国家 Germany 且 城市为Berlin 或Munchen 的Customers 表中选择所有客户

select * from Customers where Country='Germany' and (City='Berlin' or City='Munchen');

选择国家不是Germany 且不是USA 的所有字段

select * from Customers where not Country='Germany' and not Country='USA';

order by

升序降序排序

从Customers表中选取所有客户,并按照Country 排序

select * from Customers order by Country;

从Customers表中选取素有客户,并降序排序

select * from Customers order by Country desc;

多实例

从customers 中选取所有客户,按country 升序排列,按CustomerName 降序排列

select * from Customers order by Country asc, CustomerName desc;

insert into

可以以两种形式编写

表单没有指定要插入数据的列的名称,只提供要插入的值

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

要为所有列添加值,可以不需要为sql 查询指定列名称,但是,确保值的顺序与表中列顺序相同

INSERT INTO table_name
VALUES (value1, value2, value3, ...);

在指定的列中插入数据

insert into Customers (CustomerName, City, Country) values ('Cardianl', 'Stavanger','Norway'); 

Null

null 表示缺失的值,null表示字段为空

null 和空白和0是不同的

is null

所有没有住址的人员

select LastName,FirstNamem, Addredd from Persons Where Address is null;

is not null

select LastName, FirstName, Address from Persons where Address is not null;

update

更新表中已经存在的记录

将mexico 的所有记录的联系人性名更新为juan

update Customers set ContactName='juan' where Country='mexico';

警告

如果省略where 子句, 所有记录都会更新

delete

delete 语句用于删除表中现有记录

删除客户‘Alfreds FutterKiste’

delete from Customers where CustomerName='Alfreds Futterkiste';

删除所有数据

删除表中的所有行,不需要删除该表,表结构、属性和索引不变

delete from table_name;
delete * from table_name;

猜你喜欢

转载自blog.csdn.net/LEIQINGQI/article/details/81534796
今日推荐