文章目录 CHAR和VARCHAR 插入单行 INSERT INTO 插入多行 插入分层行 LAST_INSERT_IN() 创建表复制 CREAT TABLE AS 更新单行 UPDATE...SET 更新多行 在UPDATES中使用子查询【需着重复习】 删除行 DELETE 恢复数据库到原始状态
CHAR和VARCHAR
CHAR(50):存储文本占5个字符,MySQL会再插入45个空格符填满此列。因此一般最好用VARCHAR来存储字符串或文本值。 VARCHAR(50):文本占5个字符,就只存储这5个字符,不会浪费空间。
插入单行 INSERT INTO
INSERT INTO 表明(列名) VALUE(每列的值) DEFAULT:写入默认值
INSERT INTO customers
VALUES ( DEFAULT , 'John' , 'Smith' , '1990-01-01' , NULL , 'address' , 'city' , 'CA' , DEFAULT )
INSERT INTO customers( first_name, last_name, birth_date, address, city, state)
VALUES ( 'John' , 'Smith' , '1990-01-01' , 'address' , 'city' , 'CA' )
插入多行
INSERT INTO shippers( name)
VALUES ( 'Shipper1' ) , ( 'wuxu' ) , ( 'songtuo' )
INSERT INTO products
VALUES ( DEFAULT , 'laote' , 89 , 1.34 ) , ( DEFAULT , 'laoda' , 82 , 1.33 ) , ( DEFAULT , 'laoer' , 75 , 1.32 )
INSERT INTO products( name, quantity_in_stock, unit_price)
VALUES ( 'laote' , 89 , 1.34 ) , ( 'laoda' , 82 , 1.33 ) , ( 'laoer' , 75 , 1.32 )
插入分层行 LAST_INSERT_IN()
获取新加记录的id:LAST_INSERT_IN()
INSERT INTO orders( customer_id, order_date, status )
VALUES ( 1 , '2019-01-02' , 1 ) ;
INSERT INTO order_items
VALUES ( LAST_INSERT_ID( ) , 1 , 1 , 2.95 ) , ( LAST_INSERT_ID( ) , 2 , 1 , 5.63 ) ;
创建表复制 CREAT TABLE AS
用CREAT TABLE AS 创建的表复制,复制的表中没有主键,也没有被标记为自动递增的列 Truncate:右键表,使用这个工具可以清除表中所有的数据,使表变成空表 向表中插入数据时,可以使用选择语句(SELECT FROM WHERE)作为插入语句(INSERT INTO)的子查询进行数据的插入
CREATE TABLE orders_archived_wuxu AS
SELECT * FROM orders
INSERT INTO orders_archived_wuxu
SELECT *
FROM orders
WHERE order_date < '2019-01-01'
CREATE TABLE invoices_archived_wuxu AS
SELECT invoice_id, number, c. name, invoice_total, payment_total, invoice_date, due_date, payment_date
FROM invoices
JOIN clients c USING ( client_id)
WHERE payment_date IS NOT NULL ;
更新单行 UPDATE…SET
UPDATE invoices
SET payment_total = 10 , payment_date = '2019-03-01'
WHERE invoice_id = 1 ;
UPDATE invoices
SET payment_total = DEFAULT , payment_date = NULL
WHERE invoice_id = 1 ;
UPDATE invoices
SET payment_total = invoice_total * 0.5 , payment_date = due_date
WHERE invoice_id = 3 ;
更新多行
UPDATE invoices
SET payment_total = invoice_total * 0.5 , payment_date = due_date
WHERE client_id IN ( 3 , 4 ) ;
USE sql_store;
UPDATE customers
SET points = points + 50
WHERE birth_date < '1990-01-01' ;
在UPDATES中使用子查询【需着重复习】
USE sql_invoicing;
UPDATE invoices
SET payment_total = invoice_total * 0.5 , payment_date = due_date
WHERE client_id = ( SELECT client_idFROM clientsWHERE name = 'Myworks' ) UPDATE invoices
SET payment_total = invoice_total * 0.5 , payment_date = due_date
WHERE client_id IN ( SELECT client_idFROM clientsWHERE state IN ( 'CA' , 'NY' ) )
UPDATE orders
JOIN customers USING ( customer_id)
SET comments = 'GoldenClient'
WHERE shipped_date IS NOT NULL AND points > 3000
UPDATE orders
SET comments = 'GoldenClient'
WHERE shipped_date IS NOT NULL AND customer_id IN ( SELECT customer_idFROM customersWHERE points > 3000 )
删除行 DELETE
DELETE FROM invoices
WHERE client_id = ( SELECT client_idFROM clientsWHERE name = 'Myworks' )
恢复数据库到原始状态
在IDEA中找到Project,选择对应的sql文件,双击后,CtrlA全选后执行,即可恢复数据库到原始状态。