MySQL--插入、更新与删除数据
创始人
2024-11-15 10:06:50
0

前言:本博客仅作记录学习使用,部分图片出自网络,如有侵犯您的权益,请联系删除

一、插入数据

1、为表的所有字段插入数据

使用基本的INSERT语句插入数据要求指定表名称和插入到新记录中的值,其语法:

 insert into table_name (column_list) values (value_list);

为演示示例,我们创建表:

 create table person(  id      int unsigned not null auto_increment,  name    char(40) not null default '',  age     int not null default 0,  info    char(50) null,  primary key (id)  );

像表中插入值的方法有两种:一种是指定所有字段名,另一种是完全不指定字段名

 # 在person表中,插入一条新记录,id值为1,name值为Green,age值为21,info值为Lawyer  insert into person (id,name,age,info)  values (1,'Green',21,'Lawyer');  ​  -------# insert语句后面的列顺序可以不是person表定义时的顺序;需要保证值的顺序与列字段的顺序相同  # 插入一条新记录,id为2,name为Suse,age为22,indo为dancer  insert into person (age,name,id,info)  values (22,'Suse',2,'dancer');

使用INSERT插入数据时,允许列名称列表column_list为空,此时,值列表中需要为表的每一个字段指定值,并且值的顺序必须和数据表中字段定义时的顺序相同

 # 在person表中,插入一条记录,id值为3,name值为Marry,age值为24,info值为Musician  insert into person values (3,'Marry',24,'Musician');

2、为表的指定字段插入数据

为表的指定字段插入数据,就是在INSERT语句中只向部分字段中插入值,而其他字段的值为表定义时的默认值

 # 在person表中,插入一条:name为Willam,age为20,info为sports man;  insert into person (name,age,info)  values ('Willam',20,'sports man');  ​  ------# 未定义的部分使用默认值  # 在person表中,插入一条:name为Laura,age为25  insert into person (name,age) values ('Laura',25);

3、同时插入多条记录

INSERT语句可以同时向数据表中插入多条记录,插入时指定多个值列表每个值列表之间用逗号分隔开

 insert into table_name (column_list)  values (value_list1),(value_list2),...,(value_listn);   # 在person表中,插入一条:在name、age和info字段指定插入值,同时插入3条新记录  insert into person (name,age,info)  values ('Evans',27,'secretary'),  ('Dale',22,'cook'),  ('Edison',28,'singer');  ​  # 在person表中,不指定插入列表,同时插入2条记录  insert into person  values (9,'Harry',21,'magician'),  (null,'Harriet',19,'pianist');

4、将查询结果插入到表中

INSERT语句用来给数据表插入记录时指定插入记录的列值。INSERT还可以将SELECT语句查询的结果插入到表中,如果想要从另外一个表中合并个人信息到person 表,不需要把每一条记录的值一个一个输入,只需要使用一条INSERT语句和一条SELECT语句组成的组合语句即可快速地从一个或多个表中向一个表中插入多行。基本语法格式如下:

 insert into table_name1 (column_list)  select (column_list2) from table_name2 where (condition)   # 从person_old表中查询所有的记录,并将其插入到person表中  mysql> create table person_old(  id      int unsigned not null auto_increment,  name    char(40) not null default '',  age     int not null default 0,  info    char(50) null,  primary key (id)  );  mysql> insert into person_old values (11,'Harry',20,'student'),(12,'Beckham',31,'police');  mysql> insert into person(id,name,age,info) select id,name,age,info from person_old;

二、更新数据

MySQL中使用 UPDATE 语句更新表中的记录,可以更新特定的行或者同时更新所有的行

 update table_name   set column_name1 = value1,column_name2=value2,...,column_namen=valuen  where (condition);   # 在person表中,更新id值为11的记录,将age字段值改为15,将name字段值改为LiMing  update person set age=15,name='LiMing' where id=1;  ​  # 在person表中,更新age值为19-22的记录,将info字段值都改为student  update person set info='student' where age between 19 and 22;

三、删除数据

从数据表中删除数据使用 DELETE 语句DELETE语允许WHERE子指定删除条件。DELETE语句基本语法格式如下:

 delete from table_name [where ];   # 在person表中,删除id等于11的记录  delete from person where id=11;  ​  # 在person表中,使用delete语句同时删除多条记录,在前面的update语句中将age字段值在19-22之间的记录的info字段值修改为student,删除这些记录  delete from person where age between 19 and 22;  ​  ------# 删除person表中所有记录  delete from person;

四、为表增加计算列

计算列就是某一列的值是通过别的列计算得来的,其语法格式如下:

 col_name date_type [GENERATED ALWAYS] AS (expression)  [VIRTUAL | STORED] [UNIQUE [KEY]] [COMMENT comment]  [NOT NULL | NULL] [[PRIMARY] KEY]   # 定义数据表tb1,字段id、a、b和c,其中c为计算列,用于计算a+b的值  create table tb1(  id    int(9)     not null auto_increment,  a     int(9)     default null,  b     int(9)     default null,  c     int(9)     generated always as ((a+b)) virtual,  primary key (id)  );  insert into tb1(a,b) values (100,200);  mysql> select * from tb1;  +----+------+------+------+  | id | a    | b    | c    |  +----+------+------+------+  |  1 |  100 |  200 |  300 |  +----+------+------+------+  # 更新数据发现c列值改变  update tb1 set a=500;  select * from tb1;  +----+------+------+------+  | id | a    | b    | c    |  +----+------+------+------+  |  1 |  500 |  200 |  700 |  +----+------+------+------+

致谢

在此,我要对所有为知识共享做出贡献的个人和机构表示最深切的感谢。同时也感谢每一位花时间阅读这篇文章的读者,如果文章中有任何错误,欢迎留言指正。 

学习永无止境,让我们共同进步!!

相关内容

热门资讯

科技介绍!德扑手牌胜率计算工具... 科技介绍!德扑手牌胜率计算工具(WPK私人房)外挂透明挂辅助工具(2024已更新)(哔哩哔哩)是一款...
总算了解!wpk德州(wepo... 总算了解!wpk德州(wepokerplus)外挂透明挂辅助工具!(透视)有挂透明(2024已更新)...
一分钟揭秘!((扑克时间))外... 一分钟揭秘!((扑克时间))外挂透明挂辅助安装!(透视)有挂助手(2024已更新)(哔哩哔哩);1、...
玩家实测!乐乐上海麻将三打一有... 玩家实测!乐乐上海麻将三打一有挂的!太坑了原来确实是有挂(2025已更新)(哔哩哔哩);人气非常高,...
记者揭秘!((悠悠众娱))外挂... 记者揭秘!((悠悠众娱))外挂辅助插件(透视)辅助实锤(2020已更新)(哔哩哔哩);1.悠悠众娱 ...
1秒钟猫腻!赣牌圈小程序开挂(... 1秒钟猫腻!赣牌圈小程序开挂(辅助挂)2025已更新辅助安装(哔哩哔哩);1、超多福利:超高返利,海...
重大科普!((wpk网页版))... 重大科普!((wpk网页版))外挂透明挂辅助工具!(透视)有挂必备(2024已更新)(哔哩哔哩);人...
六分钟了解!微友有辅助挂是真的... 六分钟了解!微友有辅助挂是真的!太坑了原来真的是有挂(2023已更新)(哔哩哔哩)六分钟了解!微友有...
分享一款!半岛棋牌(Wepok... 大家肯定在之前半岛棋牌或者半岛棋牌中玩过分享一款!半岛棋牌(Wepoke透视)外挂透明挂辅助机制!(...
查到实测!((创宇经济))外挂... 您好,创宇经济这款游戏可以开挂的,确实是有挂的,需要了解加微【439369440】很多玩家在这款游戏...