MySQL Developer
1.The mysql Client Program 2.Data Types 3.Joins 4.Subqueries 5.Views 6.StoredRoutine .
1.Client/Server Concepts
- 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 -CYou can also provide a database name to select that database as the default database.
shell> mysql -u user_name -p -h host_name db_name
- 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=""To create or modify an option file, you must have write permission for it. Client programs need only read access.
- 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>
- 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.
2.Data Types
- INT
Storage Required
4 bytes Signed Range
-2,147,683,648 to 2,147,483,647 Unsigned Range
0 to 4,294,967,295 - 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)The stored values are approximate.
- Fixed-Point Data Types
3.Joins
- 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; Writing Inner Joins with INNER JOIN
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.
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.
- Writing Inner Joins with the Comma Operator
- 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); - Writing RIGHT JOIN Queries
- Writing LEFT JOIN Queres
4.Subqueries
A subquery is a SELECT statement that is placed within parentheses inside another SQL statement.
- 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.
- 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;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:
set @city_pop = (select sum(Population) from city);
set @country_pop = (select sum(Population) from country);
select @city_pop / @country_pop as s2; - 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
); - 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;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:select
Name
from
Country
where
Continent = 'Europe'
AND Code = ANY (
select CountryCode
from CountryLanguage
where Language = 'Spanish'
)
order by
Name; - 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; - 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'
);
- Using ALL, ANY, and SOME
- 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'
);select
Name, Population
from
Country
where
(Continent, Region) = ('Europe', 'Western Europe'); - 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; - 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; - 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;
- Converting Subqueries to Inner Joins
5.Views
- 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
);
6.Stored Routine
- 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 .
MySQL Developer的更多相关文章
- Installing MySQL Server
Installing MySQL Server Here we will learn how to Compile and Install the MySQL Server from source c ...
- MySQL Error Number 1005 Can’t create table(Errno:150)
mysql数据库1005错误解决方法 MySQL Error Number 1005 Can’t create table ‘.\mydb\#sql-328_45.frm’ (errno: 150) ...
- Ubuntu 16.04 LAMP server tutorial with Apache 2.4, PHP 7 and MariaDB (instead of MySQL)
https://www.howtoforge.com/tutorial/install-apache-with-php-and-mysql-on-ubuntu-16-04-lamp/ This tut ...
- MySQL Yum存储库 安装、升级、集群
添加MySQL Yum存储库 首先,将MySQL Yum存储库添加到系统的存储库列表中.按着这些次序: 在http://dev.mysql.com/downloads/repo/yum/上转到MySQ ...
- (0.2.5)Mysql安装——RPM方式安装
rpm安装mysql 卸载与安装服务端 一.安装服务端与客户端 #查看RPM包中所有的文件shell> rpm -qpl mysql-community-server-version-dis ...
- Navicat连接MySQL数据库的一些问题与解决方案
前言 安装MySQL数据库与Navicat并不算难事,关键是怎么让他们工作花费了我整整一天的时间,最终才把弄好.遇到各种各样的问题,上网看了大量博客,发现很多博客都是直接copy或者并不能非常好的解答 ...
- linux应用之mysql数据库指定版本的yum安装(centos)
A Quick Guide to Using the MySQL Yum Repository Abstract The MySQL Yum repository provides RPM packa ...
- 使用MySQL Yum存储库的快速指南【mysql官方文档】
使用MySQL Yum存储库的快速指南 抽象 MySQL Yum存储库提供用于在Linux平台上安装MySQL服务器,客户端和其他组件的RPM包.这些软件包还可以升级和替换从Linux发行版本机软件存 ...
- Qt之编译MySQL数据库驱动(MSVC)
Qt之编译MySQL数据库驱动(MSVC) (2013-12-13 20:24:46) 转载▼ 标签: qt mysql qmysql qt编译mysql qt之msvc编译mysql 分类: Qt ...
随机推荐
- java类加载与static
一.类加载 当jvm去运行一个类时,会先加载该类,把该类在硬盘上字节码加载到jvm的内存.java HelloWorld>字节码会被加载到代码段中>加载过程中会有一些静态的常量,这部分会事 ...
- HanLP代码与词典分离方案与流程
之前在spark环境中一直用的是portable版本,词条数量不是很够,且有心想把jieba,swcs词典加进来, 其他像ik,ansi-seg等分词词典由于没有词性并没有加进来. 本次修改主要是采用 ...
- Linux paste命令详解
Linux paste命令 Linux paste命令用于合并文件的列.paste指令会把每个文件以列对列的方式,一列列地加以合并 将每个指定文件里的每一行整合到对应一行里写到标准输出,之间用制表符分 ...
- Apache Kafka监控之Kafka Web Console
Kafka Web Console:是一款开源的系统,源码的地址在https://github.com/claudemamo/kafka-web-console中.Kafka Web Console也 ...
- htmlcleaner使用及xpath语法初探
一.HtmlCleaner使用: 1.HtmlCleaner HtmlCleaner是一个开源的Java语言的Html文档解析器.HtmlCleaner能够重新整理HTML文档的每个元素并生成结构良好 ...
- javaweb下载中的一个问题
如果你发现,response头以及contentType都已经设置没错,但出现浏览器将下载的文件内容显示到新窗口 那么解决方案就是在请求的时候不要产生新的窗口
- 第一个NIOS II工程using Qsys-------Let Qsys Say Hello
1.新建工程 2.添加原理图文件 注:似乎Nios II工程都需要涉及到原理图编程. 3.进入Qsys进行内核设计 注:启动Qsys后,系统已经为内核默认添加了一个组件clk_0. 4.设置时钟名字和 ...
- OpenEXR的读取机制
这还是一篇学习笔记,知识重点还是领会完再敲一遍比较好. OpenEXR通过RgbaInputFile这个接口读取RGBA ONLY图像文件信息,该接口通过dataWindow()方法获取图像边界坐标信 ...
- 查找Python项目依赖的库并生成requirements.txt
使用pip freeze pip freeze > requirements.txt 这种方式配合virtualenv 才好使,否则把整个环境中的包都列出来了. 使用 pipreqs 这个工具的 ...
- 【java】之正则表达式摘要
构造 匹配 字符 x 字符 x \\ 反斜线字符 \0n 带有八进制值 0 的字符 n (0 <= n <= 7) \0nn 带有八进制值 0 的字符 nn (0 <= n < ...