sql学习-unit4
如何插入更新和删除数据1-列属性|Column AttributesColumn Name:列名PK(Primary Key):表示主键列NN(Not Null)表示非空AIAuto Increment)表示自动递增Default/Expression:表示默认值/可计算的表达式2-插入单行|Inserting a Rowinsert into customers ( customer_id, last_name, first_name, birth_date, address, city, state) values ( default, Smith, John, 1990-01-01, address, city, CA)可以选择相应列进行赋值可以改变列的顺序赋值3-插入多行|Inserting Multiple Rowsinsert into shippers (name) values (Shipper1), (Shipper2), (Shipper3)小练习insert into products ( name, quantity_in_stock, unit_price) values (potato,80,2.5), (tomato,90,1.5), (fruit,200,3.6)4-插入分层行|Inserting Hierarchical Rows如何往多表中插入数据insert into orders (customer_id,order_date,status) values (1,2019-01-02,1); -- select last_insert_id() 最近插入id:它能返回我们插入新行时MySQL生成的那个id insert into order_items values (last_insert_id(),1,1,2.95), (last_insert_id(),2,1,3.95)5-创建表复制|Creating a Copy of a Table如何从一张表复制数据到另外一张表create table orders_archived as select * from orders -- 子查询使用选择语句作为插入语句中的子查询insert into orders_archived select * from orders where order_date 2019-01-01小练习create table invoice_archived as select i.invoice_id, i.number, c.name as client, i.invoice_total, i.payment_total, i.invoice_date, i.due_date, i.payment_date from invoices i join clients c on i.client_id c.client_id where i.payment_date is not null6-更新单行|Updating a Single Rowupdate invoices set payment_total 10,payment_date 2019-03-01 where invoice_id 1update invoices set payment_total invoice_total*0.5, payment_date due_date where invoice_id 37-更新多行|Updating Multiple Rowsupdate invoices set payment_total invoice_total*0.5, payment_date due_date -- where client_id 3 where client_id in (3,4)如果想更新所有记录省略where子句小练习update customers set points points50 where birth_date 1990-01-018-在Updates中用子查询|Using Subqueries in Updatesupdate invoices set payment_total invoice_total*0.5, payment_date due_date -- where client_id -- (select client_id -- from clients -- where name Mywork) where client_id in (select client_id from clients where state in (CA,NY))小练习update orders set comments Gold customer where customer_id in (select customer_id from customers where points 3000)9-删除行|Delect Rowsdelete from invoices where invoice_id 1delete from invoices where client_id ( select client_id from clients where name Myworks)10-恢复数据库|Restoring the Databases重新打开数据库运行