(3.2)表相关操作之表的增删改查

一、表介绍


表相当于文件,表中的一条记录就相当于文件的一行内容,不同的是,表中的一条记录有对应的标题,称为表的字段。

id,name,age,sex称为字段,其余的,一行内容称为一条记录

 

二、创建表


1,语法:

# 语法:
create table 表名(
字段名1 类型[(宽度) 约束条件],
字段名2 类型[(宽度) 约束条件],
字段名3 类型[(宽度) 约束条件]
);

# 注意:
1. 在同一张表中,字段名是不能相同
2. 宽度和约束条件可选
3. 字段名和类型是必须的

2,示范:

mysql> create database db1 charset utf8;
Query OK, 1 row affected, 1 warning (0.11 sec)

mysql> use db1;
Database changed
mysql>
mysql> create table t1(
    -> id int,
    -> name varchar(50),
    -> sex enum('male','female'),
    -> age int(3)
    -> );
Query OK, 0 rows affected, 1 warning (0.63 sec)

mysql> show tables;     # 查看 db1 库下的所有表名
+---------------+
| Tables_in_db1 |
+---------------+
| t1            |
+---------------+
1 row in set (0.00 sec)

mysql> desc t1;         # 这个更清晰
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int                   | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| age   | int                   | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
4 rows in set (0.07 sec)

mysql> select id,name,sex,age from t1;
Empty set (0.00 sec)

mysql> select * from t1;
Empty set (0.00 sec)

mysql> select id,name from t1;
Empty set (0.00 sec)

mysql>

3,往表中插入数据

mysql> insert into t1 values
    -> (1,'zixi','male',18),
    -> (2,'suosuo','male',20)
    -> ;
Query OK, 2 rows affected (0.10 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> insert into t1(id) values
    -> (3),
    -> (4);
Query OK, 2 rows affected (0.07 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> select * from t1;
+------+--------+------+------+
| id   | name   | sex  | age  |
+------+--------+------+------+
|    1 | zixi   | male |   18 |
|    2 | suosuo | male |   20 |
|    3 | NULL   | NULL | NULL |
|    4 | NULL   | NULL | NULL |
+------+--------+------+------+
4 rows in set (0.00 sec)

mysql>

!!!注意!!!:表中的最后一个字段不要加逗号

 

三、查看表结构


如下:

mysql> describe t1;                 # 查看表结构,可简写为 desc 表名
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int                   | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| age   | int                   | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> show create table t1\G           # 查看表详细结构,可加\G
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int DEFAULT NULL,
  `name` varchar(50) DEFAULT NULL,
  `sex` enum('male','female') DEFAULT NULL,
  `age` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql>

 

四、修改表结构


1,语法:

语法:
1. 修改表名
      ALTER TABLE 表名 
                          RENAME 新表名;

2. 增加字段
      ALTER TABLE 表名
                          ADD 字段名  数据类型 [完整性约束条件…],
                          ADD 字段名  数据类型 [完整性约束条件…];
      ALTER TABLE 表名
                          ADD 字段名  数据类型 [完整性约束条件…]  FIRST;
      ALTER TABLE 表名
                          ADD 字段名  数据类型 [完整性约束条件…]  AFTER 字段名;

3. 删除字段
      ALTER TABLE 表名 
                          DROP 字段名;

4. 修改字段
      ALTER TABLE 表名 
                          MODIFY  字段名 数据类型 [完整性约束条件…];
      ALTER TABLE 表名 
                          CHANGE 旧字段名 新字段名 旧数据类型 [完整性约束条件…];
      ALTER TABLE 表名 
                          CHANGE 旧字段名 新字段名 新数据类型 [完整性约束条件…];

2,示例:

1. 修改存储引擎
mysql> alter table service 
    -> engine=innodb;
2. 添加字段
mysql> alter table student10
    -> add name varchar(20) not null,
    -> add age int(3) not null default 22;

mysql> alter table student10
    -> add stu_num varchar(10) not null after name;                //添加name字段之后

mysql> alter table student10                        
    -> add sex enum('male','female') default 'male' first;          //添加到最前面

3. 删除字段
mysql> alter table student10
    -> drop sex;

mysql> alter table service
    -> drop mac;

4. 修改字段类型modify
mysql> alter table student10
    -> modify age int(3);
mysql> alter table student10
    -> modify id int(11) not null primary key auto_increment;    //修改为主键

5. 增加约束(针对已有的主键增加auto_increment)
mysql> alter table student10 modify id int(11) not null primary key auto_increment;
ERROR 1068 (42000): Multiple primary key defined

mysql> alter table student10 modify id int(11) not null auto_increment;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

6. 对已经存在的表增加复合主键
mysql> alter table service2
    -> add primary key(host_ip,port);        

7. 增加主键
mysql> alter table student1
    -> modify name varchar(10) not null primary key;

8. 增加主键和自动增长
mysql> alter table student1
    -> modify id int not null primary key auto_increment;

9. 删除主键
a. 删除自增约束
mysql> alter table student10 modify id int(11) not null; 

b. 删除主键
mysql> alter table student10                                 
    -> drop primary key;

 

五、复制表


如下:

1,复制表结构 + 记录(key不会复制:主键,外键和索引)

mysql> use db1;
Database changed
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1            |
+---------------+
1 row in set (0.00 sec)

mysql> desc t1;            # 看表结构
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int                   | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| age   | int                   | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> select * from t1;        # 看记录
+------+--------+------+------+
| id   | name   | sex  | age  |
+------+--------+------+------+
|    1 | zixi   | male |   18 |
|    2 | suosuo | male |   20 |
|    3 | NULL   | NULL | NULL |
|    4 | NULL   | NULL | NULL |
+------+--------+------+------+
4 rows in set (0.00 sec)
mysql> create table t2 select * from t1;        # 复制表结构和记录给t2
Query OK, 4 rows affected (0.48 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> desc t2;                # 查看 t2 的表结构
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int                   | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| age   | int                   | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> select * from t2;        # 查看 t2 的记录,和 t1 的是一样的
+------+--------+------+------+
| id   | name   | sex  | age  |
+------+--------+------+------+
|    1 | zixi   | male |   18 |
|    2 | suosuo | male |   20 |
|    3 | NULL   | NULL | NULL |
|    4 | NULL   | NULL | NULL |
+------+--------+------+------+
4 rows in set (0.00 sec)

mysql>

2,只拷贝表结构,不要记录(方法一:)

mysql> select * from t1 where 1>5;        # 这个条件为假,查不出结果,但是有表结构。
Empty set (0.04 sec)
mysql> create table t3 select * from t1 where 1>5;    # 复制表
Query OK, 0 rows affected (0.38 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t3;         # 查看表结构
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int                   | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| age   | int                   | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> select * from t3;        # 只复制了表结构,所以记录为空
Empty set (0.00 sec)

mysql>

3,只拷贝表结构,不要记录(方法二:)

mysql> create table t4 like t1;        # 只拷贝 t1 的表结构
Query OK, 0 rows affected (0.62 sec)

mysql> desc t4;            # t4 的表结构与 t1是一样的
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int                   | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| age   | int                   | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> select * from t4;        # 记录是没有的
Empty set (0.00 sec)

mysql>

 

六、删除表


如下:

DROP TABLE 表名;

 

posted @ 2020-08-08 11:06  zoling7  阅读(134)  评论(0编辑  收藏  举报