1.1 create 创建数据库
1.2 show 查看所有数据库
1.3 alter 修改数据库
1.4 use 使用数据库
1.5 查看当前使用的数据库
1.6 drop 删除数据库
2、数据表(table)管理
2.1 create 创建表
2.2 show 显示表
2.3 desc 查看表结构
2.4 alter 修改表结构(增、删、改)
2.4.1 insert 在表中添加列(字段)
2.4.2 alter 修改表(列)字段
2.4.3 delete 删除表(列)字段
2.4.4 rename 重命名表名
2.5 create 利用已有数据创建新表
3、数据的操作及管理
3.1 增加数据(增)
3.2 删除数据(删)
3.3 修改数据(改)
3.4 查询数据(查)
1、数据库(database)管理
1.1 create 创建数据库
create database firstDB;
mysql> show databases; +--------------------+| Database|+--------------------+| information_schema || firstDB|| mysql || performance_schema |+--------------------+4 rows in set (0.00 sec)
mysql> ALTER DATABASE testDB CHARACTER SET UTF8;Query OK, 1 row affected (0.00 sec)
mysql> use firstDB;Database changed
mysql> select database();+------------+| database() |+------------+| firstdb|+------------+1 row in set (0.00 sec)
mysql> drop database firstDB;Query OK, 0 rows affected (0.00 sec)
mysql> create database testDB;Query OK, 1 row affected (0.00 sec)创建后记得用use命令进入(使用)数据库,不然后面的操作都会不成功的。
mysql> create table PEOPLE (-> ID int AUTO_INCREMENT PRIMARY KEY,-> NAME varchar(20) not null,-> AGE int not null,-> BIRTHDAY datetime); Query OK, 0 rows affected (0.01 sec)
mysql> show tables;+------------------+| Tables_in_testdb |+------------------+| PEOPLE|+------------------+1 row in set (0.00 sec)
mysql> desc PEOPLE-> ;+----------+-------------+------+-----+---------+----------------+| Field| Type| Null | Key | Default | Extra |+----------+-------------+------+-----+---------+----------------+| ID| int(11) | NO| PRI | NULL| auto_increment || NAME | varchar(20) | NO| | NULL||| AGE | int(11) | NO| | NULL||| BIRTHDAY | datetime| YES | | NULL||+----------+-------------+------+-----+---------+----------------+4 rows in set (0.01 sec)
mysql> ALTER TABLE KEYCHAIN CONVERT TO CHARACTER SET UTF8;Query OK, 1 row affected (0.02 sec)Records: 1 Duplicates: 0 Warnings: 0
mysql> alter table PEOPLE add star BOOL;Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0提示:在MySQL里,布尔类型会自动转换为tinyint(1)类型。
mysql> desc PEOPLE;+----------+-------------+------+-----+---------+----------------+| Field| Type| Null | Key | Default | Extra |+----------+-------------+------+-----+---------+----------------+| ID| int(11) | NO| PRI | NULL| auto_increment || NAME | varchar(20) | NO| | NULL||| AGE | int(11) | NO| | NULL||| BIRTHDAY | datetime| YES | | NULL||| star | tinyint(1) | YES | | NULL||+----------+-------------+------+-----+---------+----------------+5 rows in set (0.00 sec)现在,你该相信我了吧?
mysql> alter table PEOPLE MODIFY star int;Query OK, 0 rows affected (0.01 sec)Records: 0 Duplicates: 0 Warnings: 0
mysql> desc PEOPLE;+----------+-------------+------+-----+---------+----------------+| Field| Type| Null | Key | Default | Extra |+----------+-------------+------+-----+---------+----------------+| ID| int(11) | NO| PRI | NULL| auto_increment || NAME | varchar(20) | NO| | NULL||| AGE | int(11) | NO| | NULL||| BIRTHDAY | datetime| YES | | NULL||| star | int(11) | YES | | NULL||+----------+-------------+------+-----+---------+----------------+5 rows in set (0.00 sec)
mysql> alter table PEOPLE DROP column star;Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0
mysql> desc PEOPLE;+----------+-------------+------+-----+---------+----------------+| Field| Type| Null | Key | Default | Extra |+----------+-------------+------+-----+---------+----------------+| ID| int(11) | NO| PRI | NULL| auto_increment || NAME | varchar(20) | NO| | NULL||| AGE | int(11) | NO| | NULL||| BIRTHDAY | datetime| YES | | NULL||+----------+-------------+------+-----+---------+----------------+4 rows in set (0.00 sec)删除字段成功,现在我们已经不能看到star的字段了。
mysql> RENAME TABLE PEOPLE TO NEW_PEOPLE;Query OK, 0 rows affected (0.00 sec)
mysql> create table newTable select * from PEOPLE;Query OK, 0 rows affected (0.01 sec)Records: 0 Duplicates: 0 Warnings: 0
mysql> show tables;+------------------+| Tables_in_testdb |+------------------+| PEOPLE|| newTable |+------------------+2 rows in set (0.00 sec)
mysql> insert into PEOPLE VALUES (null, ‘Anny‘, 22, ‘1992-05-22‘);Query OK, 1 row affected (0.00 sec)
mysql> select * from PEOPLE;+----+------+-----+---------------------+| ID | NAME | AGE | BIRTHDAY|+----+------+-----+---------------------+| 1 | Anny | 22 | 1992-05-22 00:00:00 |+----+------+-----+---------------------+1 row in set (0.00 sec)数据表现在有一条数据。
mysql> select * from PEOPLE;+----+--------+-----+---------------------+| ID | NAME| AGE | BIRTHDAY|+----+--------+-----+---------------------+| 1 | Anny| 22 | 1992-05-22 00:00:00 || 2 | Garvey | 23 | 1991-05-22 00:00:00 || 3 | Lisa| 25 | 1989-05-22 00:00:00 || 4 | Nick| 24 | 1990-05-22 00:00:00 || 5 | Rick| 24 | 1991-05-22 00:00:00 |+----+--------+-----+---------------------+5 rows in set (0.00 sec)
mysql> delete from PEOPLE where name = ‘Lisa‘;Query OK, 1 row affected (0.01 sec)
mysql> select * from PEOPLE;+----+--------+-----+---------------------+| ID | NAME| AGE | BIRTHDAY|+----+--------+-----+---------------------+| 1 | Anny| 22 | 1992-05-22 00:00:00 || 2 | Garvey | 23 | 1991-05-22 00:00:00 || 4 | Nick| 24 | 1990-05-22 00:00:00 || 5 | Rick| 24 | 1991-05-22 00:00:00 |+----+--------+-----+---------------------+4 rows in set (0.00 sec)已经看不到名为“Lisa”的数据了。
mysql> update PEOPLE set name=‘Calvin‘ where name = ‘Garvey‘;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from PEOPLE;+----+--------+-----+---------------------+| ID | NAME| AGE | BIRTHDAY|+----+--------+-----+---------------------+| 1 | Anny| 22 | 1992-05-22 00:00:00 || 2 | Calvin | 23 | 1991-05-22 00:00:00 || 4 | Nick| 24 | 1990-05-22 00:00:00 || 5 | Rick| 24 | 1991-05-22 00:00:00 |+----+--------+-----+---------------------+4 rows in set (0.00 sec)名为“Garvey”的记录已经修改为“Calvin”。
mysql> select * from PEOPLE;+----+--------+-----+---------------------+| ID | NAME| AGE | BIRTHDAY|+----+--------+-----+---------------------+| 1 | Anny| 22 | 1992-05-22 00:00:00 || 2 | Calvin | 23 | 1991-05-22 00:00:00 || 4 | Nick| 24 | 1990-05-22 00:00:00 || 5 | Rick| 24 | 1991-05-22 00:00:00 |+----+--------+-----+---------------------+4 rows in set (0.00 sec)格式:select * from <表名>,*代表所有字段。
mysql> select NAME, AGE, BIRTHDAY from PEOPLE;+--------+-----+---------------------+| NAME| AGE | BIRTHDAY|+--------+-----+---------------------+| Anny| 22 | 1992-05-22 00:00:00 || Calvin | 23 | 1991-05-22 00:00:00 || Nick| 24 | 1990-05-22 00:00:00 || Rick| 24 | 1991-05-22 00:00:00 |+--------+-----+---------------------+4 rows in set (0.00 sec)格式:select <字段名,字段名,...> from <表名>。