zoukankan      html  css  js  c++  java
  • 常见的数据表操作语句-增删改查

    INSERT INTO 插入一条语句

    -- INSERT INTO
    INSERT INTO customers (
    	first_name, 
    	last_name,
        birth_date,
        address,
        city,
        state,
        points)
    VALUES (
        'John', 
        'Smith',
        '1990-01-01',
        'address',
        'city',
        'CA',
        DEFAULT);
    
    INSERT INTO shippers (name)
    VALUES  ('Shipper1'),
    	('Shipper2'),
            ('Shipper3');
    
    -- Exercise
    -- Insert three rows in the products table
    INSERT INTO products(
    	name,
        quantity_in_stock,
        unit_price)
    VALUES
    	('tomato1', 12, 24),
    	('tomato2', 1.2, 23),
        ('tomato2', 1.4, 12);
        
    -- 快速复制一张表alter
    CREATE TABLE orders_archived AS
    SELECT * FROM orders;
    -- 插入一部分
    INSERT INTO orders_archived
    SELECT *
    FROM orders
    WHERE order_date < '2019-01-01';
    

    UPDATE 更新数据

    -- update
    USE sql_invoicing;
    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 invoice_id IN (3, 4);
    
    -- Exercies
    -- Write a SQL statement to 
    -- give any customers born before 1990
    -- 50 extra points
    USE sql_store;
    UPDATE customers
    SET points = points + 50
    WHERE birth_date > '1990-01-01';
    
    -- UPDATE
    
    UPDATE invoices
    SET payment_total = invoice_total * 0.5, payment_date = due_date
    WHERE invoice_id = 
    			(SELECT client_id
    			FROM clients
    			WHERE name = 'Myworks');
                
    UPDATE invoices
    SET payment_total = invoice_total * 0.5, payment_date = due_date
    WHERE invoice_id IN
    			(SELECT client_id
    			FROM clients
    			WHERE state IN ('CA', 'NY'))
    

    删除数据 不常用操作

    -- 删除所有记录
    DELETE FROM invoices;
    
    DELETE FROM invoices
    WHERE invoice_id = (
    		SELECT *
    		FROM clients
    		WHERE name = 'Myworks'
    )
    
    -- 恢复数据库
    
  • 相关阅读:
    流程图如何画
    flex布局
    css函数
    常用的Array相关的属性和方法
    判断一个字符串中出现次数最多的字符,统计这个次数
    css溢出滚动条及去除滚动条的方法
    vue生命周期
    css中添加屏幕自适应方法(rem)
    vue-cli中配置屏幕自适应(px2rem)
    关于解决项目运行时出现的缓存问题
  • 原文地址:https://www.cnblogs.com/jly1/p/12977399.html
Copyright © 2011-2022 走看看