"6.1" SQL base----2 DML statement "Data manipulation statement"

Source: Internet
Author: User

2 DML Statement "Data manipulation Statement" 2.1 Inserting data

1. Insert data for all fields of the table : INSERT into Table name VALUES ("Value 1", "Value 2", "Value 3", "Value 4"); or INSERT into table name (field name 1, field name 2, field name 3, field name 4) VALUES ("Value 1", "Value 2", "Value 3", "Value 4");

Note: values () in parentheses are the same as in the English single quotation mark ' or double quotation mark ' in English.

2. Insert data for the specified field: INSERT intotable name (Field 1, Field 3) VALUES ("Value 1", "Value 3");

3. Insert more than one data: INSERT intotable name (field name 1, field name 2, field name 3, field name 4) VALUES ("Value 1", "Value 2", "Value 3", "Value 4"), ("Value 1", "Value 2", "Value 3", "Value 4");

or (omit field name)

Insert into table name VALUES ("Value 1", "Value 2", "Value 3", "Value 4"), ("Value 1", "Value 2", "Value 3", "Value 4");

Example: INSERT into Student VALUES ("4", "female", "4444"), ("3", "female", "3333");

2.2 Modifying data

Update table name SET field Name 1 = value 1, field Name 2 = value 2, ..., field name n = value n where conditional expression;

2.3 Deleting data

1. Delete data using delete: delete from table name [where conditional expression]; (Note: If there is no where condition expression, all data in the table will be deleted)

2. Use truncate to clear the table data:TRUNCATE TABLE name;

3.truncate,delete,drop difference:truncate speed is faster than delete and will free up space but neither of them deletes the table definition, drop delete table definition + free space "Reference blog, thank the blogger": http://blog.csdn.net/ws0513/article/details/49980547

2.4 Form Queries

2.4.1 Simple Query

    1. Query all fields: SELECT * from table name;
    2. Query specified field: Select Field 1, Field 3 from table name;
    3. Avoid duplicate queries: SELECT DISTINCT field name ... from table name;
    4. Alias tables and fields: Select Field name as alias from table name as alias;

2.4.2 Condition Query

    1. Queries with relational operators and logical operators: Example: SELECT * FROM student where sno>3;
    2. Query with in Keyword: example: SELECT * from student where Sno in (1,3,6);
    3. Query with between and keyword: example 1:select * from student where Sno between 3 and 5; <<>> Example 2:select * from student where Sno not between 5 and 6;
    4. Null value query with IS NULL keyword: example: SELECT * FROM student where ZnO is null;
    5. Query with a like [not like] Keyword: example 1:select * from student where ZnO like ' 6% '; <<>> Example 2:select * from student where ZnO like ' 666_ '; [where '% ' stands for any length of character, ' _ ' stands for single character]

2.4.3 Advanced Query

    1. Grouped query "GROUP BY keyword and having keyword": Having and where are used to limit the display, but have only been used after grouping the group by keyword to select the group that satisfies the having condition.
    • Example: Select Ssex,count (ssex) from student group by Ssex; The results are as follows:
    • Example: Select Ssex,count (ssex) from student group by Ssex have count (ssex) > 3; The results are as follows:
    1. Null
    2. Sort the query structure: example: SELECT * FROM Student order by Sno;
    3. Limit number of Queries: example: SELECT * FROM Student ORDER by Sno limit 2, 3; [Find information about 3 students starting from the 3rd classmate in the Student table, the index of the result set returned by the Select query starts from 0]
    4. Aggregate functions: Example: SELECT COUNT (Sno) from student;

"6.1" SQL base----2 DML statement "Data manipulation statement"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.