SQL syntax (1)

First, the understanding of SQL:

1. What is SQL?

  • Structured Query Language: Structured Query Language
  • In fact, it defines all the rules of relational database operations. Existence of each database operation is not the same place, called "dialects."

2.SQL general syntax:

  1. SQL statements can be single or multiple rows of writing, end with a semicolon.
  2. You can use spaces and indentation to enhance the readability of the statement
  3. MySQL database SQL statements are not case sensitive, use uppercase keyword suggestions.
  4. Three kinds of comments:
    •   Single-line comments ①: - annotation content (note - followed by a space)
    •        Single-line comments ②: # annotation content (mysql-specific)
    •        Multi-line comments: / * comment * content /   

 3.SQL classification:

①: DDL: data definition language for defining database objects: databases, tables, columns and the like.

    Keywords: create, drop, alter, etc.

②: DML: database manipulation language, used for the data tables in the database additions and deletions.

    Keywords: insert, delete, update, etc.

③: DQL: data query, the query for records (data) tables in the database.

    Keywords: select, where, etc.

④: DCL: Database Control Language (understanding), is used to define the level of access and security database, and create a user.

    Keywords: GRANT, REVOKE, etc.

Illustration four SQL language:

 

Two, DDL:

***** operate the database: CRUD

1, C (Create): Create:

    ① create the database:
    create database database name;
    ② create a database to determine not exist, then create, there is not created:
    create database if not exists database name;
    ③ create a database and specify the character set:
    create database database name of the character set and character set;
    ④ create a database to determine whether there and specify the character set:
    create database if not exists the name of the database character set character set names;

2, R (Retrieve): Query:

    ① names of all the queries of the database:

    show  databases;

    ② query a database character sets: a database query creation statement

    show create database database name;

3, U (Update): Review:

    ① modify the character set of the database:

    alter database database name of the character set and character set;

4, D (Delete): Delete:

     ① delete the database:

    drop database database name;

    ② determine the database exists, delete:

    database name drop database if exists;

5, using the database:

    ① query the database name currently in use:

    select database();

    ② use the database:

    use database name;

***** operating table:

1, C (Create): Create:

    ① syntax:

    create table 表名(

      Column name Data type 1,

      Column name Data type 2 2

      。。。

      Column Name Data Type n n

); Note the last line can not have, "" number.

    ② database type:

    1, int integer type: age int,

    2, double decimal type: score double (5,2) represents a total of five, two, such as 111.11 after the decimal point.

    3, data date, it contains only the date, yyyy-MM-dd

    4, datatime date, time, day, hour containing year, month, yyyy-MM-dd HH: mm: ss

    5, timestamp time wrong type, minutes and seconds, yyyy-MM-dd HH date when comprising: mm: ss (if not to the future field assignment, or the assignment is null, the current system time is used by default, automatically assigned)

    6, varchar string, name varchar (n), n represents the maximum character name

Create an instance:

create table student(

  id int,

  name varchar(32),

  age int,

  score double(4,1),

  birthday data,

  insert_time timestamp

);

    ③ create a replica of an existing table:

    create table table name like copy table name

2, R (Retrieve): Query:

    ① the name of a query for all tables in the database:

    show tables;

    ② query table structure:

    Desc name of the table;  

3, U (Update): Review:

    ①: Modify the table name:

    alter table table name rename to new table name;

    ②: Modify the table character set:

    alter table table name character set the character set name;

    ③: add the line:

    alter table table add Column Name Data Type;

    ④: change the column name, type:

    alter table change table column names new column name for the new data type;

    alter table column name table modify the new data type;

    ⑤: delete columns:

    alter table drop table column name;

4, D (Delete): Delete:

    ① delete the table:

    drop table 表名;

    ② plus a judge:

    drop table if exists 表名;

 

Three, DML (additions and deletions to the data look-up table):

1, add data:

  Syntax: insert into table (1 column names, column names, column names 2 .... n) values ​​(value 1, value 2 ... n-value);

  note:

  1. Column names and values ​​to one correspondence.
  2. If the table name, column name is not defined, the default value is added to all columns: insert into table values ​​(value 1, value 2 .... n-value);
  3. In addition to digital type, other types require the use of quotation marks.

2, delete the data:

  Syntax: delete from table [where] conditions

  note:

  1. If not where conditions will delete all the records in the table.
  2. If you want to delete all records in two ways:
    1.   delete from table name; - not recommended, because the table how many records how many times the delete operation will be executed, efficiency is too low.
    2. truncate table table name; - recommended, because his principle is to drop the table, and then create an empty table the same high efficiency.

3, modify the data:

  Syntax: updata table set column name 1 = value 1, column name 2 = value 2, ... [where] the conditions

  note:

  1. Note that if you do not add while conditions in the table all the data will be modified.

 

Four, DQL (query):

Record lookup table: select * from table name;

grammar:

  select 

    Field list

  from

    List of table names

  where

    List of conditions

  group by

    Group field

  having 

    Conditions after grouping

  order by

    Sequence

  limit

    Paging limited

1, the underlying query:

  1. Multiple query fields

  select field names, field names 2 ... from table name;

  note:

    If the query all the fields, you can use * instead of a list of fields

  2. Remove duplicate:

  distinct:delect distinct age from st;

  3. computed column

  Usually four operations may be used a series of values.

  IFNULL (Expression 1, Expression 2): null involved, and calculates the result is null

  Expression 1: Which fields need to determine whether it is null;

  Alternatively if the field value is null, the.

  4. surnamed

  as: it can be omitted.

2, condition query:

  1, where clause followed by conditions

  2, the operator:

  >, <, <=,> =, =, <> (Not equal, can also be written! =)

  between ... and ... (such as between 20 and 30, containing 20 and 30)

  in (a collection, you can put multiple query conditions)

  like (fuzzy query), _ represents a position,% indicates multiple locations.

    For example, "Liu" people beginning: select from st where name like 'horse%';

    The second example is the word "small" person: select from st where name like '_% less';

    For example, the name contains the "King" of the people: select from st where name like "% king%";

 

  is null和is not null

  and 或 &&

  or 或 ||

  or not!

3, sorting query:

  Syntax: select from table name order by clause to sort (not written by default ASC);

  Sort order by sort field 1, field 2 Sort, Sort 2 ....

  Sort by:

  ASC: ascending order, default.

  DESC: descending order.

  note:

  If there are a plurality of sorting criteria, the current condition value when the same side, only the second determination condition.

4, aggregate functions: a column data as a whole, longitudinally calculated.

  1. count: count the number of (non-null ① usually selected columns: primary key .②count (*) is not recommended)
  2. max: maximum calculated
  3. min: minimum value Calculation
  4. sum: computing and
  5. avg: average calculated

  Note: the polymerization calculation function, excluding the null value. (Thus when calculating the number, the column select free value, leads to a null value calculated column is not the number)

  solution:

  1, does not comprise selected non-null columns excited calculated.

  2, IFNULL function becomes non-null 0: IFUNLL (column name, 0).

5, grouping query:

  1, the syntax: group by grouping field;

  2, note that:

    1. After the query packet fields: packet fields, aggregate functions

    2.where and having a difference?

      1.where defined in the following packet, if the condition is not satisfied, it is not involved in the packet. After having defined in the packet, if the result is not satisfied, it will not be queried.

      After 2.where not function with the polymerization, having a polymerization function can be determined.

example:

--- grouped according to gender. Queries were boys and girls of the average number of:

select sex,AVG(math),count(id) from st group by sex;

--- grouped according to gender, respectively inquiry average boys and girls, the number. Requirements: Score 70 points lower than people who do not participate in group:

select sex,avg(math),count(id) from st where math > 70 group by sex;

--- grouped according to gender, respectively inquiry average boys and girls, the number. Requirements: Score 70 points lower than people who do not participate in packet after packet number greater than two.

select  sex,avg(math),count(id) from st where math > 70 group by sex having count(id)> 2;

Of course, you can play an alias:

select  sex,avg(math),count(id) renshu from st where math > 70 group by sex having renshu> 2;

6, paging query:

  1. Syntax: limit the beginning of the index, the number of queries per page;

  * The maximum number of display per page - index = 2. Start the (current 1 page)

such as:

  select * from st limit 0,3; - the first page

  select * from st limit 3,3; - second page

  select * from st limit 6,3; - the third page

   3 is a paging operation of MYSQL "dialect", it can only be used in the MYSQL.

  

Guess you like

Origin www.cnblogs.com/liuhuan425/p/10952005.html