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 |  +----+------+------+------+

致谢

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

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

相关内容

热门资讯

一分钟揭秘辅助!微乐小程序游戏... 一分钟揭秘辅助!微乐小程序游戏破解器,微乐家乡小程序修改器-一直真的有辅助挂1)微乐小程序游戏破解器...
技术分享开挂!微信小程序微乐辅... 技术分享开挂!微信小程序微乐辅助器,微乐贵阳捉鸡麻将能不能开挂-一贯是真的有辅助工具在进入微信小程序...
科技介绍开挂!微乐小程序家乡自... 科技介绍开挂!微乐小程序家乡自建房辅助app,微乐自建房脚本如何下载-其实是有辅助神器1、超多福利:...
重大通报辅助!微信小程序微乐辅... 重大通报辅助!微信小程序微乐辅助器教程,微乐小程序微乐家乡辅助器-都是真的有辅助插件重大通报辅助!微...
玩家必备教程辅助!微信小程序游... 玩家必备教程辅助!微信小程序游戏破解微乐游戏,微信小程序微乐安徽辅助苹果-确实有辅助脚本1、完成微信...
科技分享开挂!微乐手游辅助脚本... 科技分享开挂!微乐手游辅助脚本平台,微乐小程序辅助器免费-其实有辅助神器一、微乐手游辅助脚本平台可以...
发现玩家辅助!微乐小程序辅助工... 您好,微乐小程序辅助工具这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多...
推荐一款透视挂!微乐小程序家乡... 推荐一款透视挂!微乐小程序家乡自建房辅助app,微乐河南小程序微乐辅助脚本-原来存在有辅助挂微乐小程...
一起来探讨开挂!微乐小程序开挂... 一起来探讨开挂!微乐小程序开挂黑科技,微乐自建房免费黑科技推荐-本来真的是有辅助脚本小薇(辅助器软件...
2024教程辅助!微信小程序微... 2024教程辅助!微信小程序微乐辅助免费,微乐小程序辅助教程-都是是真的有辅助挂1、点击下载安装,微...