mysql表的操作

创建表

  • 语法

create table table_name(
field1 datatype,
field2 datatype,
field3 datatype
) character set 字符集 collate 校验规则 ;

  • 查看表结构

desc 表名


mysql> desc users;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| name     | varchar(20) | YES  |     | NULL    |       |
| password | char(32)    | YES  |     | NULL    |       |
| birthday | date        | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

修改表

  • 在users表添加一个字段,用于保存图片路径

alter table users add image varchar(100) comment ‘图片路径’ after birthday;


mysql> alter table users add  image varchar(100) comment '图片路径' after birthday;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc users;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int(11)      | YES  |     | NULL    |       |
| name     | varchar(20)  | YES  |     | NULL    |       |
| password | char(32)     | YES  |     | NULL    |       |
| birthday | date         | YES  |     | NULL    |       |
| image    | varchar(100) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
  • 修改name,将其长度改成60

alter table users modify name varchar(60);


mysql> alter table users modify name varchar(60);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc users;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int(11)      | YES  |     | NULL    |       |
| name     | varchar(60)  | YES  |     | NULL    |       |
| password | char(32)     | YES  |     | NULL    |       |
| birthday | date         | YES  |     | NULL    |       |
| image    | varchar(100) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
  • 删除password列

alter table users drop password;


mysql> alter table users drop password;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc users;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int(11)      | YES  |     | NULL    |       |
| name     | varchar(60)  | YES  |     | NULL    |       |
| birthday | date         | YES  |     | NULL    |       |
| image    | varchar(100) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
  • 修改表名为employee

alter table users rename to employee;


mysql> alter table users rename to employee;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+---------------+
| Tables_in_aaa |
+---------------+
| employee      |
| person        |
+---------------+
2 rows in set (0.00 sec)
  • 修改字符集为gbk

alter table employee charset=gbk;


mysql> show create table employee;
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                                                                    |
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| employee | CREATE TABLE `employee` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(60) CHARACTER SET utf8 DEFAULT NULL,
  `birthday` date DEFAULT NULL COMMENT '生日',
  `image` varchar(100) CHARACTER SET utf8 DEFAULT NULL COMMENT '图片路径'
) ENGINE=MyISAM DEFAULT CHARSET=gbk       |
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
  • 将name列修改为xingming

alter table employee change name xingming varchar(60);


mysql> alter table employee change name xingming varchar(60); 
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc employee;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int(11)      | YES  |     | NULL    |       |
| xingming | varchar(60)  | YES  |     | NULL    |       |
| birthday | date         | YES  |     | NULL    |       |
| image    | varchar(100) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

你可能感兴趣的:(mysql)