五、SQL之表操作

一、介绍

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

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

二、创建表

语法

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

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

创建表

mysql> create database db1 charset utf8;   # 创建数据库
Query OK, 1 row affected (0.01 sec)

mysql> use db1;    # 进入数据库
Database changed


mysql> create table t1(id int,name varchar(50),sex enum('male','female'),age int(3));                          # 创建表
Query OK, 0 rows affected (0.01 sec)


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(11)               | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
4 rows in set (0.02 sec)


mysql> select * from t1;   # 查看表数据
Empty set (0.00 sec)

插入表数据

mysql> insert into t1 values (1,'zhangsan',18,'male');  # 单条插入
Query OK, 1 row affected, 2 warnings (0.00 sec)

mysql> insert into t1 values (2,'lisi',30,'male'),(3,'wangwu',22,'male'),(4,'zhaoliu',35,'male');   # 多条插入
Query OK, 3 rows affected, 6 warnings (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 6

mysql> select * from t1;   # 查询
+------+----------+------+------+
| id   | name     | sex  | age  |
+------+----------+------+------+
|    1 | zhangsan |      |    0 |
|    2 | lisi     |      |    0 |
|    3 | wangwu   |      |    0 |
|    4 | zhaoliu  |      |    0 |
+------+----------+------+------+
4 rows in set (0.00 sec)


三、查看表结构


mysql> desc t1;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

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



四、表完整性约束

链接:https://www.cnblogs.com/baishuchao/articles/9174664.html

五、修改表ALTER TABLE

语法

语法:
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 旧字段名 新字段名 新数据类型 [完整性约束条件…];

1. 修改存储引擎
mysql> alter table t1 engine=innodb;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

2. 添加字段

mysql> alter table t1 add school varchar(20) not null;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

3. 删除字段

mysql> alter table t1 drop school;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

4. 修改字段类型modigy
mysql> alter table t1 modify age int(10);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

5. 增加约束(针对已有的主键增加auto_increment)

mysql> alter table t1 modify id int(11) not null primary key auto_increment;
ERROR 1068 (42000): Multiple primary key defined

mysql> alter table t1 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 t1
    -> 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 t1modify id int(11) not null; 

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

六、修改表

复制表结构+记录 (key不会复制: 主键、外键和索引)
mysql> create table new_t1 select * from service;

只复制表结构
mysql> select * from t1 where 1=2; //条件为假,查不到任何记录
Empty set (0.00 sec)
mysql> create table new1_t1 t1 * from service where 1=2;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> create table t4 like employees;

七、删除表

DROP TABLE 表名;

posted @ 2018-06-12 19:24  云原生运维社区  阅读(162)  评论(0编辑  收藏  举报