mysql 笔记。
1,链接数据库 :mysql -uroot -p
输入密码:********
强调:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 79 --->客户端的链接ID,记录了mysql服务到目前为止的链接次数;每个新的链接id都会自动加一。
2,创建数据库
mysql> create database test1;
Query OK, 1 row affected (0.03 sec)------->影响行数1
mysql> drop database test1;
Query OK, 0 rows affected (0.07 sec)------>影响行数0,mysql drop语句的影响行数都为0.
3,创建表:
create table tablename(id int(11) not null,name varchar(20) not null);
Query OK, 0 rows affected (0.14 sec) -------->影响行数0.
4,查看表定义:desc tablename;
mysql> desc user;
5,查看表定义(详细信息);
mysql> show create table user \G; ------------>参数\G ->作用是便于查看内容较长的记录。
6,删除表:
mysql> drop table user;
Query OK, 0 rows affected (0.06 sec)
7,修改表:
1,修改字段-->修改字段类型(modify)
mysql> alter table user modify name varchar(20) not null;
Query OK, 0 rows affected (0.30 sec)
Records: 0 Duplicates: 0 Warnings: 0
2,字段改名,及该字段类型(change)
mysql> alter table user change name ename varchar(30);
Query OK, 0 rows affected (0.32 sec)
Records: 0 Duplicates: 0 Warnings: 0
3,添加字段:
mysql> alter table user add column age int(2) not null;
Query OK, 0 rows affected (0.30 sec)
Records: 0 Duplicates: 0 Warnings: 0
4,删除表字段:
mysql> alter table user drop column age;
Query OK, 0 rows affected (0.29 sec)
Records: 0 Duplicates: 0 Warnings: 0
5,修改字段排列顺序
mysql> alter table user add column sex int(2) after name;
Query OK, 0 rows affected (0.31 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table user modify age int(2) after sex;
Query OK, 0 rows affected (0.28 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table user modify sex int(2) first;
Query OK, 0 rows affected (0.30 sec)
Records: 0 Duplicates: 0 Warnings: 0
6,更改表名:
mysql> alter table user rename admin;
Query OK, 0 rows affected (0.06 sec)
8,DML语句(主要针对表记录的操作);
1,插入语句:
mysql> insert into user(id,name) value (1,'wa');
Query OK, 1 row affected (0.05 sec)
mysql> insert into user values(2,'ho');
Query OK, 1 row affected (0.05 sec)
一次性插入多条记录:
mysql> insert into user values(3,'li'),(4,'wa'),(5,'yan');
Query OK, 3 rows affected (0.05 sec)
Records: 3 Duplicates: 0 Warnings: 0
2,更新语句:
mysql> update user set name = 'hhhhhhhh' where id=1;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
3,同时更新多个表中的数据
mysql> update emp a,dept b set a.sal=a.sal*b.deptno,b.deptname=a.ename where a.depton=b.depton;
Query OK, 2 rows affected (0.05 sec)
Rows matched: 2 Changed: 2 Warnings: 0
4,删除语句:
mysql> delete from user where id = 2;
Query OK, 1 row affected (0.07 sec)
5,查询语句:
1,普通查询:
select * from user;
select username from user;
2,查询不重复的记录:(distinct);
select distinct deptno from emp;
3,条件查询:
select * from user where id > 20 order by sex desc(降序)|asc(升序);
4,select * from user where depno = 5 order by sex desc | asc limit 10,20; --10 从第十条开始,显示20条记录。
5,select * from user where id > 200 GROUP BY sex WITH ROLLUP HAVING condition
HAVING 和width 区别:
having是对聚合都的结果进行条件过滤,而where 是聚合前就对记录进行过滤,如果逻辑允许,尽量进行where过滤,减少聚合率,提高效率。
6,select sum(sal),max(sal),min(sal) from user;
7,表链接:内链接和外链接
区别:内链接选出两张两张表中互相匹配的记录,外链接则选出其他不匹配的记录。
select ename,deptname from emp,dept where emp.depno = dept.depno;
内链接:左链接,右链接
左链接:包含所有的左边表中的记录,甚至是右边表中没有和他匹配的记录。
右链接:包含所有的右边表中的记录,甚至是左边表中没有和他匹配的记录。
mysql> select * from dept;
+--------+----------+
| depton | deptname |
+--------+----------+
| 1 | spider |
| 2 | java |
| 3 | plat |
+--------+----------+
3 rows in set (0.00 sec)
mysql> select * from emp;
+-------------+----------+-----+--------+
| ename | hiredate | sal | depton |
+-------------+----------+-----+--------+
| wanghongli | 20 | 0 | 1 |
| masongzhao | 30 | 2 | 2 |
| wubinjie | 30 | 3 | 2 |
| jiajinsong | 30 | 2 | 2 |
| lihuailiang | 30 | 3 | 2 |
+-------------+----------+-----+--------+
mysql> select ename,deptname from dept left join emp on dept.depton = emp.depton;
+-------------+----------+
| ename | deptname |
+-------------+----------+
| wanghongli | spider |
| masongzhao | java |
| wubinjie | java |
| jiajinsong | java |
| lihuailiang | java |
| NULL | plat |
+-------------+----------+
mysql> select ename,deptname from emp left join dept on dept.depton=emp.depton;
+-------------+----------+
| ename | deptname |
+-------------+----------+
| wanghongli | spider |
| masongzhao | java |
| wubinjie | java |
| jiajinsong | java |
| lihuailiang | java |
+-------------+----------+
8,子查寻(in,not in,=,!=,exists,not exists);
select * from emp where depton in (select depton from dept);
9,select 1+4;
10 ,常用函数:
mysql> select VERSION();
+-------------------------+
| VERSION() |
+-------------------------+
| 5.5.29-0ubuntu0.12.04.2 |
+-------------------------+
1 row in set (0.00 sec)
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| test1 |
+------------+
1 row in set (0.00 sec)
mysql> SELECT USER();
+----------------+
| USER() |
+----------------+
| root @localhost |
+----------------+
1 row in set (0.00 sec)
11,查看数据库支持的存储引擎:show engines \G;
mysql> show variables like 'have%';
12,索引的设计和使用。
1,myisam和innoDB都支持BTREE索引。默认情况下memory存储引擎支持HASH索引,也支持BTREE索引。mysql支持前缀索引,FULLTEXT索引(全文索引,但是只限于CHAR,VARCHAR,TEXT类型)
2,索引字段必须是非空的。
3,create index[UNIQUE|FULLTEXT|SPATIAL] cityname on city (city(10));
ALTER 添加索引。
1,链接数据库 :mysql -uroot -p
输入密码:********
强调:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 79 --->客户端的链接ID,记录了mysql服务到目前为止的链接次数;每个新的链接id都会自动加一。
2,创建数据库
mysql> create database test1;
Query OK, 1 row affected (0.03 sec)------->影响行数1
mysql> drop database test1;
Query OK, 0 rows affected (0.07 sec)------>影响行数0,mysql drop语句的影响行数都为0.
3,创建表:
create table tablename(id int(11) not null,name varchar(20) not null);
Query OK, 0 rows affected (0.14 sec) -------->影响行数0.
4,查看表定义:desc tablename;
mysql> desc user;
5,查看表定义(详细信息);
mysql> show create table user \G; ------------>参数\G ->作用是便于查看内容较长的记录。
6,删除表:
mysql> drop table user;
Query OK, 0 rows affected (0.06 sec)
7,修改表:
1,修改字段-->修改字段类型(modify)
mysql> alter table user modify name varchar(20) not null;
Query OK, 0 rows affected (0.30 sec)
Records: 0 Duplicates: 0 Warnings: 0
2,字段改名,及该字段类型(change)
mysql> alter table user change name ename varchar(30);
Query OK, 0 rows affected (0.32 sec)
Records: 0 Duplicates: 0 Warnings: 0
3,添加字段:
mysql> alter table user add column age int(2) not null;
Query OK, 0 rows affected (0.30 sec)
Records: 0 Duplicates: 0 Warnings: 0
4,删除表字段:
mysql> alter table user drop column age;
Query OK, 0 rows affected (0.29 sec)
Records: 0 Duplicates: 0 Warnings: 0
5,修改字段排列顺序
mysql> alter table user add column sex int(2) after name;
Query OK, 0 rows affected (0.31 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table user modify age int(2) after sex;
Query OK, 0 rows affected (0.28 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table user modify sex int(2) first;
Query OK, 0 rows affected (0.30 sec)
Records: 0 Duplicates: 0 Warnings: 0
6,更改表名:
mysql> alter table user rename admin;
Query OK, 0 rows affected (0.06 sec)
8,DML语句(主要针对表记录的操作);
1,插入语句:
mysql> insert into user(id,name) value (1,'wa');
Query OK, 1 row affected (0.05 sec)
mysql> insert into user values(2,'ho');
Query OK, 1 row affected (0.05 sec)
一次性插入多条记录:
mysql> insert into user values(3,'li'),(4,'wa'),(5,'yan');
Query OK, 3 rows affected (0.05 sec)
Records: 3 Duplicates: 0 Warnings: 0
2,更新语句:
mysql> update user set name = 'hhhhhhhh' where id=1;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
3,同时更新多个表中的数据
mysql> update emp a,dept b set a.sal=a.sal*b.deptno,b.deptname=a.ename where a.depton=b.depton;
Query OK, 2 rows affected (0.05 sec)
Rows matched: 2 Changed: 2 Warnings: 0
4,删除语句:
mysql> delete from user where id = 2;
Query OK, 1 row affected (0.07 sec)
5,查询语句:
1,普通查询:
select * from user;
select username from user;
2,查询不重复的记录:(distinct);
select distinct deptno from emp;
3,条件查询:
select * from user where id > 20 order by sex desc(降序)|asc(升序);
4,select * from user where depno = 5 order by sex desc | asc limit 10,20; --10 从第十条开始,显示20条记录。
5,select * from user where id > 200 GROUP BY sex WITH ROLLUP HAVING condition
HAVING 和width 区别:
having是对聚合都的结果进行条件过滤,而where 是聚合前就对记录进行过滤,如果逻辑允许,尽量进行where过滤,减少聚合率,提高效率。
6,select sum(sal),max(sal),min(sal) from user;
7,表链接:内链接和外链接
区别:内链接选出两张两张表中互相匹配的记录,外链接则选出其他不匹配的记录。
select ename,deptname from emp,dept where emp.depno = dept.depno;
内链接:左链接,右链接
左链接:包含所有的左边表中的记录,甚至是右边表中没有和他匹配的记录。
右链接:包含所有的右边表中的记录,甚至是左边表中没有和他匹配的记录。
mysql> select * from dept;
+--------+----------+
| depton | deptname |
+--------+----------+
| 1 | spider |
| 2 | java |
| 3 | plat |
+--------+----------+
3 rows in set (0.00 sec)
mysql> select * from emp;
+-------------+----------+-----+--------+
| ename | hiredate | sal | depton |
+-------------+----------+-----+--------+
| wanghongli | 20 | 0 | 1 |
| masongzhao | 30 | 2 | 2 |
| wubinjie | 30 | 3 | 2 |
| jiajinsong | 30 | 2 | 2 |
| lihuailiang | 30 | 3 | 2 |
+-------------+----------+-----+--------+
mysql> select ename,deptname from dept left join emp on dept.depton = emp.depton;
+-------------+----------+
| ename | deptname |
+-------------+----------+
| wanghongli | spider |
| masongzhao | java |
| wubinjie | java |
| jiajinsong | java |
| lihuailiang | java |
| NULL | plat |
+-------------+----------+
mysql> select ename,deptname from emp left join dept on dept.depton=emp.depton;
+-------------+----------+
| ename | deptname |
+-------------+----------+
| wanghongli | spider |
| masongzhao | java |
| wubinjie | java |
| jiajinsong | java |
| lihuailiang | java |
+-------------+----------+
8,子查寻(in,not in,=,!=,exists,not exists);
select * from emp where depton in (select depton from dept);
9,select 1+4;
10 ,常用函数:
mysql> select VERSION();
+-------------------------+
| VERSION() |
+-------------------------+
| 5.5.29-0ubuntu0.12.04.2 |
+-------------------------+
1 row in set (0.00 sec)
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| test1 |
+------------+
1 row in set (0.00 sec)
mysql> SELECT USER();
+----------------+
| USER() |
+----------------+
| root @localhost |
+----------------+
1 row in set (0.00 sec)
11,查看数据库支持的存储引擎:show engines \G;
mysql> show variables like 'have%';
12,索引的设计和使用。
1,myisam和innoDB都支持BTREE索引。默认情况下memory存储引擎支持HASH索引,也支持BTREE索引。mysql支持前缀索引,FULLTEXT索引(全文索引,但是只限于CHAR,VARCHAR,TEXT类型)
2,索引字段必须是非空的。
3,create index[UNIQUE|FULLTEXT|SPATIAL] cityname on city (city(10));
ALTER 添加索引。
来源:oschina
链接:https://my.oschina.net/u/1047106/blog/135140