59. mysql入门语句
登录MySQL
mysql -h 主机地址 -u 用户名 -p 数据库
然后根据提示输入密码即可。数据库 也可先不填,留空。
出现如图所示,即为登录成功。
浏览数据库
show databases;
建库
create database 库名;
删库
drop database 库名;
选库
use 库名;
查看当前数据库下的数据表(刚创建的数据库还没有表)
show tables;
建表
create table 表名(
字段名 类型
);
声明当前的字符编码,(可解决乱码问题)
set names gbk/utf8/…;
添加数据
insert into 表名(字段名1, 字段名2) value (数据值1, 数据值2);
查询
select * from 表名;
修改
update 表名 set 字段=新的数据值 where 条件;
删除
delete from 表名 where 条件;
清空表数据
truncate 表名;
表改名
rename table 表名 to 新的表名;
表增加列
alter table 表名 add 字段名 类型;
表修改列
alter table 表名 modify 字段名 类型;
删除表
drop table 表名;
60. 留言存入MySQL
mysql数据表设计:
create table liuyan(
id int auto_increment primary key,
title varchar(50),
content varchar(200),
pubtime int
)charset utf8;
liuyan.html:
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
</head>
<body>
<a href="list.php">查看留言</a>
<form action="add.php" method="post">
标题:<font color=red>*</font><br><input type="text" name="title" /><br>
内容:<font color=red>*</font><br><textarea name="content" cols="30" rows="5"></textarea><br>
<input type="submit" value="提交">
</form>
</body>
</html>
add.php:
<?php
header('Content-Type: text/html;charset=utf-8');
error_reporting(E_ALL ^ E_DEPRECATED);
// 先登录MySQL
$conn = mysql_connect('localhost', 'root', 'root');
// 选库
mysql_query('use test', $conn);
// 设置字符编码
mysql_query('set names utf8', $conn);
if(empty($_POST['title']) || empty($_POST['content'])){
echo '表单不能有空项';
}else{
$sql = 'insert into liuyan (title, content, pubtime) value ("'. $_POST['title'] .'", "'. $_POST['content'] .'", '. time() .')';
$rs = mysql_query($sql, $conn);
if($rs){
echo '留言成功';
}else{
echo '留言失败';
}
}
?>
<br>
<a href="liuyan.html">返回</a>
list.php:
<?php
header('Content-Type: text/html;charset=utf-8');
error_reporting(E_ALL ^ E_DEPRECATED);
// 先登录MySQL
$conn = mysql_connect('localhost', 'root', 'root');
// 选库
mysql_query('use test', $conn);
// 设置字符编码
mysql_query('set names utf8', $conn);
// 查询数据
$sql = 'select * from liuyan';
$rs = mysql_query($sql, $conn);
echo '<ul>';
while($row = mysql_fetch_assoc($rs)){
echo '<li><a href="detail.php?id='. $row['id'] .'">'. $row['title'] .'</a></li>';
}
echo '</ul>';
?>
<br>
<a href="liuyan.html">返回</a>
detail.php:
<?php
header('Content-Type: text/html;charset=utf-8');
error_reporting(E_ALL ^ E_DEPRECATED);
// 先登录MySQL
$conn = mysql_connect('localhost', 'root', 'root');
// 选库
mysql_query('use test', $conn);
// 设置字符编码
mysql_query('set names utf8', $conn);
// 查询数据
$sql = 'select * from liuyan where id='. $_GET['id'];
$rs = mysql_query($sql, $conn);
$row = mysql_fetch_assoc($rs);
echo '<h1>'. $row['title'] .'</h1>';
echo '<p>'. $row['content'] .'</p>';
?>
<br>
<a href="list.php">返回</a>
62. SQL语句——insert、update、delete、select
表结构如下:
create table class(
id int primary key auto_increment,
sname varchar(10) not null default '',
gender char(1) not null default '男',
company varchar(20) not null default '',
salary decimal(6,2) not null default 0.00,
lunch smallint not null default 0
) engine myisam charset utf8;
查看库下的所有表
show tables;
查看表的结构
desc class;
增加记录
insert into 表名 (字段名) value (数据值)
思路:往哪张表添加? 往这张表的什么字段添?添什么值?
例:
① 字段和值对应,可以写部分字段
insert into class (id, sname, gender, company, salary, lunch) value (1, ‘张三’, ‘男’, ‘百度’, 7000, 500);
② 字段不写,默认全部,按顺序添数据
insert into class value (2, ‘李四’, ‘女’, ‘阿里巴巴’, 8000, 400);
③ 一次添加多条数据
insert into class (sname, company, salary) values (‘王五’, ‘sohu’, 6500), (‘马六’, ‘google’, 7000), (‘赵七’, ‘腾讯’, 5500);
修改记录
update 表名 set 字段1=数据1,字段2=数据2 where 条件
思路:修改哪张表? 修改什么? 修改哪些数据?
例:update class set lunch = 600 where gender=‘男’;
注意:如果没有加条件,将修改全部数据。开发过程中一定要仔细检查条件,否则产生严重后果。
删除记录
delete from 表名 where 条件
思路:从哪张表删除? 删除哪些数据?
例:delete from class where salary>=8000;
注意:开发中,很少会用到物理删除,大多数情况是通过修改某个字段来标识删除。
物理删除时,如果没有加条件,将删除全部数据。开发过程中一定要仔细检查条件,否则产生严重后果。
查询记录
select 字段名 from 表名 where 条件
思路:查询哪些字段? 在哪张表? 哪些数据?
例:select sname,company,salary from class where salary>=7000;
63. MySQL常用关键字
条件相关:
- and
- or
- between…and
- in
- not
- like
- is null
- having
其它:
- select
- update
- insert
- delete
- drop
- alter
- create
- from
- where
- as
- join
- union
- group by
- order by
- limit
内置函数:
- max() 求最大值
- min() 求最小值
- count() 某分组/集合的总数
- sum() 求总和
- avg() 求平均值
- floor() 向下取整
- ceil() 向上取整
- left() 截取字符串
- concat() 字符串拼接
- from_unixtime() 时间戳转日期格式
- unix_timestamp() 日期格式转时间戳
- now() 当前日期
- date_format() 格式化日期
64. MySQL案例
CREATE TABLE `customer` (
`cu_id` int(11) NOT NULL,
`cu_name` varchar(50) NOT NULL,
`cu_age` int(4) NOT NULL default 0,
`cu_phone` varchar(50) NOT NULL,
`cu_gender` int(11) NOT NULL DEFAULT '1',
`cu_address` varchar(100) NOT NULL,
`cu_used` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`cu_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `customer` (cu_age,cu_id,cu_name,cu_phone,cu_gender,cu_address,cu_used) VALUES (floor(rand()*30+20),1,'宋江','18275657465',0,'广东省韶关市',1),(floor(rand()*30+20),2,'卢俊义','18271105634',0,'其它省其它市',1),(floor(rand()*30+20),3,'吴用','18271975605',1,'其它省其它市',1),(floor(rand()*30+20),4,'公孙胜','18271067886',1,'广东省惠州市',1),(floor(rand()*30+20),5,'关胜','18276207339',0,'四川省攀枝花市',1),(floor(rand()*30+20),6,'林冲','18278792087',0,'台湾省台北市',1),(floor(rand()*30+20),7,'秦明','18278494952',1,'广西省贵港市',1),(floor(rand()*30+20),8,'呼延灼','18276763041',1,'四川省德阳市',1),(floor(rand()*30+20),9,'花荣','18272521371',1,'其它省其它市',1),(floor(rand()*30+20),10,'柴进','18272367183',0,'广东省韶关市',1),(floor(rand()*30+20),11,'李应','18275204860',0,'上海省虹口市',1),(floor(rand()*30+20),12,'朱仝','18276339179',1,'台湾省台中市',1),(floor(rand()*30+20),13,'鲁智深','18276264583',1,'其它省其它市',1),(floor(rand()*30+20),14,'武松','18276949667',0,'四川省成都市',1),(floor(rand()*30+20),15,'董平','18272456490',1,'广西省钦州市',1),(floor(rand()*30+20),16,'张清','18275846414',0,'其它省其它市',1),(floor(rand()*30+20),17,'杨志','18271711447',0,'辽宁省丹东市',1),(floor(rand()*30+20),18,'徐宁','18272028860',1,'上海省闵行市',1),(floor(rand()*30+20),19,'索超','18271369521',1,'四川省成都市',1),(floor(rand()*30+20),20,'戴宗','18275204314',0,'其它省其它市',1),(floor(rand()*30+20),21,'刘唐','18274764347',1,'其它省其它市',1),(floor(rand()*30+20),22,'李逵','18274981330',1,'广东省梅州市',1),(floor(rand()*30+20),23,'史进','18276444559',0,'其它省其它市',1),(floor(rand()*30+20),24,'穆弘','18271931763',1,'辽宁省本溪市',1),(floor(rand()*30+20),25,'雷横','18279901069',1,'其它省其它市',1),(floor(rand()*30+20),26,'李俊','18275475478',1,'辽宁省朝阳市',1),(floor(rand()*30+20),27,'阮小二','18278633687',1,'广西省防城港市',1),(floor(rand()*30+20),28,'张横','18279633127',1,'辽宁省鞍山市',1),(floor(rand()*30+20),29,'阮小五','18272529785',1,'其它省其它市',1),(floor(rand()*30+20),30,'张顺','18275261060',0,'其它省其它市',1),(floor(rand()*30+20),31,'阮小七','18277028342',0,'四川省自贡市',1),(floor(rand()*30+20),32,'杨雄','18275622345',0,'内蒙古省阿拉善盟市',1),(floor(rand()*30+20),33,'石秀','18276801223',0,'北京省崇文市',1),(floor(rand()*30+20),34,'解珍','18273848403',1,'内蒙古省哲里木盟市',1),(floor(rand()*30+20),35,'解宝','18276402051',0,'台湾省南投市',1),(floor(rand()*30+20),36,'燕青','18279225530',0,'广西省玉林市',1),(floor(rand()*30+20),37,'朱武','18277325267',0,'内蒙古省伊克昭盟市',1),(floor(rand()*30+20),38,'黄信','18278647007',0,'广西省贺州市',1),(floor(rand()*30+20),39,'孙立','18272008167',1,'广西省梧州市',1),(floor(rand()*30+20),40,'宣赞','18277274872',1,'广西省防城港市',1),(floor(rand()*30+20),41,'郝思文','18275189574',0,'北京省宣武市',1),(floor(rand()*30+20),42,'韩滔','18273555099',0,'广东省中山市',1),(floor(rand()*30+20),43,'彭玘','18274827470',0,'辽宁省辽阳市',1),(floor(rand()*30+20),44,'单廷珪','18275965960',0,'辽宁省沈阳市',1),(floor(rand()*30+20),45,'魏定国','18277847723',1,'天津省汉沽市',1),(floor(rand()*30+20),46,'萧让','18279543680',0,'上海省黄浦市',1),(floor(rand()*30+20),47,'裴宣','18279516496',0,'广西省玉林市',1),(floor(rand()*30+20),48,'欧鹏','18278672217',1,'内蒙古省乌兰察布盟市',1),(floor(rand()*30+20),49,'邓飞','18279057510',1,'辽宁省盘锦市',1),(floor(rand()*30+20),50,'燕顺','18275544972',1,'天津省蓟县市',1),(floor(rand()*30+20),51,'杨林','18272523469',1,'其它省其它市',1),(floor(rand()*30+20),52,'凌振','18274734763',1,'广东省江门市',1),(floor(rand()*30+20),53,'蒋敬','18278198755',1,'广东省肇庆市',1),(floor(rand()*30+20),54,'吕方','18277515729',1,'其它省其它市',1),(floor(rand()*30+20),55,'郭盛','18273033069',0,'天津省汉沽市',1),(floor(rand()*30+20),56,'安道全','18275966295',0,'上海省黄浦市',1),(floor(rand()*30+20),57,'皇甫端','18279872059',1,'台湾省彰化市',1),(floor(rand()*30+20),58,'王英','18278223898',0,'内蒙古省呼和浩特市',1),(floor(rand()*30+20),59,'扈三娘','18272073004',1,'四川省宜宾市',1),(floor(rand()*30+20),60,'鲍旭','18277763682',0,'广西省柳州市',1),(floor(rand()*30+20),61,'樊瑞','18275868276',0,'其它省其它市',1),(floor(rand()*30+20),62,'孔明','18274014797',1,'其它省其它市',1),(floor(rand()*30+20),63,'孔亮','18279406372',1,'天津省北辰市',1),(floor(rand()*30+20),64,'项充','18274294983',1,'天津省西青市',1),(floor(rand()*30+20),65,'李衮','18274138339',0,'广西省玉林市',1),(floor(rand()*30+20),66,'金大坚','18275451658',1,'上海省奉贤市',1),(floor(rand()*30+20),67,'马麟','18277057778',1,'台湾省金门市',1),(floor(rand()*30+20),68,'童威','18275948243',0,'天津省红挢市',1),(floor(rand()*30+20),69,'童猛','18274825702',1,'天津省宝坻市',1),(floor(rand()*30+20),70,'孟康','18271113974',0,'广西省钦州市',1),(floor(rand()*30+20),71,'猿侯健','18271771290',1,'内蒙古省巴彦淖尔盟市',1),(floor(rand()*30+20),72,'陈达','18275933472',0,'上海省静安市',1),(floor(rand()*30+20),73,'杨春','18276581237',1,'四川省乐山市',1),(floor(rand()*30+20),74,'郑天寿','18277232285',0,'四川省雅安市',1),(floor(rand()*30+20),75,'陶宗旺','18275771248',1,'广东省清远市',1),(floor(rand()*30+20),76,'宋清','18275757233',1,'辽宁省葫芦岛市',1),(floor(rand()*30+20),77,'乐和','18275561782',1,'台湾省苗栗市',1),(floor(rand()*30+20),78,'龚旺','18278291568',1,'其它省其它市',1),(floor(rand()*30+20),79,'丁得孙','18279285769',0,'广东省广州市',1),(floor(rand()*30+20),80,'穆春','18277472579',0,'内蒙古省哲里木盟市',1),(floor(rand()*30+20),81,'曹正','18273437929',1,'四川省广安市',1),(floor(rand()*30+20),82,'宋万','18274818974',0,'辽宁省阜新市',1),(floor(rand()*30+20),83,'杜迁','18279185024',0,'广西省钦州市',1),(floor(rand()*30+20),84,'薛永','18272025904',0,'台湾省云林市',1),(floor(rand()*30+20),85,'施恩','18275268048',1,'北京省通州市',1),(floor(rand()*30+20),86,'李忠','18275408023',0,'四川省成都市',1),(floor(rand()*30+20),87,'周通','18274682149',0,'四川省乐山市',1),(floor(rand()*30+20),88,'汤隆','18275683239',1,'广西省柳州地区市',1),(floor(rand()*30+20),89,'杜兴','18271561384',1,'天津省河北市',1),(floor(rand()*30+20),90,'邹渊','18276381328',1,'天津省塘沽市',1),(floor(rand()*30+20),91,'邹润','18279795765',1,'内蒙古省呼和浩特市',1),(floor(rand()*30+20),92,'朱贵','18276823938',1,'广东省湛江市',1),(floor(rand()*30+20),93,'朱富','18279873678',0,'广东省阳江市',1),(floor(rand()*30+20),94,'蔡福','18275407317',0,'上海省虹口市',1),(floor(rand()*30+20),95,'蔡庆','18272297037',0,'天津省津南市',1),(floor(rand()*30+20),96,'李立','18277242996',1,'台湾省澎湖市',1),(floor(rand()*30+20),97,'李云','18273898239',1,'台湾省澎湖市',1),(floor(rand()*30+20),98,'焦挺','18278404744',1,'上海省松江市',1),(floor(rand()*30+20),99,'石勇','18271293209',0,'辽宁省鞍山市',1),(floor(rand()*30+20),100,'孙新','18279932467',0,'上海省卢湾市',1),(floor(rand()*30+20),101,'顾大嫂','18272938510',1,'广东省潮州市',1),(floor(rand()*30+20),102,'张青','18277838993',1,'广西省北海市',1),(floor(rand()*30+20),103,'孙二娘','18278646573',0,'天津省宁河市',1),(floor(rand()*30+20),104,'王定六','18275331262',0,'广西省贵港市',1),(floor(rand()*30+20),105,'郁保四','18273605180',0,'内蒙古省伊克昭盟市',1),(floor(rand()*30+20),106,'白胜','18278067272',0,'辽宁省朝阳市',1),(floor(rand()*30+20),107,'时迁','18276534232',1,'天津省宁河市',1),(floor(rand()*30+20),108,'段景住','18271147434',1,'天津省河西市',1);
问题:
-
把所有人的年龄修改成十的整数倍,例:21改成20,29也改成20,31改成30…。
-
找出所有人中,姓’宋’的人的姓名和性别,性别用’男’、'女’表示
-
显示前十个人的姓名和年龄,显示的名字前面加上’梁山’
-
给数据表加上一个列cu_birthday,表示出生年份,根据年龄来计算
-
计算梁山好汉的平均年龄
-
计算梁山好汉中,每个姓氏中年龄最大的人的姓名
-
获取每个姓氏中年龄有超过39岁的人的姓
答案: -
update customer set cu_age=floor(cu_age/10)*10;
-
select cu_name,if(cu_gender,‘男’,‘女’) as ‘性别’ from customer where cu_name like ‘宋%’;
-
select concat(‘梁山’,cu_name),cu_age from customer limit 10;
-
alter table customer add cu_birthday varchar(10) NOT NULL default ‘’;
update customer set cu_birthday=left(now(),4)-cu_age; -
select floor(avg(cu_age)) from customer;
-
select cu_name,max(cu_age) from customer group by left(cu_name,1);
-
select left(cu_name,1),max(cu_age) from customer group by left(cu_name,1) having max(cu_age)>39;
65. 排序
select 字段名 from 表名 order by 字段名 desc/asc
desc,降序
asc,升序
多列排序用‘,’隔开
例:select cu_name,cu_age from customer order by cu_age desc;
66. 截取
select 字段名 from 表名 limit M,N
M,表示从第M个开始
N,表示一共截取N个
例:
获取customer表中,从第3条开始的8条数据:
select cu_name from customer limit 3,8;
如果limit后面只有一个参数N,将从0开始,一共获取N条:
select cu_name from customer limit 10;
67. 内联链接
select 字段名 from 表1 inner join 表2 on 表1.字段 = 表2.字段
表1 中存在一个字段和 表2 中的某一个字段数据值一样,二者通过该值连接在一起。
例:select student.id,student.name,score.score from student inner join score on score.studentid = student.id;
68. union查询
select 字段 from 表1 union select 字段 from 表2
select 字段 from 表1 union all select 字段 from 表2
前后结果集中的字段数量必须一致,否则将报错。
使用 union 时,完全相等的行,将会被合并,而合并是比较耗时的操作,
一般不让 union 进行合并,使用 union all 可以避免合并。
union 子句中,不用写 order by。
经合并后得到的总的结果集,可以 order by,子句的 order by 没有意义
例:(select * from a) union all (select * from b) order by id desc;
68. 整型列的属性
unsigned 无符号,列的值从0开始,不为负。
zerofill 使用于学号,编号等固定宽度的数字,可以用0填充至固定宽度。默认决定列为unsigned。
69. 浮点列和定点列
floot(M, D) 单精度浮点型
double(M, D) 双精度浮点型
decimal(M, D) 定点型,更精确,无精度损失
M 是精度总位数,D 是标度,小数点后的位数
70. 字符列
char、varchar、text、enum
char 定长字符串,不管内容大小,所占用的空间固定
varchar 变长字符串,占用空间跟内容有关,一般多一个字符用于记录所占空间大小
text 文本类型,主要用于保存大段文本
enum 枚举,是定义好的,值就在某几个范围内。
71. 列的默认值
default,如果没有特意声明或修改,数据列将以默认值保存在数据库。
defaualt 常配合 not null 使用。null 查询不便且索引效果不高,所以实用中,避免列的值为null。
72. 主键与自增
primary key 主键,表示该列不重复,该行的唯一标识,能区分每一行
auto_increment 自增,一般放在主键上。在添加时,不用指定值,系统根据已有的最大值加1。
73. 列的增删改
增:
// 将在表的最后
alter table 表名 add 列名 列类型 列属性
// 将再指定列后面
alter table 表名 add 列名 列类型 列属性 after 列名
删:
alter table 表名 drop column 列名
改:
// 修改列名、列的属性
alter table 表名 change 列名 新列名 新属性
// 修改列的属性
alter table 表名 modify 列名 新属性
74. 视图
view 又称为虚拟表,是 SQL 的查询结果。如果基表发生了改变,视图也会相应改变。
例:create view view_user as select id,name from user;
视图:
- 权限控制时可以用
- 简化复杂的查询
- 如果视图的每一行和物理表是一一对应的,则视图的数据会自动更新、删除、添加。如果视图的行是由物理表经过计算得来的结果,视图不可以更新
- 视图和数据表一样,增删改命令不变
75. 索引
索引是数据表的目录,能快速定位行数据的位置。
索引提高了查询速度,降低了增删改的速度。
一般在查询频繁的列上加,而且在重复度低的列上加效果更好。
索引类型:
命令 | 名称 |
---|---|
key | 普通索引 |
unique key | 唯一索引 |
primary key | 主键索引 |
fulltext | 全文索引 |
索引长度:建索引时,可以只索引列的前一部分内容,此时索引的长度为指定的长度
如:前10个字符 key(email(10))
多列索引:把2列或多列的值,看成是一个整体,然后建索引
冗余索引:在某个列中,可能存在多个索引
76. 索引操作
查看
show index from 表名
或 show create table 表名
删除
alter table 表名 drop index 索引名
或 drop index 索引名 on 表名
增加
alter table 表名 add [index / unique] 索引名(列名)
77. 事务
事务,一系列的数据库操作,是数据库应用的基本逻辑单位。
简单来讲,事务就是被绑定在一起作为一个逻辑工作单元的sql语句组,如果任何一个语句操作失败那个整个操作就被失败,之前的所有操作回滚到操作前的状态。为了确保要么全部执行,要么不全部执行,就可以使用事务。
start transaction 启动事务
commit 确认提交
rollback 回滚撤销