2023年8月3日发(作者:)

Mysql8.0学习笔记(3)——insertinto,set,delete⽂章⽬录简介在本章前先了解下table的属性,还是以customers为例,点击表旁边的扳⼿获得如下图对于Datatype :INT :只⽀持数字形式,例如“1234”VARCHAR(50):该类型为字符串格式,空间为变长,最长为50CHAR(2):字符串格式,空间固定为2,不⾜补空格DATE:⽇期格式 例如"2020-04-16"PK:表⽰为主键NN:表⽰不为空,即在这张表⾥勾选中的不能为空,其他可选AI:⾃动增量属性,⼀般以主键为准,根据插⼊数据使主键递增Default/Expression:没输⼊值的默认值insert into插⼊⼀⾏数据use sql_store;insert into customers//因为主键设置AI,默认加1,其他没填写的使⽤默认值value(default,"lj",'jiawei','1993-09-22',null,

'adress','city','ca',default)//也可以指定插⼊哪些值,顺序只要保证对应⼀致就⾏insert into customers(first_name, last_name, birth_date, address,city,state)value("lj",'jiawei','1993-09-22',

'adress','city','ca')插⼊多⾏数据使⽤’,'隔开就⾏insert into customers(first_name, last_name, birth_date, address,city,state)value("lj",'jiawei','1993-09-22',

'adress','city','ca'), ("lj2",'jiawei2','1993-09-22',

'adress','city','ca'),将数据插⼊分层表中对于⼀个table,可能有⼦表来表⽰其其余的信息,例如订单orders,有⼦表order_items其中order中的customer_id,order——date,status为NN;order_items中的product_id,quantity,unit_price也为NN;order_id都为两个表的主键在插⼊orders的时候同时也要插⼊order_itemsinsert into orders(customer_id,order_date,status)values(1,'2019-01-02',1);insert into order_items//LAST_INSERT_ID()为内置的函数,可以获取上⼀次插⼊的主键的值values(LAST_INSERT_ID(),1,1,2.25), (LAST_INSERT_ID(),2,1,2.25)此时orders与order_items的插⼊如下复制整张表//将orders表中的数据全部复制到orders_archivedcreate table orders_archived asselect * from orders这⾥select作为⼦句完成及其强⼤的功能注意:这种⽅式复制出来的表没有主键,且不⽀持AI复制部分表insert into orders_archivedselect * from orderswhere order_date < '2019-01-01'利⽤联合以及create table构建⾃⼰想要的新表,⽤invoices与clients表显⽰已经下过订单且显⽰客户信息use sql_invoicing;create table invoices_archived asselect e_id,, as client, e_total,t_total,e_date, t_date,_datefrom invoices iright join clients c on _id = _idwhere t_date is not nullinvoices 表clients 表创建出来的新表invoices_archivedupdate以之前invoice表为例,更新第⼀⾏数据update invoicesset payment_total = 10, payment_date = '2019-03-01'//set payment_total = default, payment_date = NULL

恢复默认值where invoice_id = 1更新多⾏数据只需要更改where的条件就⾏,不加where表⽰全表use sql_invoicing;update invoicesset

payment_total = invoice_total * 0.5, payment_date = due_datewhere client_id = 3利⽤select挑选更新条件use sql_invoicing;update invoicesset

payment_total = invoice_total * 0.5, payment_date = due_datewhere client_id in (select client_id from where state in ('CA','NY'))delete与update类似delete from invoiceswhere client_id in (select client_id from clients where name = 'Myworks')

2023年8月3日发(作者:)

Mysql8.0学习笔记(3)——insertinto,set,delete⽂章⽬录简介在本章前先了解下table的属性,还是以customers为例,点击表旁边的扳⼿获得如下图对于Datatype :INT :只⽀持数字形式,例如“1234”VARCHAR(50):该类型为字符串格式,空间为变长,最长为50CHAR(2):字符串格式,空间固定为2,不⾜补空格DATE:⽇期格式 例如"2020-04-16"PK:表⽰为主键NN:表⽰不为空,即在这张表⾥勾选中的不能为空,其他可选AI:⾃动增量属性,⼀般以主键为准,根据插⼊数据使主键递增Default/Expression:没输⼊值的默认值insert into插⼊⼀⾏数据use sql_store;insert into customers//因为主键设置AI,默认加1,其他没填写的使⽤默认值value(default,"lj",'jiawei','1993-09-22',null,

'adress','city','ca',default)//也可以指定插⼊哪些值,顺序只要保证对应⼀致就⾏insert into customers(first_name, last_name, birth_date, address,city,state)value("lj",'jiawei','1993-09-22',

'adress','city','ca')插⼊多⾏数据使⽤’,'隔开就⾏insert into customers(first_name, last_name, birth_date, address,city,state)value("lj",'jiawei','1993-09-22',

'adress','city','ca'), ("lj2",'jiawei2','1993-09-22',

'adress','city','ca'),将数据插⼊分层表中对于⼀个table,可能有⼦表来表⽰其其余的信息,例如订单orders,有⼦表order_items其中order中的customer_id,order——date,status为NN;order_items中的product_id,quantity,unit_price也为NN;order_id都为两个表的主键在插⼊orders的时候同时也要插⼊order_itemsinsert into orders(customer_id,order_date,status)values(1,'2019-01-02',1);insert into order_items//LAST_INSERT_ID()为内置的函数,可以获取上⼀次插⼊的主键的值values(LAST_INSERT_ID(),1,1,2.25), (LAST_INSERT_ID(),2,1,2.25)此时orders与order_items的插⼊如下复制整张表//将orders表中的数据全部复制到orders_archivedcreate table orders_archived asselect * from orders这⾥select作为⼦句完成及其强⼤的功能注意:这种⽅式复制出来的表没有主键,且不⽀持AI复制部分表insert into orders_archivedselect * from orderswhere order_date < '2019-01-01'利⽤联合以及create table构建⾃⼰想要的新表,⽤invoices与clients表显⽰已经下过订单且显⽰客户信息use sql_invoicing;create table invoices_archived asselect e_id,, as client, e_total,t_total,e_date, t_date,_datefrom invoices iright join clients c on _id = _idwhere t_date is not nullinvoices 表clients 表创建出来的新表invoices_archivedupdate以之前invoice表为例,更新第⼀⾏数据update invoicesset payment_total = 10, payment_date = '2019-03-01'//set payment_total = default, payment_date = NULL

恢复默认值where invoice_id = 1更新多⾏数据只需要更改where的条件就⾏,不加where表⽰全表use sql_invoicing;update invoicesset

payment_total = invoice_total * 0.5, payment_date = due_datewhere client_id = 3利⽤select挑选更新条件use sql_invoicing;update invoicesset

payment_total = invoice_total * 0.5, payment_date = due_datewhere client_id in (select client_id from where state in ('CA','NY'))delete与update类似delete from invoiceswhere client_id in (select client_id from clients where name = 'Myworks')