Simple operation mysql sql statement

A commonly used, simple SQL operation

  1. database operations:

    1) Create a database:  the Create Database database_name; create and set the character encoding  create database database_name character set utf8; 

    2) delete the database:  drop The DateBase database_name; 

    3) See the database character set encoding:  Show Variables like 'the character_set_database' If a visualization tool to switch to the search database, or use:  use database_name; database search using the command

    4) modify the character encoding database:   ALTER Character database_name SET UTF8; 

  Table 2. Operating data:

    1)创建表: create table table_name(field1 int primary key,field2 varchar(20) not null ...) 

    2) delete the table:  drop the Table table_name 

    3)插入表: insert into table_name(field1,field2) values(value1,value2) 

    4) look-up table:  the SELECT * query from the WHERE table_name 

    5)添加列: alter table table_name add col_name varchar(20) not null 

    6) Delete columns:  the ALTER drop the Table table_name column col_name 

    7)修改列: alter table table_name modify column col_name varchar(50) 

    8) Update Column:  Update table_name the SET col1 = value1 ... the WHERE condition ... 

  3. Constraints

    1) types: primary key (primary key constraint), default (default constraint), not null (non-empty constraint), unique (unique constraint), foreign key (foreign key constraints), check (check constraint)

    2) add a constraint:  ALTER Table table_name the Add constraint constraint constraint name Type 

         比如: alter table student add constraint fk_1 foreign key(class_id) references class(class_id); 

    3) Delete constraint:  the ALTER the Table table_name drop constraint constraint type name Note: When you delete the primary key, you should delete references to its foreign key

         比如: alter table student drop foreign key fk_1 

III. Common query

 

  1. A simple query:

    1) Unconditional inquiry:  the SELECT * from table_name; || the SELECT col1, col2, ... from table_name; 

    2) conditions of inquiry:  the SELECT * from table_name the WHERE condition; 

    3) Sort query:  the SELECT col1, col2, ... from table_name the WHERE condition .. order by column name desc / asc desc: descending order. asc: from small to large, the default is asc

      For example:  the SELECT s_id, s_name, s_score from the WHERE s_score Student> = 60 by the Order s_id asc translation: check out the student failed, according to the student id from small to large

    4) Fuzzy queries: query keywords  like  primarily  %  ,  _  , []  three characters, represents matches zero or more characters (wildcard), _ a matching character []   matches one of which (similar to the regular expression formula)

      Example:  the SELECT * Student from the WHERE s_name like 'Zhang%' Translation: Query students surnamed Zhang, two words, three words are found out, such as: Joe Smith, Zhang Mazi

      Example:  the SELECT * Student from the WHERE s_name like 'Zhang _' Translation: Query students surnamed Zhang, and only two words, such as: Joe Smith, Zhang four

      Example:  the SELECT * Student from the WHERE s_name like '[Zhang Li Wang] three' translation: Name Query students are: Joe Smith, Lee III, king of the three information

    5) grouping query:  the SELECT * from table_name Group by column name keyword group by, the same statistics columns to group data

         For example:  the SELECT s_score, COUNT (*) 'number' from student group by s_score Translation: students score for each query how many people, that is, the same results have been grouped

      Grouped query commonly used functions:

        (1) max: seeking maximum cases:  the SELECT s_name, max (math_score) from Student Group by s_name query highest math student names

        (2) min: minimization cases:  the SELECT s_name, min (math_score) from Student Group by s_name query lowest math student names

        (3) avg: Averaging example:  the SELECT class_id, AVG (math_score) by Student Group class_id query each class average math scores from

        (4) sum: total number of patients seeking:  the SELECT SUM (s_id) How many students from student look-up table, a total of

        (5) count: find the number of lines 

    6) having usage: filtering into a variety of data sets, it can not filter the data in the table as a real query

            For example:  the SELECT s_name, SUM (s_score) from Student Group students by s_name the HAVING sum (s_score)> 600 queries a total score of greater than 600 points, but we did not score table this record

          Only grades for each subject, then you can use having a, where it can not be screened a total score of greater than 600 students.

          having and where the difference between:

            having: having to play a role in the query results columns, filter the data

            wherer: where to play a role for the columns in the table, query data

    7) limit Usage: limit is mainly used for paging, limit n, m represents the m data fetch start from n + 1

        For example:  SELECT * from 2,5 Student limit to all the information represented in the article 3 of the rear five records: 3,4,5,6,7

    8) simple multi-table query:  the SELECT * table1, table2 * from table1, table2 the WHERE condition. 

  2. The sub-queries and join queries

    1) where the sub-query: the inner query results as a comparison condition of the outer query

          For example:  the SELECT s_name, s_score from s_score in the WHERE Student (Student from the SELECT s_score the WHERE s_score> = 60) of students with passing grades query, you can put parentheses after sub-queries, you can also put the known data.

    2) from the sub-query: handle query results as a table, query again

          For example: query passing grades the student's name classes, there will be sub-query as a new table (stu) further inquiry, here are the class table (calss) and student table (student)

        select s_name,class_name from class,(select s_name,class_id from student where s_score>=60) as stu where class.class_id = stu.class_id 

 

 

    3) exists subqueries: the query results to get the inner handle, look at the inner query is established

         For example: Query class in the student's name,

         select class_id,s_name from student where exists(select * from class where class.class_id=student.class_id) 

    4) join query

      We join query data table to come out, easy to view control

          

      left join  left connecting: subject to the left table, the table to find the right data, if there is no matching data, places null fill vacancies

           Syntax:  the SELECT col1, col2, col3 from TA TB on conditions on the Join left behind to put some conditions connected with the back where the same conditions with

              例: SELECT class.*,s_id,s_name FROM class LEFT JOIN student ON class.class_id=student.class_id   

            Results:   query the class of students, no students would fill the seats with a null

     right join  Right connection: subject to the right table, the table to the left to find the data, if there is no matching data, null fill vacancies places   and connected to the opposite left

            Syntax:  the SELECT col1, col2, right from the Join TA TB ON condition col3 

            例: SELECT class.*,s_id,s_name FROM student RIGHT JOIN class ON class.class_id=student.class_id  

           Result:   the location of the table for a moment, we can see that the result is the same, only the left and right connections in different directions connected

     inner join  the connection: the query result is the intersection of the two tables are connected,

           Syntax:  .. The SELECT * TA1, TA2 * from the Join Inner TA1 TA2 ON condition 

           Example:  . * The SELECT class, s_id, s_name the FROM the INNER JOIN Student class we are not here with the conditions, the query is that all the intersection of two tables

                

           Example:  . * The SELECT class, s_id, after s_name FROM student INNER JOIN class ON class.class_id = student.class_id conditions, the conditions are met will check out

              

Guess you like

Origin www.cnblogs.com/wpnr/p/11951664.html