zoukankan      html  css  js  c++  java
  • MySQL Crash Course #10# Chapter 19. Inserting Data

    INDEX

    BAD EXAMPLE

    INSERT INTO Customers
    VALUES(NULL,
       'Pep E. LaPew',
       '100 Main Street',
       'Los Angeles',
       'CA',
       '90046',
       'USA',
       NULL,
       NULL);

    Always Use a Columns List As a rule, never use INSERT without explicitly specifying the column list. This will greatly increase the probability that your SQL will continue to function in the event that table changes occur.

    Improving Overall Performance

    Databases are frequently accessed by multiple clients, and it is MySQL's job to manage which requests are processed and in which order. INSERT operations can be time consuming (especially if there are many indexes to be updated), and this can hurt the performance of SELECT statements that are waiting to be processed.

    If data retrieval is of utmost importance (as is usually is), you can instruct MySQL to lower the priority of your INSERT statement by adding the keyword LOW_PRIORITY in between INSERT and INTO, like this:

    INSERT LOW_PRIORITY INTO

    Incidentally, this also applies to the UPDATE and DELETE statements that you'll learn about in the next chapter.

    Inserting Multiple Rows INSTEAD OF Inserting a Single Row

    INSERT INTO customers(cust_name,
       cust_address,
       cust_city,
       cust_state,
       cust_zip,
       cust_country)
    VALUES(
            'Pep E. LaPew',
            '100 Main Street',
            'Los Angeles',
            'CA',
            '90046',
            'USA'
         ),
          (
            'M. Martian',
            '42 Galaxy Way',
            'New York',
            'NY',
            '11213',
            'USA'
       );

    Improving INSERT Performance This technique can improve the performance of your database possessing, as MySQL will process multiple insertions in a single INSERT faster than it will multiple INSERT statements.

    Inserting Retrieved Data 

    INSERT INTO customers(cust_id,
        cust_contact,
        cust_email,
        cust_name,
        cust_address,
        cust_city,
        cust_state,
        cust_zip,
        cust_country)
    SELECT cust_id,
        cust_contact,
        cust_email,
        cust_name,
        cust_address,
        cust_city,
        cust_state,
        cust_zip,
        cust_country
    FROM custnew;

    PS. 列名可以不对应,列的值类型对应就可以了。

  • 相关阅读:
    重写/覆盖基类的事件
    关于“日志”的输出控制
    WPF中DataGrid垂直滚动条滚动后导致每行CheckBox选择错乱
    WPF窗体应用程序开发
    C# 操作自定义config文件
    WPF绑定数据源之RelativeSource
    C# http请求 设置代理(标题可以作为搜索关键字)
    前端加载特效
    实现不同的项目,用不同的git 账号提交
    Casbin 使用记录
  • 原文地址:https://www.cnblogs.com/xkxf/p/8881884.html
Copyright © 2011-2022 走看看