MySQL学习——操作数据库(增删改查)
Posted 葫芦七娃
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL学习——操作数据库(增删改查)相关的知识,希望对你有一定的参考价值。
修改表
修改表是指修改数据库中已经存在的表的定义,修改表比重新定义表简单,不需要重新加载数据,也不会影响正在进行的服务,mysql中通过ALTER TABLE语句来修改表。修改表包括修改表名,修改字段数据类型,修改字段名,增减字段,修改字段的排列位置,更改默认存储引擎和删除表的外键约束等。
1.修改表名:
ALTER TABLE 旧表名 RENAME 新表名;
将example0 表改名为user表:
mysql>
mysql> desc example0;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| sex | tinyint(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table example0 rename user;
Query OK, 0 rows affected (0.25 sec)
mysql> desc example0;
ERROR 1146 (42S02): Table \'text.example0\' doesn\'t exist
mysql> desc user;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| sex | tinyint(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql>
2,。修改字段数据类型:
ALTER TABLE 表名 MODIFY 属性名 数据类型;
修改user表中name字段的数据类型
mysql>
mysql> desc user;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| sex | tinyint(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table user modify name varchar(30);
Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| sex | tinyint(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql>
3.修改字段名:
ALTER TABLE 表名 CHANGE 旧属性名 新属性名 新数据类型;
1.只修改字段名:
将example1表中stu_name 字段改名为name,且不改变数据类型:
mysql>
mysql> desc example1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| stu_id | int(11) | NO | PRI | NULL | |
| stu_name | varchar(20) | YES | | NULL | |
| stu_sex | tinyint(1) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table example1 change stu_name name varchar(20);
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc example1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| stu_id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| stu_sex | tinyint(1) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql>
2.修改字段名和字段数据类型
将example1表中stu_sex字段改名为sex,且数据类型改为INT(2):
mysql>
mysql> desc example1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| stu_id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| stu_sex | tinyint(1) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table example1 stu_sex sex int(2);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'stu_sex sex int(2)\' at line 1
mysql> alter table example1 change stu_sex sex int(2);
Query OK, 0 rows affected (0.95 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc example1;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| stu_id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| sex | int(2) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql>
4.增加字段
ALTER TABLE 表名 ADD 属性名1 数据类型【完整约束条件】【first | after 属性名2】;
1.增加无完整性约束条件的字段:
将user表中增加一个没有完整性约束条件约束的phone字段
mysql>
mysql> desc user;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| sex | tinyint(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table user add phone varvhar(20);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'varvhar(20)\' at line 1
mysql> alter table user add phone varchar(20);
Query OK, 0 rows affected (0.68 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| sex | tinyint(1) | YES | | NULL | |
| phone | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
2.增加有完整性约束条件的字段:
在user表中增加一个有非空约束的age字段
mysql>
mysql> alter table user add age int(4) not null;
Query OK, 0 rows affected (1.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| sex | tinyint(1) | YES | | NULL | |
| phone | varchar(20) | YES | | NULL | |
| age | int(4) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql>
3.表的第一个位置增加字段:
在user表中第一个位置增加num字段,并设置num字段为主键
mysql>
mysql> alter table user add num int(8) primary key first;
Query OK, 0 rows affected (0.76 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| num | int(8) | NO | PRI | NULL | |
| id | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| sex | tinyint(1) | YES | | NULL | |
| phone | varchar(20) | YES | | NULL | |
| age | int(4) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
mysql>
4.表的指定位置之后增加字段
在user表的phone字段后加address字段,并设置address字段为非空:
mysql>
mysql> alter table user add address varchar(30) not null after phone;
Query OK, 0 rows affected (0.74 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| num | int(8) | NO | PRI | NULL | |
| id | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| sex | tinyint(1) | YES | | NULL | |
| phone | varchar(20) | YES | | NULL | |
| address | varchar(30) | NO | | NULL | |
| age | int(4) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
mysql>
5.删除字段:
ALTER TABLE 表名 DROP 属性名;
将user表中删除id字段:
mysql>
mysql> desc user;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| num | int(8) | NO | PRI | NULL | |
| id | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| sex | tinyint(1) | YES | | NULL | |
| phone | varchar(20) | YES | | NULL | |
| address | varchar(30) | NO | | NULL | |
| age | int(4) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
mysql> alter table user drop id;
Query OK, 0 rows affected (0.60 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| num | int(8) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| sex | tinyint(1) | YES | | NULL | |
| phone | varchar(20) | YES | | NULL | |
| address | varchar(30) | NO | | NULL | |
| age | int(4) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql>
6.修改字段的排列位置:
ALTER TABLE 表名 MODIFY 属性名1 数据类型 FIRST | AFTER 属性名2;
1.字段修改到第一个位置:
将user表中name字段修改为该表的第一个字段
mysql> desc user;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| num | int(8) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| sex | tinyint(1) | YES | | NULL | |
| phone | varchar(20) | YES | | NULL | |
| address | varchar(30) | NO | | NULL | |
| age | int(4) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> alter table user modify name varchar(30) first;
Query OK, 0 rows affected (0.73 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(30) | YES | | NULL | |
| num | int(8) | NO | PRI | NULL | |
| sex | tinyint(1) | YES | | NULL | |
| phone | varchar(20) | YES | | NULL | |
| address | varchar(30) | NO | | NULL | |
| age | int(4) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql>
2.字段修改到指定位置
将user表中sex字段修改到age字段之后;
mysql>
mysql> alter table user modify sex tinyint(1) after age;
Query OK, 0 rows affected (0.63 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(30) | YES | | NULL | |
| num | int(8) | NO | PRI | NULL | |
| phone | varchar(20) | YES | | NULL | |
| address | varchar(30) | NO | | NULL | |
| age | int(4) | NO | | NULL | |
| sex | tinyint(1) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql>
7.更改表的存储引擎:
ALTER TABLE 表名 ENGINE = 存储引擎名;
将user表的存储引擎改为MyISAM:
mysql>
mysql> show create table user\\G
*************************** 1. row ***************************
Table: user
Create Table: CREATE TABLE `user` (
`name` varchar(30) DEFAULT NULL,
`num` int(8) NOT NULL,
`phone` varchar(20) DEFAULT NULL,
`address` varchar(30) NOT NULL,
`age` int(4) NOT NULL,
`sex` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`num`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> alter table user engine=MyISAM;
Query OK, 0 rows affected (0.37 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table user\\G
*************************** 1. row ***************************
Table: user
Create Table: CREATE TABLE `user` (
`name` varchar(30) DEFAULT NULL,
`num` int(8) NOT NULL,
`phone` varchar(20) DEFAULT NULL,
`address` varchar(30) NOT NULL,
`age` int(4) NOT NULL,
`sex` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`num`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql>
8.删除表的外键约束:
ALTER TABLE 表名 DROP FOREIGN KEY 外键别名;
删除表example3的外键约束
alter table example3 drop foreign key c_fk;
删除表:
删除表是指删除数据库中已经存在的表,删除表时,会删除表中的所有数据,在删除表时要特别注意,mysql中通过DROP TABLE语句来删除表,由于创建表时可能存在外键约束,一些表成为了与之关联的表的父表,要删除这些表,情况比较复杂。
删除当前存在的example5表
DROP TABLE example5;
注:删除一个表时,表中的所有数据也会被删除,因此,在删除表的时候一定要慎重,做好先备份在删除。
删除被其他表关联的父表
在删除其他表关联的父表时,先删除关联外键,在删除表
示例测试:
mysql>
mysql>
mysql>
mysql> create database example;
Query OK, 1 row affected (0.00 sec)
mysql> use example;
Database changed
mysql>
mysql>
mysql> create table student(num int(10) not null unique primary key,
-> name varchar(20) not null,
-> sex varchar(4) not null,
-> birthday datetime,
-> address varchar(50)
-> );
Query OK, 0 rows affected (0.42 sec)
mysql> desc student;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| num | int(10) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| sex | varchar(4) | NO | | NULL | |
| birthday | datetime | YES | | NULL | |
| address | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> create table grade(id int(10) not null unique primary key auto_increment,
-> course varchar(10) not null,
-> s_num int(10) not null,
-> grade varchar(4),
-> CONSTRAINT grade_fk FOREIGN KEY(s_num)
-> REFERENCES student(num)
-> );
Query OK, 0 rows affected (1.03 sec)
mysql>
mysql>
mysql> show create table grade\\G
*************************** 1. row ***************************
Table: grade
Create Table: CREATE TABLE `grade` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`course` varchar(10) NOT NULL,
`s_num` int(10) NOT NULL,
`grade` varchar(4) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `grade_fk` (`s_num`),
CONSTRAINT `grade_fk` FOREIGN KEY (`s_num`) REFERENCES `student` (`num`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql>
mysql> alter table grade modify course varchar(20);
Query OK, 0 rows affected (1.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mysql> desc grade;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| course | varchar(20) | YES | | NULL | |
| s_num | int(10) | NO | MUL | NULL | |
| grade | varchar(4) | YES | | NULL | |
+--------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> alter table grade modify s_num int(10) after id;
Query OK, 0 rows affected (0.82 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc grade;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| s_num | int(10) | YES | MUL | NULL | |
| course | varchar(20) | YES | | NULL | |
| grade | varchar(4) | YES | | NULL | |
+--------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> alter table grade change grade score varchar(4);
Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mysql> desc grade;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| s_num | int(10) | YES | MUL | NULL | |
| course | varchar(20) | YES | | NULL | |
| score | varchar(4) | YES | | NULL | |
+--------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql>
mysql> alter table grade engine = MyISAM;
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
mysql> alter table grade drop foreign key grade_fk;
Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table grade engine = MyISAM;
Query OK, 0 rows affected (0.50 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mysql> show create table grade\\G
*************************** 1. row ***************************
Table: grade
Create Table: CREATE TABLE `grade` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`s_num` int(10) DEFAULT NULL,
`course` varchar(20) DEFAULT NULL,
`score` varchar(4) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `grade_fk` (`s_num`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql>
mysql> alter table grade engine=MyISAM;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mysql>
mysql> alter table student drop address;
Query OK, 0 rows affected (0.72 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table student add phone int(10);
Query OK, 0 rows affected (0.63 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| num | int(10) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| sex | varchar(4) | NO | | NULL | |
| birthday | datetime | YES | | NULL | |
| phone | int(10) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> alter table grade rename gradeinfo;
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql>
mysql> show tables;
+-------------------+
| Tables_in_example |
+-------------------+
| gradeinfo |
| student |
+-------------------+
2 rows in set (0.00 sec)
mysql> drop table studnet;
ERROR 1051 (42S02): Unknown table \'example.studnet\'
mysql> drop table student;
Query OK, 0 rows affected (0.25 sec)
mysql> show tables;
+-------------------+
| Tables_in_example |
+-------------------+
| gradeinfo |
+-------------------+
1 row in set (0.01 sec)
mysql>
以上是关于MySQL学习——操作数据库(增删改查)的主要内容,如果未能解决你的问题,请参考以下文章