2023年8月3日发(作者:)
mysql更新语句sql_SQL更新语句说明:更新表的查询(包括MySQL⽰例)mysql更新语句sql更新查询可以做什么 (What an Update query can do)An update query gives the DBA or SQL-using programmer the ability to update many records with one command.更新查询使DBA或使⽤SQL的程序员能够使⽤⼀个命令更新许多记录。Important Safety Tip: always have a backup copy of what you are about to change BEFORE you change it.重要安全提⽰:更改之前,请始终保留要更改内容的备份副本。This guide will:本指南将:add a new field to the student table向学⽣表添加新字段test the logic to update that field with a school assigned email address测试⽤学校分配的电⼦邮件地址更新该字段的逻辑update the new field.更新新字段。Here is the student table as we start this process这是我们开始此过程时的学⽣表SELECT * FROM student;+-----------+------------------------+-----------+------------------+---------------------+---------------------+| studentID | FullName | sat_score | programOfStudy | rcd_Created | rcd_Updated |+-----------+------------------------+-----------+------------------+---------------------+---------------------+| 1 | Monique Davis | 400 | Literature | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 || 2 | Teri Gutierrez | 800 | Programming | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 || 3 | Spencer Pautier | 1000 | Programming | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 || 4 | Louis Ramsey | 1200 | Programming | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 || 5 | Alvin Greene | 1200 | Programming | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 || 6 | Sophie Freeman | 1200 | Programming | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 || 7 | Edgar Frank "Ted" Codd | 2400 | Computer Science | 2017-08-16 15:35:33 | 2017-08-16 15:35:33 || 8 | Donald D. Chamberlin | 2400 | Computer Science | 2017-08-16 15:35:33 | 2017-08-16 15:35:33 || 9 | Raymond F. Boyce | 2400 | Computer Science | 2017-08-16 15:35:33 | 2017-08-16 15:35:33 |+-----------+------------------------+-----------+------------------+---------------------+---------------------+9 rows in set (0.00 sec)更改表并添加新字段 (Alter the table and add a new field)ALTER TABLE `fcc_sql_guides_database`.`student`
ADD COLUMN `schoolEmailAdr` VARCHAR(125) NULL AFTER `programOfStudy`;The student table after the alter is executed.执⾏更改后的学⽣表。mysql> SELECT FullName, sat_score, programOfStudy, schoolEmailAdr FROM student;+------------------------+-----------+------------------+----------------+| FullName | sat_score | programOfStudy | schoolEmailAdr |+------------------------+-----------+------------------+----------------+| Monique Davis | 400 | Literature | NULL || Teri Gutierrez | 800 | Programming | NULL || Spencer Pautier | 1000 | Programming | NULL || Louis Ramsey | 1200 | Programming | NULL || Alvin Greene | 1200 | Programming | NULL || Sophie Freeman | 1200 | Programming | NULL || Edgar Frank "Ted" Codd | 2400 | Computer Science | NULL || Donald D. Chamberlin | 2400 | Computer Science | NULL || Raymond F. Boyce | 2400 | Computer Science | NULL |+------------------------+-----------+------------------+----------------+9 rows in set (0.00 sec)测试逻辑(⾮常重要的⼀步!) (TESTING the logic (VERY important step!))SELECT FullName, instr(FullName," ") AS firstSpacePosition,
concat(substring(FullName,1,instr(FullName," ")-1),"@") AS schoolEmailFROM student;+------------------------+--------------------+------------------------+| FullName | firstSpacePosition | schoolEmail |+------------------------+--------------------+------------------------+| Monique Davis | 8 | Monique@ || Teri Gutierrez | 5 | Teri@ || Spencer Pautier | 8 | Spencer@ || Louis Ramsey | 6 | Louis@ || Alvin Greene | 6 | Alvin@ || Sophie Freeman | 7 | Sophie@ || Edgar Frank "Ted" Codd | 6 | Edgar@ || Donald D. Chamberlin | 7 | Donald@ || Raymond F. Boyce | 8 | Raymond@ |+------------------------+--------------------+------------------------+9 rows in set (0.00 sec)A note about concat(): in MySQL this command is used to combined strings, not so in other SQL versions (check yourmanual). In this usage it works like this: The substring of the FullName field up to but not including the first space iscombined with “@”. In the real world this would HAVE TO be much more complex and you would need toensure that the email address is unique.关于concat()的注释:在MySQL中,此命令⽤于组合字符串,⽽在其他SQL版本中则不是这样(请查看⼿册)。
在这种⽤法中,它的⼯作⽅式如下:FullName字段的⼦字符串,直到但不包括第⼀个空格,都与“ @ ”组合在⼀起。
在现实世界中,这将变得更加复杂,并且您需要确保电⼦邮件地址是唯⼀的。进⾏更新 (Doing the update)We’ll pretend that this is what we want and update the table with this information:我们将假装这就是我们想要的,并使⽤以下信息更新表:UPDATE student SET schoolEmailAdr = concat(substring(FullName,1,instr(FullName," ")-1),"@")WHERE schoolEmailAdr is NULL;Success!成功!mysql> SELECT FullName, sat_score, programOfStudy, schoolEmailAdr FROM student;+------------------------+-----------+------------------+------------------------+| FullName | sat_score | programOfStudy | schoolEmailAdr |+------------------------+-----------+------------------+------------------------+| Monique Davis | 400 | Literature | Monique@ || Teri Gutierrez | 800 | Programming | Teri@ || Spencer Pautier | 1000 | Programming | Spencer@ || Louis Ramsey | 1200 | Programming | Louis@ || Alvin Greene | 1200 | Programming | Alvin@ || Sophie Freeman | 1200 | Programming | Sophie@ || Edgar Frank "Ted" Codd | 2400 | Computer Science | Edgar@ || Donald D. Chamberlin | 2400 | Computer Science | Donald@ || Raymond F. Boyce | 2400 | Computer Science | Raymond@ |+------------------------+-----------+------------------+------------------------+9 rows in set (0.00 sec)As with all of these SQL things there is MUCH MORE to them than what’s in this introductory guide.与所有这些SQL事物⼀样,它们⽐本⼊门指南中的内容要多得多。I hope this at least gives you enough to get started.我希望这⾄少能给您⾜够的⼊门。Please see the manual for your database manager and have fun trying different options yourself.请参阅数据库管理员的⼿册,并尝试⾃⼰尝试其他选项,这很有趣。您如何使⽤Update语句? (How do you use an Update statement?)To update a record in a table you use the UPDATE statement.要更新表中的记录,请使⽤UPDATE语句。Be careful. You can update all records of the table or just a few. Use the WHERE condition to specify which records do youwant to update. It is possible to update one or more columns at a time. The syntax is:⼩⼼。 您可以更新表中的所有记录或仅更新其中的⼀些记录。 使⽤WHERE条件指定要更新的记录。 可以⼀次更新⼀个或多个列。 语法为:UPDATE table_nameSET column1 = value1,
column2 = value2, ...WHERE condition;Here is an example updating the Name of the record with Id 4:这是⼀个使⽤ID 4更新记录名称的⽰例:UPDATE PersonSET Name = “Elton John”WHERE Id = 4;You can also update columns in a table by using values from other tables. Use JOIN clause to get data from multiple syntax is:您还可以通过使⽤其他表中的值来更新表中的列。 使⽤JOIN⼦句从多个表中获取数据。 语法为:UPDATE table_name1SET table_1 = table_A table_2 = table_BFROM table_name1JOIN table_name2 ON table_nKey = table_e is an example updating Manager of all records:这是所有记录的更新管理器的⽰例:UPDATE PersonSET r = rFROM PersonJOIN Department ON mentID = l更新语句sql
2023年8月3日发(作者:)
mysql更新语句sql_SQL更新语句说明:更新表的查询(包括MySQL⽰例)mysql更新语句sql更新查询可以做什么 (What an Update query can do)An update query gives the DBA or SQL-using programmer the ability to update many records with one command.更新查询使DBA或使⽤SQL的程序员能够使⽤⼀个命令更新许多记录。Important Safety Tip: always have a backup copy of what you are about to change BEFORE you change it.重要安全提⽰:更改之前,请始终保留要更改内容的备份副本。This guide will:本指南将:add a new field to the student table向学⽣表添加新字段test the logic to update that field with a school assigned email address测试⽤学校分配的电⼦邮件地址更新该字段的逻辑update the new field.更新新字段。Here is the student table as we start this process这是我们开始此过程时的学⽣表SELECT * FROM student;+-----------+------------------------+-----------+------------------+---------------------+---------------------+| studentID | FullName | sat_score | programOfStudy | rcd_Created | rcd_Updated |+-----------+------------------------+-----------+------------------+---------------------+---------------------+| 1 | Monique Davis | 400 | Literature | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 || 2 | Teri Gutierrez | 800 | Programming | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 || 3 | Spencer Pautier | 1000 | Programming | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 || 4 | Louis Ramsey | 1200 | Programming | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 || 5 | Alvin Greene | 1200 | Programming | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 || 6 | Sophie Freeman | 1200 | Programming | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 || 7 | Edgar Frank "Ted" Codd | 2400 | Computer Science | 2017-08-16 15:35:33 | 2017-08-16 15:35:33 || 8 | Donald D. Chamberlin | 2400 | Computer Science | 2017-08-16 15:35:33 | 2017-08-16 15:35:33 || 9 | Raymond F. Boyce | 2400 | Computer Science | 2017-08-16 15:35:33 | 2017-08-16 15:35:33 |+-----------+------------------------+-----------+------------------+---------------------+---------------------+9 rows in set (0.00 sec)更改表并添加新字段 (Alter the table and add a new field)ALTER TABLE `fcc_sql_guides_database`.`student`
ADD COLUMN `schoolEmailAdr` VARCHAR(125) NULL AFTER `programOfStudy`;The student table after the alter is executed.执⾏更改后的学⽣表。mysql> SELECT FullName, sat_score, programOfStudy, schoolEmailAdr FROM student;+------------------------+-----------+------------------+----------------+| FullName | sat_score | programOfStudy | schoolEmailAdr |+------------------------+-----------+------------------+----------------+| Monique Davis | 400 | Literature | NULL || Teri Gutierrez | 800 | Programming | NULL || Spencer Pautier | 1000 | Programming | NULL || Louis Ramsey | 1200 | Programming | NULL || Alvin Greene | 1200 | Programming | NULL || Sophie Freeman | 1200 | Programming | NULL || Edgar Frank "Ted" Codd | 2400 | Computer Science | NULL || Donald D. Chamberlin | 2400 | Computer Science | NULL || Raymond F. Boyce | 2400 | Computer Science | NULL |+------------------------+-----------+------------------+----------------+9 rows in set (0.00 sec)测试逻辑(⾮常重要的⼀步!) (TESTING the logic (VERY important step!))SELECT FullName, instr(FullName," ") AS firstSpacePosition,
concat(substring(FullName,1,instr(FullName," ")-1),"@") AS schoolEmailFROM student;+------------------------+--------------------+------------------------+| FullName | firstSpacePosition | schoolEmail |+------------------------+--------------------+------------------------+| Monique Davis | 8 | Monique@ || Teri Gutierrez | 5 | Teri@ || Spencer Pautier | 8 | Spencer@ || Louis Ramsey | 6 | Louis@ || Alvin Greene | 6 | Alvin@ || Sophie Freeman | 7 | Sophie@ || Edgar Frank "Ted" Codd | 6 | Edgar@ || Donald D. Chamberlin | 7 | Donald@ || Raymond F. Boyce | 8 | Raymond@ |+------------------------+--------------------+------------------------+9 rows in set (0.00 sec)A note about concat(): in MySQL this command is used to combined strings, not so in other SQL versions (check yourmanual). In this usage it works like this: The substring of the FullName field up to but not including the first space iscombined with “@”. In the real world this would HAVE TO be much more complex and you would need toensure that the email address is unique.关于concat()的注释:在MySQL中,此命令⽤于组合字符串,⽽在其他SQL版本中则不是这样(请查看⼿册)。
在这种⽤法中,它的⼯作⽅式如下:FullName字段的⼦字符串,直到但不包括第⼀个空格,都与“ @ ”组合在⼀起。
在现实世界中,这将变得更加复杂,并且您需要确保电⼦邮件地址是唯⼀的。进⾏更新 (Doing the update)We’ll pretend that this is what we want and update the table with this information:我们将假装这就是我们想要的,并使⽤以下信息更新表:UPDATE student SET schoolEmailAdr = concat(substring(FullName,1,instr(FullName," ")-1),"@")WHERE schoolEmailAdr is NULL;Success!成功!mysql> SELECT FullName, sat_score, programOfStudy, schoolEmailAdr FROM student;+------------------------+-----------+------------------+------------------------+| FullName | sat_score | programOfStudy | schoolEmailAdr |+------------------------+-----------+------------------+------------------------+| Monique Davis | 400 | Literature | Monique@ || Teri Gutierrez | 800 | Programming | Teri@ || Spencer Pautier | 1000 | Programming | Spencer@ || Louis Ramsey | 1200 | Programming | Louis@ || Alvin Greene | 1200 | Programming | Alvin@ || Sophie Freeman | 1200 | Programming | Sophie@ || Edgar Frank "Ted" Codd | 2400 | Computer Science | Edgar@ || Donald D. Chamberlin | 2400 | Computer Science | Donald@ || Raymond F. Boyce | 2400 | Computer Science | Raymond@ |+------------------------+-----------+------------------+------------------------+9 rows in set (0.00 sec)As with all of these SQL things there is MUCH MORE to them than what’s in this introductory guide.与所有这些SQL事物⼀样,它们⽐本⼊门指南中的内容要多得多。I hope this at least gives you enough to get started.我希望这⾄少能给您⾜够的⼊门。Please see the manual for your database manager and have fun trying different options yourself.请参阅数据库管理员的⼿册,并尝试⾃⼰尝试其他选项,这很有趣。您如何使⽤Update语句? (How do you use an Update statement?)To update a record in a table you use the UPDATE statement.要更新表中的记录,请使⽤UPDATE语句。Be careful. You can update all records of the table or just a few. Use the WHERE condition to specify which records do youwant to update. It is possible to update one or more columns at a time. The syntax is:⼩⼼。 您可以更新表中的所有记录或仅更新其中的⼀些记录。 使⽤WHERE条件指定要更新的记录。 可以⼀次更新⼀个或多个列。 语法为:UPDATE table_nameSET column1 = value1,
column2 = value2, ...WHERE condition;Here is an example updating the Name of the record with Id 4:这是⼀个使⽤ID 4更新记录名称的⽰例:UPDATE PersonSET Name = “Elton John”WHERE Id = 4;You can also update columns in a table by using values from other tables. Use JOIN clause to get data from multiple syntax is:您还可以通过使⽤其他表中的值来更新表中的列。 使⽤JOIN⼦句从多个表中获取数据。 语法为:UPDATE table_name1SET table_1 = table_A table_2 = table_BFROM table_name1JOIN table_name2 ON table_nKey = table_e is an example updating Manager of all records:这是所有记录的更新管理器的⽰例:UPDATE PersonSET r = rFROM PersonJOIN Department ON mentID = l更新语句sql
发布评论