1、MySQL的介绍和安装
1、记在心里
2、写在纸上
3、使用内存
4、使用磁盘
1)数据库
就是存放数据的仓库,是按照数据结构来组织、存储和管理数据的仓库,是数据持久化的工具。
2)使用数据库的优点
存储大量数据
保持数据信息的完整、一致性
数据的共享和安全
3)数据库与应用程序的关系
4)数据库发展历史
萌芽阶段------文件系统数据库
- 使用磁盘等载体记录数据
初始阶段-----网状,层次型数据库
-DBMS的诞生,IBM的IDS,IMS数据库。
现级阶段----关系对象型数据库
-SQL的诞生、加强了数据存储的依赖性。
5)常用数据库
ORACLE
——甲骨文公司的产品
——第一个支持SQL的数据库,在高端数据应用中分布最广
SQLServer
微软公司的产品
将关系型数据库应用推向普及化,在小型机中很好的适用性。
DB2
IMB公司的产品
基于UNIX系统,主要应用在商务大型平台。
MySQL
MySQLLAB公司产品
后被SUN收购,后来随着Oracle公司收购SUN,MySQL归Oracle所有
主要应用在中小型网站,速度快体积小,成本低,开源。
6)MySQL的介绍和安装
首先从Oracle官网下载MySQL的安装包: http://www.mysql.com/downloads/
双击安装
安装过程中可能会 提示你安装.NET framework4.0 安装过程中使用UTF8字符集编码
下载.NET framework4.0并安装即可。
首先停止Windows Update服务
开始——运行——输入%windir%
找到有个叫SoftwareDistribution的文件夹,把它重命名为SDold
重新启动Windows Update服务
2、数据库的操作
MySQL数据库使用SQL语句对数据库进行操作 SQL(Structured Query Language)结构化的查询语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统。
SQL语言包含4个部分
DDL(Data Definition Language ) 数据定义语言,如数据表的建立修改删除等。
DML(Data Manipulation Language) 数据操作语言,如对数据的增删改。
DQL(Data Query Language ) 数据查询语言,如对数据的查询。
DCL(Data Control Language) 数据控制语言,如事务的提交回滚,权限控制等。
1.now()是由mysql数据库提供的当系统时间函数。
2.char与varchar类型的区别:
char(2)是定长 对于性别男/女;
char保存数据时会去掉字符串末尾的空格。
varchar是变长字符串。
varchar字符串类型不能去掉末尾的空格。
例子比较:
mysql> insert into tb_char_varchar
values(cid,'string ','String ');
Query OK, 1 row affected, 2 warnings (0.00 sec)
mysql> select concat(",",t_char,",") from
tb_char_varchar;
+------------------------+
| concat(",",t_char,",") |
+------------------------+
| ,ab, |
| ,ab, |
| ,Abc, |
| ,stri, |
+------------------------+
4 rows in set (0.00 sec)
mysql> insert into tb_char_varchar
values(cid,'s ','St ');
Query OK, 1 row affected (0.00 sec)
mysql> select concat(",",t_char,",") from
tb_char_varchar;
+------------------------+
| concat(",",t_char,",") |
+------------------------+
| ,ab, |
| ,ab, |
| ,Abc, |
| ,stri, |
| ,s, |
+------------------------+
5 rows in set (0.00 sec)
mysql> select concat(",",t_varchar,",") from
tb_char_varchar;
+---------------------------+
| concat(",",t_varchar,",") |
+---------------------------+
| ,cd, |
| ,ef , |
| ,Cdf, |
| ,Stri, |
| ,St , |
+---------------------------+
5 rows in set (0.00 sec)
4.BLOB可以存放二进制数据。
Text:存放文本类型数据。
5.varchar 与char的空格存放问题!!
6.select abs(-9) from dual;
7. p' at line 3
mysql> create table tb_time(
-> tid int(12) not null auto_increment,
-> t_datetime datetime,
-> t_date date,
-> t_timestamp timestamp,
-> t_time time,
-> t_year year,
-> primary key(tid)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> desc tb_test;
+-------+--------------+------+-----+---------+---------
-------+
| Field | Type | Null | Key | Default | Extra
|
+-------+--------------+------+-----+---------+---------
-------+
| id | int(11) | NO | PRI | NULL |
auto_increment |
| num1 | int(11) | YES | | NULL |
|
| num2 | tinyint(127) | YES | | NULL |
|
+-------+--------------+------+-----+---------+---------
-------+
3 rows in set (0.00 sec)
mysql> desc tb_time;
+-------------+-----------+------+-----+----------------
---+-----------------------------+
| Field | Type | Null | Key | Default
| Extra |
+-------------+-----------+------+-----+----------------
---+-----------------------------+
| tid | int(12) | NO | PRI | NULL
| auto_increment |
| t_datetime | datetime | YES | | NULL
| |
| t_date | date | YES | | NULL
| |
| t_timestamp | timestamp | NO | |
CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| t_time | time | YES | | NULL
| |
| t_year | year(4) | YES | | NULL
| |
+-------------+-----------+------+-----+----------------
---+-----------------------------+
6 rows in set (0.00 sec)
mysql> insert into
tb_time(tid,t_datetime,t_date,t_timestamp,t_time,t_year)
-> values(tid,now(),now(),now(),now(),now());
Query OK, 1 row affected, 2 warnings (0.00 sec)
mysql> select * from tb_time;
+-----+---------------------+------------+--------------
-------+----------+--------+
| tid | t_datetime | t_date | t_timestamp
| t_time | t_year |
+-----+---------------------+------------+--------------
-------+----------+--------+
| 1 | 2016-06-17 14:11:32 | 2016-06-17 | 2016-06-17
14:11:32 | 14:11:32 | 2016 |
+-----+---------------------+------------+--------------
-------+----------+--------+
1 row in set (0.00 sec)
mysql> create table tb_char_varchar(
-> cid int(10) not null auto_increment,
-> t_char char(4),
-> t_varchar varchar(4),
-> primary key(cid)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> desc tb_char_varchar;
+-----------+------------+------+-----+---------+-------
---------+
| Field | Type | Null | Key | Default | Extra
|
+-----------+------------+------+-----+---------+-------
---------+
| cid | int(10) | NO | PRI | NULL |
auto_increment |
| t_char | char(4) | YES | | NULL |
|
| t_varchar | varchar(4) | YES | | NULL |
|
+-----------+------------+------+-----+---------+-------
---------+
3 rows in set (0.01 sec)
mysql> insert into tb_char_varchar(cid,t_char,t_varchar)
-> values(cid,'ab','cd');
Query OK, 1 row affected (0.00 sec)
mysql> select * from tb_char_varchar;
+-----+--------+-----------+
| cid | t_char | t_varchar |
+-----+--------+-----------+
| 1 | ab | cd |
+-----+--------+-----------+
1 row in set (0.00 sec)
mysql> select length(t_char),length(t_varchar) from
tb_char_varchar;
+----------------+-------------------+
| length(t_char) | length(t_varchar) |
+----------------+-------------------+
| 2 | 2 |
+----------------+-------------------+
1 row in set (0.00 sec)
mysql> insert into tb_char_varchar(cid,t_char,t_varchar)
-> values(cid,'ab ','ef ');
Query OK, 1 row affected (0.00 sec)
mysql> select * from tb_char_varchar;
+-----+--------+-----------+
| cid | t_char | t_varchar |
+-----+--------+-----------+
| 1 | ab | cd |
| 2 | ab | ef |
+-----+--------+-----------+
2 rows in set (0.00 sec)
mysql> insert into tb_char_varchar(
->
-> ;
ERROR 1064 (42000): You have an error in your SQL
syntax; check the manual that corresponds to your MySQL
server version for the right syn
mysql> insert into tb_char_varchar(cid,t_char,t_varchar)
values(cid,'Abc','Cdf');
Query OK, 1 row affected (0.00 sec)
mysql> select * from tb_char_varchar;
+-----+--------+-----------+
| cid | t_char | t_varchar |
+-----+--------+-----------+
| 1 | ab | cd |
| 2 | ab | ef |
| 3 | Abc | Cdf |
+-----+--------+-----------+
3 rows in set (0.01 sec)
mysql> select abs(-9);
+---------+
| abs(-9) |
+---------+
| 9 |
+---------+
1 row in set (0.00 sec)
mysql> show tables;
+-----------------------+
| Tables_in_zrcx_string |
+-----------------------+
| tb_char_varchar |
| tb_test |
| tb_time |
+-----------------------+
3 rows in set (0.00 sec)
mysql> select * from tb_test;
+----+------+------+
| id | num1 | num2 |
+----+------+------+
| 1 | -24 | NULL |
| 2 | 14 | NULL |
| 3 | NULL | 12 |
| 4 | -13 | -13 |
| 5 | 13 | 25 |
| 6 | 11 | 127 |
+----+------+------+
6 rows in set (0.01 sec)
mysql> select abs(num1) from tb_test;
+-----------+
| abs(num1) |
+-----------+
| 24 |
| 14 |
| NULL |
| 13 |
| 13 |
| 11 |
+-----------+
6 rows in set (0.00 sec)
mysql> select bin(8);
+--------+
| bin(8) |
+--------+
| 1000 |
+--------+
1 row in set (0.00 sec)
mysql> select oct(9);
+--------+
| oct(9) |
+--------+
| 11 |
+--------+
1 row in set (0.00 sec)
mysql> select hex(56);
+---------+
| hex(56) |
+---------+
| 38 |
+---------+
1 row in set (0.00 sec)
mysql>
8.ceiling(x)表示返回大于等于X的最小整数
mysql> select ceiling(2.89);
+---------------+
| ceiling(2.89) |
+---------------+
| 3 |
+---------------+
1 row in set (0.01 sec)
mysql> select ceiling(2.1);
+--------------+
| ceiling(2.1) |
+--------------+
| 3 |
+--------------+
1 row in set (0.00 sec)
mysql> select ceiling(-2.1);
+---------------+
| ceiling(-2.1) |
+---------------+
| -2 |
+---------------+
1 row in set (0.00 sec)
9.cmd 命令下退出
mysql> use zrcx_db;
Database changed
mysql> \q;
Bye
10.floor(X)表示返回小于X的最大整数值。
mysql> select floor(-2.89) ;
+--------------+
| floor(-2.89) |
+--------------+
| -3 |
+--------------+
1 row in set (0.00 sec)
11.EXP(x)返回值e的x次方 比如求e的3次幂。
mysql> select exp(2);
+------------------+
| exp(2) |
+------------------+
| 7.38905609893065 |
+------------------+
1 row in set (0.00 sec)
12.gretest()求集合当中的最大值。
mysql> select greatest(1,2,10,23,100,23,0,1);
+--------------------------------+
| greatest(1,2,10,23,100,23,0,1) |
+--------------------------------+
| 100 |
+--------------------------------+
1 row in set (0.00 sec)
13.least()求集合中的最小值。
14.LN(x)即e的多少次方。
15.LOG(x,y)返回x的以y为底的对数。
mysql> select log(4,16);
+-----------+
| log(4,16) |
+-----------+
| 2 |
+-----------+
1 row in set (0.00 sec)
16.mod(x,y)返回x/y的模。
17.rand()返回0到1之间的随机数。
1 row in set (0.00 sec)
mysql> select rand();
+-------------------+
| rand() |
+-------------------+
| 0.995950645763381 |
+-------------------+
1 row in set (0.00 sec)
mysql> select rand(1);
+-------------------+
| rand(1) |
+-------------------+
| 0.405403537121977 |
+-------------------+
1 row in set (0.00 sec)
18.round(x,y)表示返回x的四舍五入的保留y小数的数值。
19.sign(x)表示返回x的符号标记
mysql> select sign(-12);
+-----------+
| sign(-12) |
+-----------+
| -1 |
+-----------+
1 row in set (0.00 sec)
mysql> select sign(12);
+----------+
| sign(12) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
20.truncate(x,y)表示截断数值x保留y位小数的值。
mysql> select truncate(12.345678,4);
+-----------------------+
| truncate(12.345678,4) |
+-----------------------+
| 12.3456 |
+-----------------------+
1 row in set (0.00 sec)
3、表级别的操作
select * from emp;
-- 求列的平均值
select avg(comm) from emp;
select count(empno) from emp;
select min(sal) from emp;
-- 求 指定列中的最大值
select max(sal) from emp;
select sum(comm) from emp;
-- 求指定列的所有值的和
select sum(sal) from emp;
-- group by 分组 根据部门分组,然后求每个部门的人数
select count(empno)as countperson,deptno from emp group by deptno;
-- count()求个数,需要指定列名
-- 字符串函数
-- 求字符的ascii码值
select ASCII('A') from dual;
-- 返回字符串的比特长度
select BIT_LENGTH("abc") from dual;
-- 在数据库中字符串的拼接
select concat('li','ning','哈哈') from dual;
-- 可以使用间隔字符,拼接字符串
select CONCAT_WS("-","li","ning","困神") from dual;
-- (下标1开始)将字符串str从x位置开始替换,参数三替换的长度,参数四要替换的字符串
select INSERT("abcedfe",2,3,"ABC") from dual;
-- 下标从1开始,参数一在逗号分割列表中的位置
select FIND_IN_SET("a","f,b,c,d,a,f,d") from dual;
-- Lower(), lcase()将大写字符改变为小写
select Lower("abcDFa") from dual;
select lcase("abcDF") from dual;
-- 将小写转换为大写
select upper("abcDFdefd") from dual;
-- 返回字符串的长度
select length("dadjfdalf") from dual;
-- 返回字符串中最左边的n个字符
select LEFT("adbdfadf",3) from dual;
-- trim过滤字符串中前后的空格
select trim(" 李宁 ") from dual;
-- 返回子串在 字符串中的位置(从1开始)
select POSITION("b" IN "adfdsfbdfd") from dual;
--
select quote("你好") from dual;
-- 返回字符串str重复x次的结果
select REPEAT("adbdcfd",2) from dual;
select REPEAT(str,count)
-- 字符串逆序输出
select reverse("abcdef") from dual;
-- 比较两个字符串若所有的字符串均相同,则返回STRCMP(),
-- 若根据当前分类次序,第一个参数小于第二个,则返回 -1,其它情况返回 1
select STRCMP("abc","dfdsfda") from dual;
-- 日期函数和时间函数
-- 返回当前系统时间
select now() from dual;
-- 返回当前系统日期
select current_date() from dual;
-- 返回当前系统的当前时间
select current_time() from dual;
-- 返回日期加上间隔结果
select DATE_ADD(CURRENT_DATE(),INTERVAL 6 month) from dual
-- 返回指定格式的日期 (格式 特殊 %。。。)
select DATE_FORMAT(now(),"%m-%d-%y") from dual;
select FROM_UNIXTIME(CURRENT_TIME(),"%h:%i") from dual;
-- 返回时间中的小时数
select HOUR(now()) from dual;
select quarter(CURRENT_DATE()) from dual;
-- 数字的格式化 把x以逗号的方式进行格式化, 参数二代表的是小数位数
select FORMAT(1234,2) from dual;
-- 对时间的格式化
select TIME_FORMAT(now(),"%h-%i") from dual;
-- 返回ip地址所代表的数字
select inet_aton("127.0.0.1") from dual;
-- 返回数字所代表的ip地址
select INET_NTOA("2130706433") from dual;
-- 将日期转换为整型
SELECT CAST(NOW() AS SIGNED INTEGER),CURDATE()+0 from dual;
--
select found_rows() from dual;