MySQL数据库,每条命令后要加;号。不然会认为命令语句未输入完,
若在语句结尾不添加分号时, 命令提示符会以 -> 提示你继续输入(有个别特例, 但加分号是一定不会错的);
show databases; 查看所有的数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| abc |
| mysql |
| performance_schema |
| sys |
+--------------------+
mysql> use abc; 进入某个数据库,(对数据库内部的表格做创建和修改,必须先进入数据库中)
Database changed
mysql> create table stu1(id int, name varchar(10)); 创建表格
Query OK, 0 rows affected (2.06 sec)
mysql> show tables; 查看库内所有已创建的表
+---------------+
| Tables_in_abc |
+---------------+
| stu1 |
+---------------+
1 row in set (0.08 sec)
mysql> insert into stu1(id, name) values (2, 'chenkai'); 向表内插入数据 (指定了要插入id和name列)
Query OK, 1 row affected (0.13 sec)
mysql> insert into stu1 values (2, 'chenkai', '18839923411'); (未指定要插入哪一列,则为每一列数据都
Query OK, 1 row affected (0.10 sec) 要插入,则此时数据量一定要够)
mysql> select * from stu1; 查询表格数据。* 表示查询所有
+------+------------+
| id | name |
+------+------------+
| 1 | chenna |
| 2 | chenkai |
| 3 | hulin |
| 4 | chengcheng |
+------+------------+
mysql> show create table stu1; 查看指定的表的结构
+-------+-----------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------------------------------------------------------------------------------------+
| stu1 | CREATE TABLE `stu1` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.19 sec)rows in set (0.14 sec)
mysql> desc stu1; 查看指定的表的结构 (= show columns from 表名;)
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.30 sec)
mysql> select id from stu1; 查询表内的指定数据,(这里查询的是ID号)
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
mysql> update stu1 set name = 'xiaocheng' where name = 'chengcheng' 更新数据(把名字为 chengcehng 更新成xiaocheng)
Query OK, 1 row affected (0.41 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select name from stu1 where id=2; 查看ID为2 的名字
+---------+
| name |
+---------+
| chenkai |
+---------+
1 row in set (0.08 sec)
mysql> delete from stu1; Query OK, 4 rows affected (0.23 sec) 删除数据,这里删除的是表中的所有数据,表格本身并未删除
mysql> select * from stu1; Empty set (0.00 sec)
mysql> show tables;
+---------------+ |
Tables_in_abc |
+---------------+ |
stu1 |
+---------------+
1 row in set (0.07 sec)
mysql> delete from stu1 where id=6; 删除某一行数据(删除id号为6 的那行数据)
Query OK, 1 row affected (0.04 sec)
修改表结构:
增加一列
alter table 表名 add 列名 数据类型 after 前表名;
改变列名
alter table 表名 change 原列名 新列名 数据类型
修改类型
alter table 表名 modify 列名 数据类型
删除列
alter table 表名 drop 列名
修改表名
alter table 表名 rename 新表名
mysql> alter table stu1 change name Name varchar(10); 更改列名,这里可以顺带更改列的数据类型
Query OK, 0 rows affected (0.13 sec) 如果不更改数据类型,也要写上,不然会报错
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table stu1 add sex varchar(5) after Name; 添加新的列,可指定新添加的列的位置。
Query OK, 0 rows affected (0.39 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from stu1;
+------+------------+------+-------------+
| id | Name | sex | num |
+------+------------+------+-------------+
| 1 | chenna | NULL | 15195883599 |
| 4 | chenkai | NULL | 18839923411 |
| 3 | chenchen | NULL | NULL |
| 4 | chenkai | NULL | NULL |
| 5 | hulin | NULL | NULL |
| 7 | xingfu | NULL | NULL |
+------+------------+------+-------------+
6 rows in set (0.00 sec)
mysql> alter table stu1 modify sex varchar(10); 更改列的数据类型
Query OK, 0 rows affected (0.18 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table stu1 drop num; 删除某一列
Query OK, 0 rows affected (0.38 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table stu1 rename student1; 更改表的名字
Query OK, 0 rows affected (0.21 sec)