创建表+数据增删改查

### 一:环境搭建

​               启动数据库服务

​               配置环境变量----客户端

​               创建用户

​               赋予权限

​               创建数据库

​               创建表

### 二:数据库表

###     1:创建表的方法

```mysql
   mysql> create table if not exists _testmoviestar(
    -> t_name int,
    -> t_address int,
    -> t_gender enum('男','女'),
    -> t_birthdata date
    -> );
Query OK, 0 rows affected (0.20 sec)
```

###     2:数据类型

​                整数

​                小数

​                字符串

​                时间

​                特殊类型  枚举  集合  json

###    3:数据的操作

​                 数据的存储,表格中添加数据

​                 `insert into 表名(字段列表)values(对应的值)`

###    4:数据库的数据约束

####              1:非空约束 not null

​                     当字段添加非空约束时,当前字段不允许插入null值

​                      如果插入会报错

​                   `字段名   类型(长度)  not null`

####               2:默认 default

​                     如果没有给此字段添加数据,默认自动添加默认值

​                   `字段名  类型(长度) default  默认值`

*例如:*

```mysql
mysql> create table _testdef(
-> t_user varchar(10) not null,
-> t_passw varchar(10) default '33333'
-> );
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    20
Current database: movie

Query OK, 0 rows affected (0.20 sec)

mysql> insert into _testdef(t_user) values('张三');
Query OK, 1 row affected (0.03 sec)

mysql> select * from _testdef;
+--------+---------+
| t_user | t_passw |
+--------+---------+
| 张三   | 33333   |
+--------+---------+
1 row in set (0.00 sec)

mysql> insert into _testdef(t_user,t_passw)values('李四','1234');
Query OK, 1 row affected (0.07 sec)

mysql> select * from _testdef;
+--------+---------+
| t_user | t_passw |
+--------+---------+
| 张三   | 33333   |
| 李四   | 1234    |
+--------+---------+
2 rows in set (0.00 sec)
```
####               3:检查cherk

​                      检测传入的值是否在给定范围内

​                 mysql不支持  使用 enum 和 set 实现

####               4:唯一unipue

​                      一旦字段被定义成唯一约束,表示当前字段的值不能重复,可以为null

​                  *语法:*

​                        **字段添加:**`字段名  类型(长度)unique`

​                        **独立添加:**`【constraint】约束名  unique(字段名)`

​         因为标注的数据列是唯一的,所以方便与查询,而数据库系统会自动对该字段建立索引 **Btree**【二叉树】

​         如果没有给唯一约束起名字,默认是约束创建的索引名字是字段名,可以自己给索引起名字

​                       *查看对应表的索引语法:*

​                              `show  index from  表名;`

*例如:*

```mysql
mysql> create table _testuni(
-> t_id int unique,
-> t_name varchar(10)
-> );
Query OK, 0 rows affected (0.32 sec)

mysql> desc _testuni;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| t_id   | int(11)     | YES  | UNI | NULL    |       |
| t_name | varchar(10) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> insert into _testuni(t_id,t_name)values(12,'傻逼');
Query OK, 1 row affected (0.05 sec)

mysql> insert into _testuni(t_id,t_name)values(12,'傻逼');
ERROR 1062 (23000): Duplicate entry '12' for key 't_id'
mysql> show index from _testuni;
+----------+------------+----------+--------------+-------------+-----------+---
----------+----------+--------+------+------------+---------+---------------+
| Table    | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Ca
rdinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------+--------------+-------------+-----------+---
----------+----------+--------+------+------------+---------+---------------+
| _testuni |          0 | t_id     |            1 | t_id        | A         |
        1 |     NULL | NULL   | YES  | BTREE      |         |               |
+----------+------------+----------+--------------+-------------+-----------+---
----------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
```
####               5:主键约束  primary key

​                           相当于unique + not null,但是 primary key 比 unique + not null 高

​                           主键约束的字段,唯一且不为空【因为null值在数据库中表示无穷,无法用于比较】

​                           主键才是替代 unique 来进行准确的数据查询

​                     **主键约束分类:**

​                          单主键的方式,一个表中只有一列拿出来作为主键,方便唯一查询

​                     **联合主键的方式:**

​                          一个表中有多个字段一起来表示一条数据唯一,不为空,是unique做不到的

​                    *例如:*

| uname【primary key】 | uid【primary key】 |
| :------------------: | :----------------: |
|         张三         |        123         |
|         张三         |        1234        |
|         李四         |        123         |

创建方式:

​          `字段:字段名  类型(长度) primary key`

​          `独立:constraint  约束名  primary key(字段1,字段2)`

  备注:每一个表只能创建一个主键,只能用一次 primary key

​              联合主键唯一方式:

​                   主键字段1 - 主键字段2 - 主键字段3……的值是唯一的

​              联合主键比较耗资源---非必要不用

*例如:*

```mysql
mysql> create table _testpri2(
    -> t_user varchar(10),
    -> t_passw varchar(11),
    -> constraint myconcatprikey primary key(t_user,t_passw)
    -> );
Query OK, 0 rows affected (0.27 sec)

mysql> desc _testpri2;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| t_user  | varchar(10) | NO   | PRI | NULL    |       |
| t_passw | varchar(11) | NO   | PRI | NULL    |       |
+---------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> show index from _testpri2;
+-----------+------------+----------+--------------+-------------+-----------+--
-----------+----------+--------+------+------------+---------+---------------+
| Table     | Non_unique | Key_name | Seq_in_index | Column_name | Collation | C
ardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+----------+--------------+-------------+-----------+--
-----------+----------+--------+------+------------+---------+---------------+
| _testpri2 |          0 | PRIMARY  |            1 | t_user      | A         |
         0 |     NULL | NULL   |      | BTREE      |         |               |
| _testpri2 |          0 | PRIMARY  |            2 | t_passw     | A         |
         0 |     NULL | NULL   |      | BTREE      |         |               |
+-----------+------------+----------+--------------+-------------+-----------+--
-----------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> insert into _testpri2(t_user,t_passw)values('张三','123');
Query OK, 1 row affected (0.03 sec)

mysql> insert into _testpri2(t_user,t_passw)values('张三1','123');
Query OK, 1 row affected (0.09 sec)

mysql> insert into _testpri2(t_user,t_passw)values('张三1','123');
ERROR 1062 (23000): Duplicate entry '张三1-123' for key 'PRIMARY'
mysql> insert into _testpri2(t_user,t_passw)values('张三1','1233');
Query OK, 1 row affected (0.03 sec)

```

####               6:外键约束 froeign key

​                          主要用来关联多个表

​                          外键一般关联另一个表的主键

​                          外键与被关联的主键定义一致,依赖于被关联的主键存在

​                *语法:*

​                       `【constraint  外键的名字】foreign key(字段名) references  关联的表(主键字段)`

​        注意:一旦两个表发生了主外键关联,外键字段中添加的值必须是关联的主键字段中有的才行

例如:学生和成绩的关联

​              学生表:学号  名字

​              成绩表:学生学号  成绩  科目

```mysql

```

####               7:自增约束  auto_Increment

​                              当字段名被定义了自增约束,当前字段的数据就会自动增长,一般用表格数据的主键编号

​                    *语法:*

​                         `字段名  类型(长度) auto_Increment`

####               8:删除表的操作

​                          *语法:*

​                              `drop  table 【if exists】表1,表2……`

####               9:修改表

#####                        1)查看数据库表创建语法

​                              `show create table  表名;`

#####                        2)修改数据库表的名字

​                               `alter table  原来的表名  rename  to  新的表名;`

​                               `rename table 原来的表名 to 新的表名;`

```mysql
 mysql> create table student(
-> s_id int
-> );
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    45
Current database: work

Query OK, 0 rows affected (0.31 sec)

mysql> alter table student rename to stu;
Query OK, 0 rows affected (0.10 sec)

mysql> show tables;
+----------------+
| Tables_in_work |
+----------------+
| stu            |
+----------------+
1 row in set (0.00 sec)
```
#####                        3)添加信息

​                     **添加字段:**

​                           `alter table  表名  add   column  字段名(长度)【约束】【first  |  after  字段】`

```
mysql> alter table student add column s_name varchar(10) not null;
Query OK, 0 rows affected (0.52 sec)
Records: 0  Duplicates: 0  Warnings: 0
```

​                      **添加约束和索引等:(独立添加)**

​                           `alter  table  表名  add  primary  key(字段)`或

​                                                                  `foreign key(字段)`或

​                                                                   `references  表名(字段)`或

​                                                                   `unique(字段)`

```
mysql> alter table student add primary key(s_id);
Query OK, 0 rows affected (0.52 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| s_id   | int(11)     | NO   | PRI | NULL    |       |
| s_name | varchar(10) | NO   |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
```

#####                        4)删除信息

​                        **删除约束:**

​                             `alter  table  表名  drop  column  字段名  /  约束名`

​                        **删除主键**

​                             `alter  table  表名 drop  primary key;`

```
mysql> alter table student drop column s_id;
Query OK, 0 rows affected (0.65 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    49
Current database: work

+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| s_name | varchar(10) | NO   |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
1 row in set (0.03 sec)

mysql>  alter table student add column s_id varchar(10) not null;

Query OK, 0 rows affected (0.51 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| s_name | varchar(10) | NO   |     | NULL    |       |
| s_id   | varchar(10) | NO   |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> alter table student add primary key(s_id);
Query OK, 0 rows affected (0.37 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| s_name | varchar(10) | NO   |     | NULL    |       |
| s_id   | varchar(10) | NO   | PRI | NULL    |       |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> alter table student drop primary key;
Query OK, 0 rows affected (0.53 sec)
Records: 0  Duplicates: 0  Warnings: 0
```

​                        **删除外键**

​                              `alter  table  表名   drop  FOREIGN KEY  外键名字`

​                        **删除索引**

​                               `alter  table  表名  drop index  索引名`

​                        **删除字段**

​                                `alter   table  表名  drop  column  字段名`

#####                        5)修改字段信息

​                          `alter   table  表名  MODIFY【COLUMN】旧的列名  新类型(长度)  【FIRST | AFTER col_name】`

​                         `alter   table  表名  CHANGE【COLUMN】旧的列名 新的列名【类型】【约束】 column_definition 【FIRST | AFTER col_name】`

### 三:数据处理---DML

####          1:DML语言数据处理

​                 将数据添加到表格,从表格中移除,修改数据,从表格获取数据查看

####          2:创建数据库,创建表

####          3:表中添加数据的语法  要插入数据  关键字 insert

​                 **语法1:**

​                    `insert   into  表名(字段1,字段2,……)values(值1,值2,……)`

​                *备注:字段需要和值一一对应,个数和顺序对应,每插入一条数据就表示在表格中添加一行数据*

​                      *如果我们是给表中所有字段添加数据,表名后面的字段列表可以省略,默认的字段个数就是表中所有字段,顺序按照添加字段或者创建表的时候字段顺序*

​                       `insert  into  表名  values(值1,值2)`

​      例如

```
                insert into _stu(s_name,s_sex,s_birtime)

                             values('a12','misliu','男','2023-02-02');
```

​                  **语法2:**

​                      `insert   into  表名(字段1,字段2,……)values(值1,值2,……),(值1,值2,……),(值1,值2,……)`

​                  **语法3:**

​                      `insert  into  表名(字段1,字段2)select  字段1,字段2,…from 存在的表名`

​                  例如:insert  into ——stu(s_num,s_name,s_sex,s_birtime)select  st_id,st_name,st_se

####          4:删除数据  delete

​                      `delete  from 表名【where 条件】`

​                 *备注:如果不加【where 条件】表示删除所有数据*

​                   截断表:

​                       `truncate  table  表名;`

​            **delete 和 truncate的区别**

​                 delete只是删除的表中的数据。表中的一些特殊设置结构没有改变,比如自增

​                 truncate,截断表,不仅仅是删除数据,相当于将表删除,再重新创建一个新的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值