SQL基础(增删改查基本功能)

来源:互联网 发布:软件版本 alpha beta 编辑:程序博客网 时间:2024/05/21 06:46
SQL基础操作
1.select语句
SELECT语句:用于从数据库中选取数据

select * from table1;select name,id from table2;

(1)从 "customers" 表中选取 "name" 和 "id" 列:
select name,id from customers;
(2)从 "Customers" 表中选取所有列:
select * from customers;

 

select distinct语句:用于返回唯一不同的值。

(一个列可能包含多个重复值)
例:从 "customers" 表的 "name" 列中选取唯一不同的值
select distinct name from customers;

 (1)WHERE子句:用于过滤记录
例:从 "menber" 表中选取国家(country)为 "China" 的所有内容:
select * from menber where country='China';
(数值字段不用引号)
可在where子句中使用的运算符:
=,<>/!=,>,<,>=,<=,between(在某个范围内),like(搜索某种模式),in(指定针对某个列的多个可能值)

 (2)and&or
例:从 "Peron" 表中选取 Country 为 "China" 且 City 为 "Jilin" 或 "Beijing" 的所有内容:
select * from Person where Country="China" and (City="Jilin" or City="Beijing");

 

(3)ORDER BY:用于对结果集按照一个列或者多个列进行排序

默认升序,降序则用desc关键字
例:从 "person1" 表中选取所有内容,并按照 "age" 列降序排序:
select * from person1 order by age desc;

 2.INSERT INTO:语句用于向表中插入新记录
两种语法:
insert into table1 values (value1,value2,...);insert into table2 (col1,col2,...) values (value1,value2);
例:插入一个新行,但是只在 "name" 和 "country" 列插入数据:
insert into person (name,country) values ('Jack','England');

 3.UPDATE语句:用于更新表中的记录
例:把 "person" 表的 name 为 'Jason' 的 country 改为 'China', city 改为 'Shanghai':
update person set country='China',city='Shanghai' where name='Jason';

 4.DELETE语句:用于删除表中的记录
例:从"person"表中删除name为"Jane"且address为"London"的数据:
delete from person where name='Jane' and address='London';