1.The mysql Client Program 2.Data Types 3.Joins 4.Subqueries 5.Views 6.Stored Routine .mysql
--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
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
[client] host = argor.com port = 3306 compress [mysql] [mysqld] port = 3306 basedir="" datadir=""
To create or modify an option file, you must have write permission for it. Client programs need only read access.sql
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>
--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.
Storage Required 4 bytes Signed Range -2,147,683,648 to 2,147,483,647 Unsigned Range 0 to 4,294,967,295
weight FLOAT(10, 3) avg_score DOUBLE(20, 7)
The stored values are approximate.shell
select name,language from ContryLanguage,Country where CountryCode = Code;
Writing Inner Joins with INNER JOINexpress
select name,language from ContryLanguage inner join Country on CountryCode = 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);
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
select name,language from Contry left join CountryLanguage on Code = CountryCode; select name,language from Contry left join CountryLanguage using(Code);
A subquery is a SELECT statement that is placed within parentheses inside another SQL statement.less
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;
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;
select Continent, Name, Population from Country c where Population = ( select MAX(Population) from Country c2 where c.Continent = c2.Continent and Population > 0 );
select Name, Population from Country where Population > ALL ( select avg(Population) from Country group by Continent ) Order by Name;
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;
select Name from Country where Continent = 'Europe' AND Code IN ( select CountryCode from CountryLanguage where Language = 'Spanish' ) order by Name;
select Code c, Name from Country where Continent = 'Europe' AND EXISTS ( select * from CountryLanguage where CountryCode = c AND Language = 'Spanish' );
select City.Name from City where (City.ID, City.CountryCode) = ( select Capital, Code from Country where Name = 'Finland' );
select Name, Population from Country where (Continent, Region) = ('Europe', 'Western Europe');
select avg(cont_sum) from ( select Continent, sum(Population) as cont_sum from Country group by Continent ) as t;
select Name from Country where Code IN (select CountryCode from CountryLanguage); select DISTINCT Name from Country, CountryLanguage where Code = CountryCode;
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;
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 );
MySQL Developer .