(1) 命令语法
insert into <表名>[(<字段名1>[..<字段名n>])]values(值1)[,(值n)]
(2) 建立一个简单的测试表test
CREATE TABLE `test` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, PRIMARY KEY (`id`)) ;mysql> desc test;+-------+----------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+----------------+| id | int(4) | NO | PRI | NULL | auto_increment || name | char(20) | NO | | NULL| |+-------+----------+------+-----+---------+----------------+
插入值
insert into test(id,name) values(1,'oldboy');insert into test(id,name) values(1,'oldboy');
insert | into | test(id,name) | values(1,‘oldboy’) |
固定不能 | 固定不变 | 表明,指定列 | 插入值1,字符串’’ |
查询
mysql> select * from test;+----+--------+| id | name |+----+--------+| 1 | oldboy |+----+--------+1 row in set (0.00 sec)
第二种方法:
id列可以不指定,不指定就自己增长
insert into test(name) values('oldgirl'); mysql> select * from test;+----+---------+| id | name|+----+---------+| 1 | oldboy || 2 | oldgirl |+----+---------+2 rows in set (0.00 sec)
第三种方法,可以不指定列,后面按照循序插入
mysql> insert into test values(3,'inca');Query OK, 1 row affected (0.00 sec) mysql> select * from test;+----+---------+| id | name|+----+---------+| 1 | oldboy || 2 | oldgirl || 3 | inca|+----+---------+3 rows in set (0.00 sec)
批量插入:
mysql> insert into test values(4,'zuma'),(5,'kaka');Query OK, 2 rows affected (0.00 sec)Records: 2Duplicates: 0 Warnings: 0 mysql> select * from test;+----+---------+| id | name|+----+---------+| 1 | oldboy || 2 | oldgirl || 3 | inca || 4 | zuma|| 5 | kaka|+----+---------+5 rows in set (0.00 sec)
清空所有值
mysql> truncate table test;Query OK, 0 rows affected (0.00 sec) mysql> select * from test;Empty set (0.00 sec)
一条命令解决以上所有配置
mysql> insert into test values (1,'oldboy'),(2,'oldgirl'),(3,'inca'),(4,'zuma'),(5,'kaka');Query OK, 5 rows affected (0.01 sec)Records: 5Duplicates: 0 Warnings: 0 mysql> select * from test;+----+---------+| id | name|+----+---------+| 1 | oldboy || 2 | oldgirl || 3 | inca || 4 | zuma || 5 | kaka |+----+---------+5 rows in set (0.00 sec)
查询数据
查询表的所有数据行
1.命令句法:select<字段1,字段2,…>frin
<表名>where<表达式>
其中,select,from,where是不能随便改的,是关键字,支持大小写
2.列:查看表test中所有数据
a.进入指定库后查询
如果不进入库可以使用
mysql> select * from oldboy.test;mysql> select user,host,password from mysql.user;
+-----------+---------------------------+-------------------------------------------+| user| host | password|+-----------+---------------------------+-------------------------------------------+| root| localhost | *7495041D24E489A0096DCFA036B166446FDDD992 || root| 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 || wordpress | 192.168.1.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 || cyh| 10.1.1.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 || bbs| 172.16.1.1/255.255.255.0 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 || blog| localhost | *7495041D24E489A0096DCFA036B166446FDDD992 || oldboy| localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 || oldboy| 192.168.1.%/255.255.255.0 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 || abc| localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |+-----------+---------------------------+-------------------------------------------+
只查询前2行内容
mysql> select * from test limit 2;+----+---------+| id | name |+----+---------+| 1 | oldboy || 2 | oldgirl |+----+---------+2 rows in set (0.00 sec)
从第二条开始查,查找2个
mysql> select * from test limit 1,2;+----+---------+| id | name|+----+---------+| 2 | oldgirl || 3 | inca |+----+---------+2 rows in set (0.00 sec)
按照条件查询
mysql> select * from test where id=1;+----+--------+| id | name|+----+--------+| 1 | oldboy |+----+--------+1 row in set (0.00 sec)
提示:mysql> select * from test where name=’oldboy’;
<==查询字符串要加引号
mysql> select * from test where name='oldboy';+----+--------+| id | name |+----+--------+| 1 | oldboy |+----+--------+1 row in set (0.00 sec)
提示:查找字符串类型的条件的值要带单引号,数字值不带引号。
查询多个条件
mysql> select * from test where name='oldgirl' and id=2;+----+---------+| id | name |+----+---------+| 2 | oldgirl |+----+---------+1 row in set (0.00 sec)
范围查询
mysql> select * from test where id>2;+----+------+| id | name |+----+------+| 3 | inca || 4 | zuma || 5 | kaka |+----+------+3 rows in set (0.00 sec)mysql> select * from test where id>2 and id<5;+----+------+| id | name |+----+------+| 3 | inca || 4 | zuma |+----+------+2 rows in set (0.35 sec)
或者的意思
mysql> select * from test where id>2 or id<5;+----+---------+| id | name |+----+---------+| 1 | oldboy || 2 | oldgirl || 3 | inca || 4 | zuma || 5 | kaka |+----+---------+5 rows in set (0.34 sec)
排序
什么都不加相当于升序
mysql> select * from test;相当于mysql> select * from test order by id asc; 写法倒序mysql> select * from test order by id desc; 倒序
例子:创建学生表
drop table student;create table student(Sno int(10) NOT NULL COMMENT '学号',Sname varchar(16) NOT NULL COMMENT '姓名',Ssex char(2) NOT NULL COMMENT '性别',Sage tinyint(2) NOT NULL default '0' COMMENT '学生年龄',Sdept varchar(16) default NULL COMMENT '学生所在系别', PRIMARY KEY (Sno) ,key index_Sname (Sname)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
插入内容
create table course(Cno int(10) NOT NULL COMMENT '课程号',Cname varchar(64) NOT NULL COMMENT '课程名',Ccredit tinyint(2) NOT NULL COMMENT '学分',PRIMARY KEY (Cno) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
插入内容
CREATE TABLE `SC` ( SCid int(12) NOT NULL auto_increment COMMENT '主键', `Cno` int(10) NOT NULL COMMENT '课程号', `Sno` int(10) NOT NULL COMMENT '学号', `Grade` tinyint(2) NOT NULL COMMENT '学生成绩', PRIMARY KEY (`SCid`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;
插入内容
INSERT INTO course values(1001,'Linux中高级运维',3);INSERT INTO course values(1002,'Linux高级架构师',5);INSERT INTO course values(1003,'MySQL高级Dba',4);INSERT INTO course values(1004,'Python运维开发',4);INSERT INTO course values(1005,'Java web开发',3);
插入内容
INSERT INTO SC(Sno,Cno,Grade) values(0001,1001,4);INSERT INTO SC(Sno,Cno,Grade) values(0001,1002,3);INSERT INTO SC(Sno,Cno,Grade) values(0001,1003,1);INSERT INTO SC(Sno,Cno,Grade) values(0001,1004,6);
—
INSERT INTO SC(Sno,Cno,Grade) values(0002,1001,3);INSERT INTO SC(Sno,Cno,Grade) values(0002,1002,2);INSERT INTO SC(Sno,Cno,Grade) values(0002,1003,2);INSERT INTO SC(Sno,Cno,Grade) values(0002,1004,8);INSERT INTO SC(Sno,Cno,Grade) values(0003,1001,4);INSERT INTO SC(Sno,Cno,Grade) values(0003,1002,4);INSERT INTO SC(Sno,Cno,Grade) values(0003,1003,2);INSERT INTO SC(Sno,Cno,Grade) values(0003,1004,8);INSERT INTO SC(Sno,Cno,Grade) values(0004,1001,1);INSERT INTO SC(Sno,Cno,Grade) values(0004,1002,1);INSERT INTO SC(Sno,Cno,Grade) values(0004,1003,2);INSERT INTO SC(Sno,Cno,Grade) values(0004,1004,3);INSERT INTO SC(Sno,Cno,Grade) values(0005,1001,5);INSERT INTO SC(Sno,Cno,Grade) values(0005,1002,3);INSERT INTO SC(Sno,Cno,Grade) values(0005,1003,2);INSERT INTO SC(Sno,Cno,Grade) values(0005,1004,9);
检查,查看表格式或者表内容
mysql>desc SC; or select * from SC;
需求:统计学号、姓名、选的课程、学分
select
student.Sno,student.Sname,course.Cname,SC.Grade from student,course,SC where
student.Sno=SC.Sno and course.Cno=SC.Cno order by student.Sname;
优化
sql语句优化
explain 查看是否含有建立索引的语句mysql> explain select * from test where name='oldboy';在一个语句前面加上explain相当于模拟查询
创建索引
mysql> create index index_name on test(name);| name| char(20) | NO | MUL | NULL | mysql> explain select * from test where name='oldboy'\G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: test type: refpossible_keys: index_name key: index_name key_len: 60 ref: const rows: 1 查询行数,表示当前只查询了1行 Extra: Using where; Using index1 row in set (0.00 sec)
原图:
mysql> explain select * from test where name='oldboy'\G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: test type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 5 Extra: Using where1 row in set (0.00 sec)
帮助
mysql>
help explain 包含EXPLAIN的用法
使用explain命令优化SQL语句(select语句)的基本流程
1、 抓慢查询SQL语法方法
每隔2,秒输入mysql> SHOW FULL PROCESSLIST; 如果出现2次说明是慢查询mysql> SHOW FULL PROCESSLIST;
2、 分析慢查询日志
配置参数记录慢查询语句log_query_time = 2log_queries_not_using_indexeslog-slow-queries = /data/3306/slow.log
3、 对需要建索引的条件列建立索引
大表不能高峰期建立索引,300万条记录(如果访问已经慢了,可以直接创建)
4、 分析慢查询SQL的工具mysqlala(每天早上发邮件)
切割慢查询日志,去重分析后发给大家,如果并发太大可以按小时,去重。
1)mv 然后flush进程
2)cp复制,然后利用>清空。
3)定时任务
mv /data/3306/slow.log /opt/$(date +%F)_slow.logmysqladmin -uroot -poldboy -S /data/3306/mysql.sock flush-logs