对数据库的操作
查看所有的数据库 | show datebases |
创建数据库 | create database |
使用哪一个数据库 | use database |
删除数据库 | drop database |
MySQL重要命令
- select 从数据库中提取数据
- update 更新数据库中的数据
- delete 删除数据库中的数据
- insert into 增加数据库中的数据
- create database 创建数据库
- alter database 修改数据库
- create table 创建一个数据表
- alter table 修改表
- drop table 删除表
- drop database 删除库
- show databases 展示所有的数据库
- create index 创建索引 搜索键
- drop index 删除索引
select 选择语句
语法 * 代表表1中的所有字段
select * from 表1
练习
- 从Customers 表中选择CustomerName 和 City字段
-
select CustomerName,City from Customers;
- 选择Customers 表中所有字段
-
select * from Customers;
- 从Customers 表中的Country列中选择所有不同的值
-
select distinct Country from Customers;
- select distinct 选择不同语句 在表中通常一列有好多重复值 这时候只想看到 不同的值 就可以用这个语句
where 查询定位 子句
where 用来过滤记录 ,它用于仅提取满足条件的记录
语法
select c1,c2,...
from table_name
where 条件
例子
查询Customers 表中的客户来自中国的用户 国家字段用Country来表示
select * from Customers where Country='中国';
在sql语句中 文本值周围尽量使用单引号 但是数组字段不应该在单引号中
select * from Customers where id=1;
可以在where条件中使用运算符
sql中的运算符
在sql语言中常用的运算符有以下几种:
1.算术运算符
+(加)、-(减)、*(乘)、/(除)、%(取模,也就是商)。
2.赋值运算符
“=”(等号)表示赋值,例如:Name=‘王华’
3.比较运算符
4.逻辑运算符
逻辑运算符用来对某个条件进行判断,以获得判断的真假,返回带有true或false值得布尔数据类型
练习
- 选取所有产品价格为18的产品
-
select * from Products where Price=18;
- 选取所有价格大于30的产品
-
select * from Products where Price >30;
- 选取所有产品小于30的产品
-
select * from Products where Price <30;
- 选取所有产品价格的价格不等于18
-
select * from 表 where 价格<>18;
- 选取所有产品在50到60元之间的产品
-
select * from 表 where 价格 between 50 and 60;
- 选择数据库中所有城市 以s开头的数据
-
select * from 表 where city like 's%';
- 从表中查找所有在河北 和北京的用户
-
select * from 表 where city in ('河北','北京');
- 使用not关键字 查询所有不在河北的用户
-
select * from 表 where not city='河北';
and、or 和not运算符
where语句可以结合and or和not语句操作
- and操作同一条数据 ,如果满足条件and为真
- or操作同一条数据 ,如果有任意一个条件满足 就为真
- not操作数据 ,如果条件是不正确的记录
and语法
select * from 表
where [条件1] and [条件2] and ...;
or语法
select * from 表
where [条件1] or [条件2] or....;
not语法
select * from 表
where not [条件];
练习
- 选择表中国家是中国的 切 城市是河北或者北京的
-
select * from 表 where 国家 ='中国'and (城市='河北'or 城市='北京');
- 选择表中国家不是美国的也不是英国的
-
select * from 表 where not 国家='美国' and not 国家='英国';
-
插入语句 inset into
语法
指定要插入的列名和值
inset into 表
(K1,K2,K3)
values
(V1,V2,V3);
若是要将表中所有列添加值就不需要写列名
inset into 表
values
(v1,v2,v3..);
null空值
列出address字段中具有null的所有客户
select * from 表
where address is null;
列出address字段中具有值的所有客户
select * from 表
where address is not null;
update更新语句
语法
update 表
set k1=v1,k2=v2..
where 限制条件
更新id=1的用户名和城市数据
update 表
set name="..",city='..'
where id=1;
要注意:更新表中数据要小心 要在where条件中指定表明要更新那些记录 \
如果省略where语句 则表中所有的数据将要全部更新
试一试连接我的服务器端的mysql
sql中的数据类型
去除重复记录 关键字 distinct
-- 去除重复记录
SELECT DISTINCT name FROM account;
模糊查询 like
排序
-- 降序查询 desc
-- 升序是asc 默认是升序
select * from account ORDER BY money DESC;
如果钱一样就按照id升序排行
select * from account ORDER BY money DESC,id asc;
SELECT * FROM `account`
-- 查询马性别的人
select * from account WHERE name like '马%';
-- 查询包括路字的人
select * from account WHERE name like '%路%';
-- 降序查询 desc
-- 升序是asc 默认是升序
select * from account ORDER BY money DESC;
select * from account ORDER BY money DESC,id asc
;
-- 查询一共有多少学生 count 里面的字段名是不能为空的
SELECT COUNT(id) FROM account;
分页查询
外键约束