欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MySql数据库的学习(基础篇四)

程序员文章站 2022-05-27 23:44:59
...

概述

上篇,我们简单的学习了一下如何修改数据表的约束和修改数据表的字段数量,今天,我们接着上篇的内容来学习。

学习

修改列定义

语法 ALTER TABLE tbl_name MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]

实际使用

改变列的位置
mysql> show columns from tab1;
+----------+---------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| username | varchar(20)         | YES  |     | NULL    |       |
| age      | tinyint(3) unsigned | YES  |     | NULL    |       |
| salary   | float(8,2) unsigned | YES  |     | NULL    |       |
| _id      | int(10) unsigned    | NO   | PRI | 0       |       |
+----------+---------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table tab1 modify _id int unsigned not null first;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from tab1;
+----------+---------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| _id      | int(10) unsigned    | NO   | PRI | NULL    |       |
| username | varchar(20)         | YES  |     | NULL    |       |
| age      | tinyint(3) unsigned | YES  |     | NULL    |       |
| salary   | float(8,2) unsigned | YES  |     | NULL    |       |
+----------+---------------------+------+-----+---------+-------+
4 rows in set (0.02 sec)
mysql> alter table tab1 modify _id int unsigned not null after username;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from tab1;
+----------+---------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| username | varchar(20)         | YES  |     | NULL    |       |
| _id      | int(10) unsigned    | NO   | PRI | NULL    |       |
| age      | tinyint(3) unsigned | YES  |     | NULL    |       |
| salary   | float(8,2) unsigned | YES  |     | NULL    |       |
+----------+---------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
改变数据类型
mysql> show columns from tab1;
+----------+---------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| username | varchar(20)         | YES  |     | NULL    |       |
| _id      | int(10) unsigned    | NO   | PRI | NULL    |       |
| age      | tinyint(3) unsigned | YES  |     | NULL    |       |
| salary   | float(8,2) unsigned | YES  |     | NULL    |       |
+----------+---------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table tab1 modify age int unsigned not null after _id;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from tab1;
+----------+---------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| username | varchar(20)         | YES  |     | NULL    |       |
| _id      | int(10) unsigned    | NO   | PRI | NULL    |       |
| age      | int(10) unsigned    | NO   |     | NULL    |       |
| salary   | float(8,2) unsigned | YES  |     | NULL    |       |
+----------+---------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

需要注意的是:大数据改成小数据时可能会丢失数据

修改列名称(包含以上功能)

语法 ALTER TABLE tbl_name CHANGE [COLUMNS] old_col_name new_col_name column_definition [FIRST | AFTER col_name]

mysql> alter table tab1 change age _age int unsigned not null after _id;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from tab1;
+----------+---------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| username | varchar(20)         | YES  |     | NULL    |       |
| _id      | int(10) unsigned    | NO   | PRI | NULL    |       |
| _age     | int(10) unsigned    | NO   |     | NULL    |       |
| salary   | float(8,2) unsigned | YES  |     | NULL    |       |
+----------+---------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
修改数据表的名称

修改数据表的名称有两个方法:

1、ALTER TABLE tbl_name RENAME [TO | AS] new_tbl_name;

2、RENAME TABLE tbl_name TO new_tbl_name [,tbl_name2 TO new_tbl_name2] …

mysql> alter table tab3 rename to tab_3;
Query OK, 0 rows affected (0.06 sec)

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

这里,我有个疑问,如果有外键约束,那么修改表名会有影响吗?我们来实现下

mysql> show create table tab2\G;
*************************** 1. row ***************************
       Table: tab2
Create Table: CREATE TABLE `tab2` (
  `username` varchar(20) NOT NULL,
  `age` tinyint(3) unsigned NOT NULL,
  `salary` float(8,2) unsigned NOT NULL,
  `id` int(10) unsigned DEFAULT NULL,
  KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> alter table tab2 add foreign key(id) references tab_1(_id);
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table tab_1 rename to tab1;
Query OK, 0 rows affected (0.02 sec)

mysql> show create table tab2\G;
*************************** 1. row ***************************
       Table: tab2
Create Table: CREATE TABLE `tab2` (
  `username` varchar(20) NOT NULL,
  `age` tinyint(3) unsigned NOT NULL,
  `salary` float(8,2) unsigned NOT NULL,
  `id` int(10) unsigned DEFAULT NULL,
  KEY `id` (`id`),
  CONSTRAINT `tab2_ibfk_1` FOREIGN KEY (`id`) REFERENCES `tab1` (`_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

ERROR:
No query specified

从代码中可以看出,随着我们的修改,子表中也随之修改了

那么学完了对数据表的一些操作之后,我们把目光放到数据库学习的重心:数据记录的操作

记录操作

插入操作

语法 INSERT [INTO] tbl_name [(col_name,…)] {VALUES | VALUE} ({expr | DEFAULT},…),(…),…;

mysql> insert into students value(null,'Tom','男',22,'13177020905','湖北荆州',2)
;
Query OK, 1 row affected (0.00 sec)

mysql> insert into students value(null,'Jack','女',20,'13171230905','湖北监利',3
),(null,'Lucy','女',22,'13177020905','湖北荆州',2);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from students;
+-----+------+-----+-----+-------------+----------+------+
| _id | name | sex | age | phone       | address  | pid  |
+-----+------+-----+-----+-------------+----------+------+
|   4 | 老王 | 男  |  18 | 13131313133 | 湖南长沙 | NULL |
|   6 | Tom  | 男  |  22 | 13177020905 | 湖北荆州 |    2 |
|   7 | Jack | 女  |  20 | 13171230905 | 湖北监利 |    3 |
|   8 | Lucy | 女  |  22 | 13177020905 | 湖北荆州 |    2 |
+-----+------+-----+-----+-------------+----------+------+
4 rows in set (0.00 sec)

语法 INSERT [INTO] tbl_name SET col_name={expr | DEFAULT} , …

mysql> insert into students set name = 'Ben',sex='女',age=25,phone='13872425061'
,address='湖南岳阳',pid = 3;
Query OK, 1 row affected (0.00 sec)

mysql> select * from students;
+-----+------+-----+-----+-------------+----------+------+
| _id | name | sex | age | phone       | address  | pid  |
+-----+------+-----+-----+-------------+----------+------+
|   4 | 老王 | 男  |  18 | 13131313133 | 湖南长沙 | NULL |
|   6 | Tom  | 男  |  22 | 13177020905 | 湖北荆州 |    2 |
|   7 | Jack | 女  |  20 | 13171230905 | 湖北监利 |    3 |
|   8 | Lucy | 女  |  22 | 13177020905 | 湖北荆州 |    2 |
|   9 | Ben  | 女  |  25 | 13872425061 | 湖南岳阳 |    3 |
+-----+------+-----+-----+-------------+----------+------+
5 rows in set (0.00 sec)

语法 INSERT [INTO] tbl_name [(col_name,…)] SELECT …

此方法可以将查询结果插入到指定数据表

mysql> insert into result(username) select name from students;
Query OK, 5 rows affected (0.06 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from result;
+----------+
| username |
+----------+
| 老王     |
| Tom      |
| Jack     |
| Lucy     |
| Ben      |
+----------+
5 rows in set (0.00 sec)
更新记录

语法 UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET col_name1 = {expr | DEFAULT},[col_name2 = {expr | DEFAULT} … [WHERE where_condition]

不加where他条件时,全部更新

mysql> select * from students;
+-----+------+-----+-----+-------------+----------+------+
| _id | name | sex | age | phone       | address  | pid  |
+-----+------+-----+-----+-------------+----------+------+
|   4 | 老王 | 男  |  18 | 13131313133 | 湖南长沙 | NULL |
|   6 | Tom  | 男  |  22 | 13177020905 | 湖北荆州 |    2 |
|   7 | Jack | 女  |  20 | 13171230905 | 湖北监利 |    3 |
|   8 | Lucy | 女  |  22 | 13177020905 | 湖北荆州 |    2 |
|   9 | Ben  | 女  |  25 | 13872425061 | 湖南岳阳 |    3 |
+-----+------+-----+-----+-------------+----------+------+
5 rows in set (0.09 sec)

mysql> update students set age = age + 2;
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5  Changed: 5  Warnings: 0

mysql> select * from students;
+-----+------+-----+-----+-------------+----------+------+
| _id | name | sex | age | phone       | address  | pid  |
+-----+------+-----+-----+-------------+----------+------+
|   4 | 老王 | 男  |  20 | 13131313133 | 湖南长沙 | NULL |
|   6 | Tom  | 男  |  24 | 13177020905 | 湖北荆州 |    2 |
|   7 | Jack | 女  |  22 | 13171230905 | 湖北监利 |    3 |
|   8 | Lucy | 女  |  24 | 13177020905 | 湖北荆州 |    2 |
|   9 | Ben  | 女  |  27 | 13872425061 | 湖南岳阳 |    3 |
+-----+------+-----+-----+-------------+----------+------+
5 rows in set (0.00 sec)

条件可以自己指定

mysql> update students set age = age + 10 where _id%2 = 0;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from students;
+-----+------+-----+-----+-------------+----------+------+
| _id | name | sex | age | phone       | address  | pid  |
+-----+------+-----+-----+-------------+----------+------+
|   4 | 老王 | 男  |  30 | 13131313133 | 湖南长沙 | NULL |
|   6 | Tom  | 男  |  34 | 13177020905 | 湖北荆州 |    2 |
|   7 | Jack | 女  |  22 | 13171230905 | 湖北监利 |    3 |
|   8 | Lucy | 女  |  34 | 13177020905 | 湖北荆州 |    2 |
|   9 | Ben  | 女  |  27 | 13872425061 | 湖南岳阳 |    3 |
+-----+------+-----+-----+-------------+----------+------+
5 rows in set (0.00 sec)
删除记录

语法 DELETE FROM tbl_name [WHERE where_condition]

mysql> delete from students where _id = 4;
Query OK, 1 row affected (0.05 sec)

mysql> select * from students;
+-----+------+-----+-----+-------------+----------+------+
| _id | name | sex | age | phone       | address  | pid  |
+-----+------+-----+-----+-------------+----------+------+
|   6 | Tom  | 男  |  34 | 13177020905 | 湖北荆州 |    2 |
|   7 | Jack | 女  |  22 | 13171230905 | 湖北监利 |    3 |
|   8 | Lucy | 女  |  34 | 13177020905 | 湖北荆州 |    2 |
|   9 | Ben  | 女  |  27 | 13872425061 | 湖南岳阳 |    3 |
+-----+------+-----+-----+-------------+----------+------+
4 rows in set (0.00 sec)

基础四到此,转到基础五

欢迎大家关注我的公众号

MySql数据库的学习(基础篇四)