MySQL Developer

1.The mysql Client Program  2.Data Types  3.Joins  4.Subqueries  5.Views  6.Stored Routine  .mysql


1.Client/Server Concepts

  1. Connection Parameter Options
    --host=host_name or -h host_name
        The default host value is localhost.
    
    --port=port_number or -P port_number
        The default MySQL port number is 3306.
    
    --user=user_name or -u user_name
        On Windows, the default MySQL account name is ODBC. On Unix, client programs use your system login name as your default MySQL account username.
    
    --password=pass_value or -ppass_value
    
    --compress or -C
    View Code

    You can also provide a database name to select that database as the default database.git

    shell> mysql -u user_name -p -h host_name db_name
    View Code

     

  2. Using Option Files
    • On Windows, programs look for option files in the following order: my.ini and my.cnf in the Windows directory(C:\Windows or C:\WinNT), and then C:\my.ini and C:\my.cnf.
    • On Unix, the file /etc/my.cnf serves as a global option file used by all users. Also, you can set up your own user-specific option file by creating a file named .my or .cnf in your home directory. If both exist, the global file is read first.
    • Create an option file
      [client]
      host = argor.com
      port = 3306
      compress
      
      [mysql]
      
      [mysqld]
      port = 3306
      basedir=""
      datadir=""
      View Code

      To create or modify an option file, you must have write permission for it. Client programs need only read access.sql

    • To tell a program to read a single specific option file instead of the standard option files, use the --defaults-file=file_name option as the first option on the command line.
      C:\Users\Administrator>type develop.ini
      [mysql]
      host=localhost
      port=3306
      user=develop
      password=develop
      compress
      default-character-set=utf8
      C:\Users\Administrator>mysql --defaults-file=develop.ini
      Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
      
      mysql>
      View Code

       

  3. Using the --safe-updates Option
    --safe-updates has the following effects:
    
    1.UPDATE and DELETE statements are allowed only if they include a WHERE clause that specifically identifies which records to update or delete by means of a key value, or if they include a LIMIT clause.
    
    2.Output from single-table SELECT statement is restricted to no more 1,000 rows unless the statement includes a LIMIT clause.
    
    3.Multiple-table SELECT statements are allowed only if MySQL will examine no more than 1,000,000 rows to process the query.
    View Code

     

2.Data Types

  1. INT
    Storage Required
        4 bytes
    
    Signed Range
        -2,147,683,648 to 2,147,483,647
    
    Unsigned Range
        0 to 4,294,967,295
    View Code

     

  2. Floating-Point Data Types
    The following definition specify a single-precision column with a precision of 10 digits and scale of 3 decimals, and a double-precision column with a precision of 20 digits and scale of 7 decimals.
    weight FLOAT(10, 3)
    avg_score DOUBLE(20, 7)
    View Code

    The stored values are approximate.shell

  3. Fixed-Point Data Types

 

3.Joins

  1. Writing Inner Joins
    A join that identifies combinations of matching rows from two tables is called an inner join.
    • Writing Inner Joins with the Comma Operator
      A query to display languages and full country names can be written as a join that matches the country codes in the CountryLanguage table with those in the Country table.
      select
          name,language
      from
          ContryLanguage,Country
      where
          CountryCode = Code;
      View Code

       

    • Writing Inner Joins with INNER JOINexpress

      select
          name,language
      from
          ContryLanguage inner join Country
      on
          CountryCode = Code;
      View Code

      If the name of the joined column is the same in both tables, you can add USING() rather than ON after the table names, and list the name within the parentheses.api

      select
          name,language
      from
          ContryLanguage inner join Country
      using(Code);
      View Code

       

      If you're joining the tables using more than one pair of like-named columns, list the column names within the parentheses of the USING() clause separated by commas.app

       

  2. Writing Outer Joins
    To write a join that proides information about mismatches or missing records, use an outer join.
    • Writing LEFT JOIN Queres
      A left join treats the left table (the first one named) as a reference table and produces output for each row selected from it, whether or not the row is matched by rows in the right tables.
      select
          name,language
      from
          Contry left join CountryLanguage
      on
          Code = CountryCode;
      
      select
          name,language
      from
          Contry left join CountryLanguage
      using(Code);
      View Code

       

    • Writing RIGHT JOIN Queries

4.Subqueries

A subquery is a SELECT statement that is placed within parentheses inside another SQL statement.less

  1. Types of Subqueries
    • Scalar subqueries return a single value; that is, one row with one column of data.
    • Row subqueries return a single row with one or more columns of data.
    • Column subqueries return a single column with one or more rows of data.
    • Table subqueries return a result with one or more rows containing one or more rows containing one or more columns of data.
  2. Subqueries as Scalar Expressions
    select
        concat('The country code for Finland is: ',
            (select
                Code
            from
                Country
            where
                Name = 'Finland'
            )) as s1;
    
    select
        (select sum(Population) from city)
        /
        (select sum(Population) from Country) as ratio;
    View Code

    A scalar subquery result can be assigned to a user variable for later use. The previous example can be written with user variables as follows:ide

    set @city_pop = (select sum(Population) from city);
    set @country_pop = (select sum(Population) from country);
    select @city_pop / @country_pop as s2;
    View Code

     

  3. Correlated Subqueries
    Subqueries can be non-correlated or correlated.

    In the following correlated subquery, we calculate which country on each populated continent has the argest population. The value of the column Continent, which appears in the outer query, is used to limit which rows to consider for the MAX() calculation in the subquery:
    select
        Continent, Name, Population
    from
        Country c
    where
        Population = (
                        select MAX(Population)
                        from Country c2
                        where c.Continent = c2.Continent and Population > 0
                    );
    View Code

     

  4. Comparing Subquery Results to Outer Query Columns
    The scalar subquery examples shown in previous sections use the = equality operator to compare a single column to the value returned by the subquery. But you are not limited to using the = equality operator.
    When comparing the values in the outer query with those returned by a scalar subquery, you can use any of the usual comparison operators, such as =,<,>,<>, and >=.
    • Using ALL, ANY, and SOME
      To perform a comparison between a scalar value and a subquery that returns severval rows of data in a single column (a column subquery), we must use a quantified comparison. The quantifier keywords ALL, ANY, and SOME allow comparison to multiple-row results.

      Now, suppose that wo would like to know all the countries in the world where the population is larger than the average country population of all of the world's continents. To get this information, we can use ALL in conjunction with the > operator to compare the values of the country population with every average continent population from the preceding result:
      select
          Name, Population
      from
          Country
      where
          Population > ALL (
                              select avg(Population) from Country
                              group by
                                  Continent
                          )
      Order by
          Name;
      View Code

       
      The keyword ANY is not limited to working with the = operator. Any of the standard comparison operators (=,<,>,<>,>=, and so forth) may be used for the comparison.
      The following example finds the countries on the European continent, and, for each one, tests whether the country is among the worldwide list of countries where Spanish is spoken:ui

      select
          Name
      from
          Country
      where
          Continent = 'Europe'
          AND Code = ANY (
                          select CountryCode
                          from CountryLanguage
                          where Language = 'Spanish'
                      )
      order by
          Name;
      View Code

       

    • Using IN
      When IN is used with a subquery, it is functionally equivalent to = ANY (note that the = sign is part of the equivalence).
      select
          Name
      from
          Country
      where
          Continent = 'Europe'
          AND Code IN (
                      select CountryCode
                      from CountryLanguage
                      where Language = 'Spanish'
                      )
      order by
          Name;
      View Code

       

    • Using EXISTS
      The EXISTS predicate performs a simple test: It tells you whether the subquery finds any rows. It does not return the actual values found in any of the rows, it merely returns TRUE if any rows were found.
      select
          Code c, Name
      from
          Country
      where
          Continent = 'Europe'
          AND EXISTS (
                      select *
                      from CountryLanguage
                      where CountryCode = c
                          AND Language = 'Spanish'
                      );
      View Code

       

  5. Comparison Using Row Subqueries
    For row subqueries, we can perform an equality comparison for all columns in a row. The subquery must return a single row.
    select
        City.Name
    from
        City
    where
        (City.ID, City.CountryCode) = (
                                        select Capital, Code
                                        from Country
                                        where Name = 'Finland'
                                    );
    View Code
    select
        Name, Population
    from
        Country
    where
        (Continent, Region) = ('Europe', 'Western Europe');
    View Code

     

  6. Using Subqueries in the FROM Clause
    select
        avg(cont_sum)
    from (
            select Continent, sum(Population) as cont_sum
            from Country
            group by Continent
        ) as t;
    View Code

     

  7. Converting Subqueries to Joins
    • Converting Subqueries to Inner Joins
      select
          Name
      from
          Country
      where
          Code IN (select CountryCode from CountryLanguage);
      
      select
          DISTINCT Name
      from
          Country, CountryLanguage
      where
          Code = CountryCode;
      View Code

       

    • Converting Subqueries to Outer Joins
      select
          Name
      from
          Country
      where
          Code NOT IN (select CountryCode from CountryLanguage);
      
      select
          Name
      from
          Country LEFT JOIN CountryLanguage
      on
          Code = CountryCode
      where
          CountryCode IS NULL;
      View Code

 

5.Views

  1. Creating Views
    The name of the second column is COUNT(Language), which must be referred to using a quoted identifier (that is, as `COUNT(Language)`). To avoid this, provide names for the columns by including a column list in the view definition.
    Another way to provide names for view columns is by using column aliases in the SELECT statement.
    create
        view 'CountryLangCount'
        as
    (
        select Name, count(Language)
        from Country, CountryLanguage
        where Code = CountryCode
        group by Name
    );
    
    create
        view 'CountryLangCount' (Name, LangCount)
        as
    (
        select Name, count(Language)
        from Country, CountryLanguage
        where Code = CountryCode
        group by Name
    );
    
    create
        view 'CountryLangCount'
        as
    (
        select Name, count(Language) as LangCount
        from Country, CountryLanguage
        where Code = CountryCode
        group by Name
    );
    View Code

     

6.Stored Routine

  1. Differences Between Stored Procedures and Functions
    The most general difference between procedures and functions is that they are invoked differently and for different purposes:
    • A procedure does not return a value. It is invoked with a CALL statement.
    • A function is invoked within an expression and returns a single value directly to the caller to be used in the expression.

 

 

 

 

MySQL Developer .

相關文章
相關標籤/搜索