MySQL表的操作

文章目录

  • 表的操作
    • 创建表
    • 创建表案例
    • 查看表结构
    • 修改表
    • 删除表

表的操作

创建表

语法:

CREATE TABLE table_name( 
	field1 datatype,
	field2 datatype,
	field3 datatype
) character set 字符集 collate 校验规则 engine 存储引擎;

说明:

  • field 表示列名
  • datatype 表示列的类型
  • character set 字符集,如果没有指定字符集,则以所在数据库的字符集为准
  • collate 校验规则,如果没有指定校验规则,则以所在数据库的校验规则为准

创建表案例

create table student (
	id int,
	name varchar(20),
	password char(32)
) charset=utf8 engine MyISAM;

说明:

  • 不同的存储引擎,创建表的文件不一样。

users 表存储引擎是 MyISAM ,在数据目中有三个不同的文件,分别是:

  • users.frm:表结构
  • users.MYD:表数据
  • users.MYI:表索引

MySQL表的操作_第1张图片


查看表结构

语法:

desc 表名;

示例:

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

修改表

在项目实际开发中,经常修改某个表的结构,比如字段名字,字段大小,字段类型,表的字符集类型,表的存储引擎等等。我们还有需求,添加字段,删除字段等等。这时我们就需要修改表。

ALTER TABLE tablename ADD (column datatype [DEFAULT expr][,column datatype]...);

ALTER TABLE tablename MODIfy (column datatype [DEFAULT expr][,column datatype]...);

ALTER TABLE tablename DROP (column);

示例:在student表中添加一条记录

mysql> insert into student (id, name, password) values (1, 'Jay', '123456');
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+------+------+----------+
| id   | name | password |
+------+------+----------+
|    1 | Jay  | 123456   |
+------+------+----------+
1 row in set (0.00 sec)

在users表添加一个字段,用于保存性别

mysql> alter table student add sex varchar(2);
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

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

修改name,将其长度改成60

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

mysql> desc student;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| name     | varchar(60) | YES  |     | NULL    |       |
| password | char(32)    | YES  |     | NULL    |       |
| sex      | varchar(2)  | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

删除password列

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

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

修改表名为users

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

mysql> show tables;
+----------------------+
| Tables_in_helloworld |
+----------------------+
| users                |
+----------------------+
1 row in set (0.00 sec)

将name列修改为xingming

mysql> alter table users change name xingming varchar(32);
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> desc users;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| xingming | varchar(32) | YES  |     | NULL    |       |
| sex      | varchar(2)  | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

删除表

语法:

DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ...

你可能感兴趣的:(MySQL,mysql,数据库,c++)