mysql增改删总结

mysql增改删总结

按照 数据库——表—— 字段——值的框架分别总结mysql的增删改操作

新增

新增数据库:create database 数据库名称;

mysql> create database test;   --新增数据库test 
Query OK, 1 row affected (0.01 sec)

新增表: create table 表名称 ( 字段一 数据类型 [约束], 字段二 数据类似 [约束], ··· , 字段N 数据类型 [约束]);

mysql> create table student  -- 新增表 student
    -> (
    ->    id  int primary key,
    ->    name  varchar(20) not null
    -> );
Query OK, 0 rows affected (0.11 sec)

新增表中字段:alter table 表名称 add 新字段名称 数据类型 [约束] [位置] ;

mysql> alter table student add class varchar(20) default '一班' after name;  -- 新增字段class 排在字段name 后面
Query OK, 0 rows affected (0.14 sec)
Records: 0  Duplicates: 0  Warnings: 0

新增数据 :insert inro 表名称 values ( 值1, 值2 , ··· ) ;

mysql> insert into student values (101 ,"张三", "一班") ;  -- 新增一条数据
Query OK, 1 row affected (0.06 sec)

指定字段新增数据 :insert into 表名称(字段一名称, 字段二名称) values (值1 ,值2 );

mysql> insert into student(id, name) values (102 ,"李四");  -- 只添加 id ,name 
Query OK, 1 row affected (0.10 sec)

新增多个数据 : insert into 表名称 values ( 第一行值1 ,第一行值2) , (第二行值1 ,第二行值2), ···, ( 第N行值1, 第N行值2 );

mysql> insert into student(id, name) values ( 103, "王五"), (104, "赵六"), (105, "孙七");  
Query OK, 3 rows affected (0.07 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from student;
+-----+------+-------+
| id  | name | class |
+-----+------+-------+
| 101 | 张三 | 一班  |
| 102 | 李四 | 一班  |
| 103 | 王五 | 一班  |
| 104 | 赵六 | 一班  |
| 105 | 孙七 | 一班  |
+-----+------+-------+
5 rows in set (0.00 sec)

修改

修改数据库名称,数据库名称不可以随意修改 ,过程较麻烦, 如果右这个需求可以参考1

修改表名称:rename table 原表名称 to 新表名称

mysql> rename table student to students;   -- 将表名字改为students
Query OK, 0 rows affected (0.12 sec)

修改字段名称 :alter table 表名称 change 原字段名称 新字段名称 数据类型;

mysql> alter table students change column class stu_class varchar(20);  -- 将字段class 改为 stu_class
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

修改字段的数据类型: alter table 表名称 modify column 字段名称 新数据类型;

mysql> alter table students modify column stu_class varchar(10);  -- 修改字段stu_class 的数据类型为varcahr(10)
Query OK, 5 rows affected (0.07 sec)
Records: 5  Duplicates: 0  Warnings: 0

修改字段的约束: alter table 表名称 modify column 字段名称 字段类型 新约束 ;

mysql> alter table students modify column stu_class varchar(10) default '二班';   --修改字段的默认约束
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

修改值: update 表名称 set 字段 = 新值 where 主键条件;

mysql> update students set stu_class = '二班'  where id = 101 ;   -- 修改id为101的 班级为'二班'
Query OK, 1 row affected (0.06 sec)
Rows matched: 1  Changed: 1  Warnings: 0

推荐通过主键指定行,或者其他具有唯一约束的字段, 否则会讲满足条件的行全部更新;

mysql> update students set name = "张三" where stu_class ="一班";  --  将一班的同学名字全改为张三
Query OK, 4 rows affected (0.10 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> select * from students;
+-----+------+-----------+
| id  | name | stu_class |
+-----+------+-----------+
| 101 | 张三 | 二班      |
| 102 | 张三 | 一班      |
| 103 | 张三 | 一班      |
| 104 | 张三 | 一班      |
| 105 | 张三 | 一班      |
+-----+------+-----------+
5 rows in set (0.00 sec)

删除

删除某条数据: delete from 表名 where 条件 ;

mysql> delete from students where id = 101;  --  删除id 为 101 的行
Query OK, 1 row affected (0.09 sec)

删除字段 : alter table 表名称 drop column 字段名称 ;

mysql> alter table students drop column stu_class;    -- 删除表中字段stu-class
Query OK, 0 rows affected (0.22 sec)

删除表 : drop table 表名称 ;

mysql> drop table students;   -- 删除students 表
Query OK, 0 rows affected (0.11 sec)

删除数据库: drop database 数据库名称;

mysql> drop database test;   --删除test数据库
Query OK, 0 rows affected (0.10 sec)

感谢您在茫茫的网络世界中阅读了本文, 希望没有浪费您宝贵的时间,期待您指出文中的不足!


  1. 数据库改名: https://blog.csdn.net/zyz511919766/article/details/49335897?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.compare& ↩︎

猜你喜欢

转载自blog.csdn.net/weixin_43705953/article/details/107189838