快速入门MySQL教程【转自:http://xpleaf.blog.51cto.com/9315560/1712821】
|
1
2
|
xpleaf@xpleaf-machine:~$ mysql -h localhost -u root -pEnter password: |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 37Server version: 5.5.44-0ubuntu0.12.04.1 (Ubuntu)Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> |
|
1
2
3
4
5
6
7
8
9
10
|
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || manager_system || mysql || stu_info || xpleaf_server_data |+--------------------+ |
|
1
2
|
mysql> create database students_info;Query OK, 1 row affected (0.01 sec) |
|
1
2
3
4
5
6
7
8
9
10
11
|
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || manager_system || mysql || stu_info || students_info || xpleaf_server_data |+--------------------+ |
|
1
2
|
mysql> use students_info;Database changed |
|
1
2
3
4
5
6
7
8
9
|
mysql> create table network3 -> ( -> id char(10) not null primary key, -> name char(16) not null, -> sex char(6) not null, -> age int not null, -> address char(36) not null -> );Query OK, 0 rows affected (0.05 sec) |
| 列声明语句 | 解释说明 |
| id char(10) not null primary key | 创建一列,名称为id;数据类型为char字符类型,字符的最大长度为10个字符;并且该列内容不允许为空;同时把这一列作为这张表的主键,用来区分表中不同行。 |
| name char(16) not null | 创建一列,名称为name;数据类型为char字符类型,字符的最大长度为16个字符;并且该列内容不允许为空。 |
| sex char(6) not null | 创建一列,名称为sex;数据类型为char字符类型,字符的最大长度为6个字符;并且该列内容不允许为空。 |
| age int not null | 创建一列,名称为age;数据类型为int整型;并且该列内容不允许为空。 |
| address char(36) not null | 创建一列,名称为sex;数据类型为char字符类型,字符的最大长度为36个字符;并且该列内容不允许为空。 |
|
1
2
3
4
5
6
7
8
9
10
11
|
mysql> describe network3;+---------+----------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+---------+----------+------+-----+---------+-------+| id | char(10) | NO | PRI | NULL | || name | char(16) | NO | | NULL | || sex | char(6) | NO | | NULL | || age | int(11) | NO | | NULL | || address | char(36) | NO | | NULL | |+---------+----------+------+-----+---------+-------+5 rows in set (0.00 sec) |
|
1
2
|
mysql> insert into network3 values("3114006441","xpleaf","male",35,"QingYuan");Query OK, 1 row affected (0.01 sec) |
|
1
2
3
4
5
6
7
8
9
10
11
|
mysql> insert into network3 values("3114006442","Jim","male",38,"JiangMen");Query OK, 1 row affected (0.00 sec)mysql> insert into network3 values("3114006443","Pei","male",41,"PuNing");Query OK, 1 row affected (0.01 sec)mysql> insert into network3 values("3114006440","Xuan","male",36,"ShanWei");Query OK, 1 row affected (0.02 sec)mysql> insert into network3 values("3214006336","Ting","female",30,"ChaoShan");Query OK, 1 row affected (0.02 sec) |
|
1
|
select 列名称 from 数据库表名 [查询条件]; |
|
1
2
3
4
5
6
7
8
9
10
11
|
mysql> select * from network3;+------------+--------+--------+-----+----------+| id | name | sex | age | address |+------------+--------+--------+-----+----------+| 3114006440 | Xuan | male | 36 | ShanWei || 3114006441 | xpleaf | male | 35 | QingYuan || 3114006442 | Jim | male | 38 | JiangMen || 3114006443 | Pei | male | 41 | PuNing || 3214006336 | Ting | female | 30 | ChaoShan |+------------+--------+--------+-----+----------+5 rows in set (0.00 sec) |
|
1
2
3
4
5
6
7
8
9
10
11
|
mysql> select id,name from network3;+------------+--------+| id | name |+------------+--------+| 3114006440 | Xuan || 3114006441 | xpleaf || 3114006442 | Jim || 3114006443 | Pei || 3214006336 | Ting |+------------+--------+5 rows in set (0.00 sec) |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
mysql> select * from network3 where name='xpleaf';+------------+--------+------+-----+----------+| id | name | sex | age | address |+------------+--------+------+-----+----------+| 3114006441 | xpleaf | male | 35 | QingYuan |+------------+--------+------+-----+----------+1 row in set (0.02 sec)mysql> select * from network3 where sex='female';+------------+------+--------+-----+----------+| id | name | sex | age | address |+------------+------+--------+-----+----------+| 3214006336 | Ting | female | 30 | ChaoShan |+------------+------+--------+-----+----------+1 row in set (0.00 sec)mysql> select * from network3 where sex='male' and address='QingYuan';+------------+--------+------+-----+----------+| id | name | sex | age | address |+------------+--------+------+-----+----------+| 3114006441 | xpleaf | male | 35 | QingYuan |+------------+--------+------+-----+----------+1 row in set (0.00 sec)mysql> select * from network3 where age > 40;+------------+------+------+-----+---------+| id | name | sex | age | address |+------------+------+------+-----+---------+| 3114006443 | Pei | male | 41 | PuNing |+------------+------+------+-----+---------+1 row in set (0.00 sec)mysql> select * from network3 where age < 40 and age >= 31;+------------+--------+------+-----+----------+| id | name | sex | age | address |+------------+--------+------+-----+----------+| 3114006440 | Xuan | male | 36 | ShanWei || 3114006441 | xpleaf | male | 35 | QingYuan || 3114006442 | Jim | male | 38 | JiangMen |+------------+--------+------+-----+----------+3 rows in set (0.01 sec)mysql> select * from network3 where name like "%leaf";+------------+--------+------+-----+----------+| id | name | sex | age | address |+------------+--------+------+-----+----------+| 3114006441 | xpleaf | male | 35 | QingYuan |+------------+--------+------+-----+----------+1 row in set (0.00 sec) |
|
1
2
3
4
5
6
7
8
9
10
11
|
mysql> select * from network3;+------------+--------+--------+-----+----------+| id | name | sex | age | address |+------------+--------+--------+-----+----------+| 3114006440 | Xuan | male | 36 | ShanWei || 3114006441 | xpleaf | male | 35 | QingYuan || 3114006442 | Jim | male | 38 | JiangMen || 3114006443 | Pei | male | 41 | PuNing || 3214006336 | Ting | female | 30 | ChaoShan |+------------+--------+--------+-----+----------+5 rows in set (0.00 sec) |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#将name为"xpleaf"的address修改为"YuanTan"mysql> update network3 set address="YuanTan" where name='xpleaf';Query OK, 1 row affected (0.01 sec)Rows matched: 1 Changed: 1 Warnings: 0#将id为"3214006336"的name修改为"Hui"mysql> update network3 set name="Hui" where id='3214006336';Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0#将所有人的age加1mysql> update network3 set age=age+1;Query OK, 5 rows affected (0.01 sec)Rows matched: 5 Changed: 5 Warnings: 0 |
|
1
2
3
4
5
6
7
8
9
10
11
|
mysql> select * from network3;+------------+--------+--------+-----+----------+| id | name | sex | age | address |+------------+--------+--------+-----+----------+| 3114006440 | Xuan | male | 37 | ShanWei || 3114006441 | xpleaf | male | 36 | YuanTan || 3114006442 | Jim | male | 39 | JiangMen || 3114006443 | Pei | male | 42 | PuNing || 3214006336 | Hui | female | 31 | ChaoShan |+------------+--------+--------+-----+----------+5 rows in set (0.00 sec) |
|
1
2
|
mysql> delete from network3 where name='Pei';Query OK, 1 row affected (0.02 sec) |
|
1
2
3
4
5
6
7
8
9
10
|
mysql> select * from network3;+------------+--------+--------+-----+----------+| id | name | sex | age | address |+------------+--------+--------+-----+----------+| 3114006440 | Xuan | male | 37 | ShanWei || 3114006441 | xpleaf | male | 36 | YuanTan || 3114006442 | Jim | male | 39 | JiangMen || 3214006336 | Hui | female | 31 | ChaoShan |+------------+--------+--------+-----+----------+4 rows in set (0.00 sec) |
|
1
2
3
4
5
6
7
8
9
10
11
|
mysql> describe network3;+---------+----------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+---------+----------+------+-----+---------+-------+| id | char(10) | NO | PRI | NULL | || name | char(16) | NO | | NULL | || sex | char(6) | NO | | NULL | || age | int(11) | NO | | NULL | || address | char(36) | NO | | NULL | |+---------+----------+------+-----+---------+-------+5 rows in set (0.00 sec) |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#将列名称"adress"修改为"addr",其它保持不变mysql> alter table network3 change address addr char(30) not null;Query OK, 4 rows affected (0.04 sec)Records: 4 Duplicates: 0 Warnings: 0#将列"name"的数据类型修改为最大可以存放20个字符的char类型,其它保持不变mysql> alter table network3 change name name char(20) not null;Query OK, 4 rows affected (0.03 sec)Records: 4 Duplicates: 0 Warnings: 0#同时修改列"sex"的名称和数据类型mysql> alter table network3 change sex Sex char(10) not null;Query OK, 4 rows affected (0.05 sec)Records: 4 Duplicates: 0 Warnings: 0 |
|
1
2
3
4
5
6
7
8
9
10
11
|
mysql> describe network3;+-------+----------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+-------+| id | char(10) | NO | PRI | NULL | || name | char(20) | NO | | NULL | || Sex | char(10) | NO | | NULL | || age | int(11) | NO | | NULL | || addr | char(30) | NO | | NULL | |+-------+----------+------+-----+---------+-------+5 rows in set (0.01 sec) |
|
1
2
3
|
mysql> alter table network3 drop addr;Query OK, 4 rows affected (0.02 sec)Records: 4 Duplicates: 0 Warnings: 0 |
|
1
2
3
4
5
6
7
8
9
10
|
mysql> describe network3;+-------+----------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+-------+| id | char(10) | NO | PRI | NULL | || name | char(20) | NO | | NULL | || Sex | char(10) | NO | | NULL | || age | int(11) | NO | | NULL | |+-------+----------+------+-----+---------+-------+4 rows in set (0.00 sec) |
|
1
2
3
4
5
6
7
8
9
10
|
mysql> select * from network3;+------------+--------+--------+-----+| id | name | Sex | age |+------------+--------+--------+-----+| 3114006440 | Xuan | male | 37 || 3114006441 | xpleaf | male | 36 || 3114006442 | Jim | male | 39 || 3214006336 | Hui | female | 31 |+------------+--------+--------+-----+4 rows in set (0.00 sec) |
|
1
2
|
mysql> alter table network3 rename New_network3;Query OK, 0 rows affected (0.00 sec) |
|
1
2
3
4
5
6
7
|
mysql> show tables;+-------------------------+| Tables_in_students_info |+-------------------------+| New_network3 |+-------------------------+1 row in set (0.00 sec) |
|
1
2
|
mysql> drop table New_network3;Query OK, 0 rows affected (0.01 sec) |
|
1
2
|
mysql> show tables;Empty set (0.00 sec) |
|
1
2
|
mysql> drop database students_info;Query OK, 0 rows affected (0.00 sec) |



浙公网安备 33010602011771号