mysql数据库的基本操作

2018-11-29  本文已影响0人  清风徐来_简

一、基本增删改查
二、创建索引的几种方式
三、单表查询
四、连表查询

一、基本增删改查

show databases; ---> 查看当前都有哪些数据库。

create database 数据库名; ---> 创建数据库(charset utf8,这个不写,为默认编码)。

show create database 数据库名; ---> 查看创建的数据库。

select database(); ---> 查看当前所在的库的位置(在哪个库里)。

use 数据库名; ---> 选择数据库。

drop database 数据库名; ---> 删除数据库。

alter database 数据库名 charset gbk; ---> 修改数据库的编码。

create table t1(id int,name char(10));--->(engine=innodb; 不写默认为innodb引擎),创建表。

insert into t1 values (1,'hehe'),(2,'haha'); ---> 向表中插入记录。

select * from t1;/ select id,name from t1; ---> 查询表内容。

desc t1; ---> 查看表结构。

show tables; ---> 查看当前数据库有哪些表。

show create table t1; ---> 查看表的详细结构。

show create table t1\G(不带分号)---> 也是查看表的详细结构,有一丢丢不同。

create table b1 select * from t1(db1.t1); ---> 复制t1表,为b1(可在本数据库或其他数据库复制)。

create table b2 select * from db1.t1 where 1>5 / false; ---> 只复制表结构,不要数据。

create table b3 like db1.t1; ---> 也是只复制表结构,不要数据。

drop table 表名; ---> 删除表。

alter table b1 add name char(11); ---> 增加单个字段。

alter table b1 add (age int,sex char(11)); ---> 增加多个字段;

alter table t1 drop id; ---> 删除单个字段。(不能全部删除,起码留一个)。

alter table b1 drop name,drop age; ---> 删除多个字段。

alter table t1 modify name char(6); ---> 修改表中name字段的char属性。

alter table t1 change name NAME char(7); ---> 修改表中name字段为NAME字段。

update db1.t1 set name='zhangsan';---> 修改t1中的所有name字段都是‘zhangsan’。

update db1.t1 set name='zhangsan' where id=2;---> 修改t1中id为2的name字段中,为‘zhangsan’。

delete from t1;---> 清空t1表的内容。

delete from t1 where id=2; ---> 清空t1表中id为2的内容。

二、创建索引的几种方式

1.唯一索引:

mysql> create table t1 (id int,name varchar(10),unique(name));

Query OK, 0 rows affected (0.35 sec)
mysql> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(10) | YES  | UNI | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
2.唯一索引方式2:

mysql> create table t5(id int,name varchar(10));

Query OK, 0 rows affected (0.34 sec)

mysql> alter table t5 add unique(id);

Query OK, 0 rows affected (0.27 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t5;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  | UNI | NULL    |       |
| name  | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)    
3.联合唯一索引:

mysql> create table t2(id int,name varchar(10),addr varchar(20),unique(name,addr));

Query OK, 0 rows affected (0.34 sec)
mysql> desc t2;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(10) | YES  | MUL | NULL    |       |
| addr  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
4.主键索引:

mysql> create table t3(id int,name varchar(10),primary key (id));

Query OK, 0 rows affected (0.34 sec)
mysql> desc t3;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
5.主键索引方式2:

mysql> create table t4(id int,name varchar(10));

Query OK, 0 rows affected (0.45 sec)

mysql> alter table t4 add primary key (id);

Query OK, 0 rows affected (0.64 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t4;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
6.普通索引:

mysql> create table t6 (id int,name varchar(10));

Query OK, 0 rows affected (0.40 sec)

mysql> alter table t6 add index id_index (id);

Query OK, 0 rows affected (0.28 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t6;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  | MUL | NULL    |       |
| name  | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

三、单表查询

四、连表查询

mysql> select * from department;
+------+--------------+
| id   | name         |
+------+--------------+
|  200 | 技术         |
|  201 | 人力资源     |                                   表一
|  202 | 销售         |
|  203 | 运营         |
+------+--------------+
4 rows in set (0.08 sec)
mysql> select * from employee;
+----+----------+--------+------+--------+
| id | name     | sex    | age  | dep_id |
+----+----------+--------+------+--------+
|  1 | aaaaa    | male   |   18 |    200 |
|  2 | bbbbb    | female |   48 |    201 |
|  3 | ccccc    | male   |   38 |    201 |                    表二
|  4 | xxxxx    | female |   28 |    202 |
|  5 | yyyyy    | male   |   18 |    200 |
|  6 | zzzzz    | female |   18 |    204 |
+----+----------+--------+------+--------+
6 rows in set (0.00 sec)

mysql> select * from employee,department;

+----+----------+--------+------+--------+------+--------------+
| id | name     | sex    | age  | dep_id | id   | name         |
+----+----------+--------+------+--------+------+--------------+
|  1 | aaaaa    | male   |   18 |    200 |  200 | 技术         |
|  1 | aaaaa    | male   |   18 |    200 |  201 | 人力资源     |
|  1 | aaaaa    | male   |   18 |    200 |  202 | 销售         |
|  1 | aaaaa    | male   |   18 |    200 |  203 | 运营         |
|  2 | bbbbb    | female |   48 |    201 |  200 | 技术         |
|  2 | bbbbb    | female |   48 |    201 |  201 | 人力资源     |
|  2 | bbbbb    | female |   48 |    201 |  202 | 销售         |
|  2 | bbbbb    | female |   48 |    201 |  203 | 运营         |
|  3 | ccccc    | male   |   38 |    201 |  200 | 技术         |
|  3 | ccccc    | male   |   38 |    201 |  201 | 人力资源     |
|  3 | ccccc    | male   |   38 |    201 |  202 | 销售         |               迪
|  3 | ccccc    | male   |   38 |    201 |  203 | 运营         |
|  4 | xxxxx    | female |   28 |    202 |  200 | 技术         |               卡
|  4 | xxxxx    | female |   28 |    202 |  201 | 人力资源     |
|  4 | xxxxx    | female |   28 |    202 |  202 | 销售         |               尔
|  4 | xxxxx    | female |   28 |    202 |  203 | 运营         |                
|  5 | yyyyy    | male   |   18 |    200 |  200 | 技术         |               积
|  5 | yyyyy    | male   |   18 |    200 |  201 | 人力资源     |                
|  5 | yyyyy    | male   |   18 |    200 |  202 | 销售         |
|  5 | yyyyy    | male   |   18 |    200 |  203 | 运营         |
|  6 | zzzzz    | female |   18 |    204 |  200 | 技术         |
|  6 | zzzzz    | female |   18 |    204 |  201 | 人力资源     |
|  6 | zzzzz    | female |   18 |    204 |  202 | 销售         |
|  6 | zzzzz    | female |   18 |    204 |  203 | 运营         |
+----+----------+--------+------+--------+------+--------------+
24 rows in set (0.00 sec)

通过上面发现,有许多数据是无效的,只有employee的dep_id =department 的 id那一列是真实有效的数据。所以引出了下文

mysql> select employee.id, employee.name, employee.age, employee.sex, department.name from employee inner join department on employee.dep_id = department.id;

+----+---------+------+--------+--------------+
| id | name    | age  | sex    | name         |
+----+---------+------+--------+--------------+
|  1 | aaaaa   |   18 | male   | 技术         |
|  2 | bbbbb   |   48 | female | 人力资源     |                      内连接
|  3 | ccccc   |   38 | male   | 人力资源     |
|  4 | xxxxx   |   28 | female | 销售         |
|  5 | yyyyy   |   18 | male   | 技术         |
+----+---------+------+--------+--------------+
5 rows in set (0.00 sec)

mysql> select * from employee left join department on employee.dep_id=department.id;

+----+----------+--------+------+--------+------+--------------+
| id | name     | sex    | age  | dep_id | id   | name         |
+----+----------+--------+------+--------+------+--------------+
|  1 | aaaaa    | male   |   18 |    200 |  200 | 技术         |
|  5 | yyyyy    | male   |   18 |    200 |  200 | 技术         |
|  2 | bbbbb    | female |   48 |    201 |  201 | 人力资源     |
|  3 | ccccc    | male   |   38 |    201 |  201 | 人力资源     |
|  4 | xxxxx    | female |   28 |    202 |  202 | 销售         |
|  6 | zzzzz    | female |   18 |    204 | NULL | NULL         |
+----+----------+--------+------+--------+------+--------------+
6 rows in set (0.00 sec)

mysql> select * from department left join employee on employee.dep_id=department.id;

+------+--------------+------+---------+--------+------+--------+
| id   | name         | id   | name    | sex    | age  | dep_id |
+------+--------------+------+---------+--------+------+--------+
|  200 | 技术         |    1 | aaaaa  | male   |   18 |    200 |
|  201 | 人力资源     |    2 | bbbbb   | female |   48 |    201 |
|  201 | 人力资源     |    3 | ccccc   | male   |   38 |    201 |
|  202 | 销售         |    4 | xxxxx   | female |   28 |    202 |
|  200 | 技术         |    5 | yyyyy   | male   |   18 |    200 |
|  203 | 运营         | NULL | NULL    | NULL   | NULL |   NULL |
+------+--------------+------+---------+--------+------+--------+
6 rows in set (0.00 sec)
示例1:以内连接的方式查询employee和department表,并且employee表中的age字段值必须大于25,
      即找出年龄大于25岁的员工以及员工所在的部门
select employee.name,department.name from employee inner join department
on employee.dep_id = department.id
where age > 25;
示例2:以内连接的方式查询employee和department表,并且以age字段的升序方式显示。
select employee.id,employee.name,employee.age,department.name from employee,department
where employee.dep_id = department.id
and age > 25
order by age asc;
上一篇 下一篇

猜你喜欢

热点阅读