1.1 mysql初步

API:Application Programming Interface,应用程序编程接口
ODBC:Open DateBase Connection

DBMS:DateBase Manage System,数据库管理系统
数据组织结构(逻辑结构)
1)层次结构
2)网状结构
3)关系结构
RDBMS:Relational DateBase Manage System,关系型数据库管理系统

用户视图
DBA视图
物理视图

RDBMS应该具有的功能
一、数据库建立、删除、修改
二、表建立、删除、修改
三、索引的建立、删除
四、用户和权限
五、数据增、删、改
六、查
相关命令
DML:Data Manipulate Language,数据操做语言
    INSERT,REPLACE,UPFATE,DELETE
DDL:Data Definition Language,数据定义语言
    CREATE,ALTER,DROP
DCL:Data Control Language,数据控制语言
    GRANT,REVOKE
SELECT

SQL:Structured Quiry Language
RDBMS:
    Oracle,Sybase,Infomix(被IBM收购)、SQL Server( 是Sybase的变种),DB2(IBM)
    MySQL,egrepSQL→PostgreSQL(pgsql)→ EnterpriseDB

Ali去IOE化,IBM,Oracle,EMC
综合软件提供服务商
IBM:CPU,AIX(Advanced IBM Unix),服务器,DB2
SUN:CPU,Solaris,服务器,MySQL,Java → 被Oracle收购

BEA:提供WebLogic
PeopleSoft:提供客户端管理软件

OpenOffice(SUN):被Oracle收购,私有化失败
LibreOffice(OpenOffice做者)另起炉灶

MariaDB,MySQL的做者继续开发

MySQL → (二次开发)Percona

非关系模型:NoSQL(一种技术)
    MongoDB:文档数据库
    Redis:缓存数据库
    HBase:基于键值的数据库,稀疏数据库

DBMS应具有的功能
数据管理的独立性;
有效存取数据;
校验数据完整性和安全性;
数据集中管理;
并发存储和故障恢复;
减小应用程序开发的时间。

SQL命令 → 分析器(分析SQL语法) → 计划执行器(有多少种方式能够完成任务) → 优化器 → 文件存取方法
 

MySQL
    Community Edition
    Enterprise Edtion,增长了备份功能
官方网站
MySQL软件包的格式
    软件包管理器特有的格式
          rpm,exe
    通用二进制格式
    源程序

客户端:mysql
服务端:mysqld
监听
tcp/3306

RDBMS数据位置
/var/lib/mysql

安装
    
    
    
    
yum install mysql-server

初始化
service mysqld start
首次启动,完成对数据库内部元数据的初始化。
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h hiyang.com password 'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!

mysql
-u USERNAME,默认root
-p,默认为空
-h MYSER_SERVER,默认localhost
-h 127.0.0.1
Linux: socket
Windows: memory

mysql客户端:
交互式模式
批处理模式
执行mysql脚本

交互式模式中的命令类别:
客户端命令
服务器端命令
必须使用语句结束符,默认为分号;

SQL接口:
Oracle, PL/SQL
SQL Server, T-SQL

关系型数据库对象
    表
    索引
    视图(虚表)
    约束
    存储过程
    存储函数
    触发器
    游标
    用户
    权限
    事务

表:
    行(row),列(field,column)
    表:实体
字段名称,数据类型(强类型),类型修饰符(限制)



占空间 范围 备注

     字符型
不区分大小写
CHAR(n)
n字节
255

VARCHAR(n)
n+1多结束修饰符
65535
变长
区分大小写
BINARY(n)
n字节


VARBINARY(n)
n+1字节

变长
不区分大小写 TEXT(n)
n+2字节 65535
区分大小写 BLOB(n)

65535
binary large object




    数值







整型


TINYINT
1字节
256

SMALLINT
2字节
65535

MEDIUMINT
3字节


INT
4字节

 NOT NULL
BIGINT
8字节

UNSIGNED,无符号
DECIMAL


十进制
近似数值

FLOAT
4字节


DOUBLE
8字节


                   日期时间

DATE



TIME



DATETIME



TIMESTAMP



    布尔





    内置

枚举
ENUM


ENUM(‘M’,‘F’)
集合
SET


SET(‘M’,‘F’)
建立数据库
create database [ if not exists ] db_name;
删除数据库
drop database [ if not exists ] db_name;
查看库中的表
show tables from db_name;

建立表
create table tb_name(col1,col2,...)
     
     
     
     
mysql> create table student(name char(10) not null,age tinyint unsigned,gender char(1) not null);
查看表的结构
desc tb_name;
删除表
drop table tb_name;

修改表
alter table tb_name 
    modif      修正field的属性
mysql> alter table student modify course varchar(50);
change   改变field的名称
mysql> alter table student change course lesson varchar(50);
    add    增长field
mysql> alter table student add high int after age;
mysql> alter table student add course varchar(100);
    drop 删除field
mysql> alter table student drop lesson;
对数据的操做
插入数据
insert into tb_name (col1,col2,...) values|value ('STRING', NUM,...);
insert  into  tb_name (col1,col2,...) values|value ('STRING', NUM,...),   ('STRING', NUM,...),... ;
在指定字段插入数据
mysql> insert into student (name,gender) value ('Li','M'),('Yang','F');
mysql> select * from student;
+------+------+------+--------+--------+
| name | age  | high | gender | lesson |
+------+------+------+--------+--------+
| Li   | NULL | NULL | M      | NULL   |
| Yang | NULL | NULL | F      | NULL   |
+------+------+------+--------+--------+    
不指定字段,使用默认字段
mysql> insert into student value ('Zhang',26,162,'M','food');
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+-------+------+------+--------+--------+
| name  | age  | high | gender | lesson |
+-------+------+------+--------+--------+
| Li    | NULL | NULL | M      | NULL   |
| Yang  | NULL | NULL | F      | NULL   |
| Zhang |   26 |  162 | M      | food   |
+-------+------+------+--------+--------+
修改数据
update tb_name set column=value WHERE  
mysql> update student set high=178 where name='yang';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
mysql> select * from student;
+-------+------+------+--------+--------+
| name  | age  | high | gender | lesson |
+-------+------+------+--------+--------+
| Li    | NULL | NULL | M      | NULL   |
| Yang  | NULL |  178 | F      | NULL   |
| Zhang |   26 |  162 | M      | food   |
+-------+------+------+--------+--------+
删除数据
delete from tb_name where CONDITION;
mysql> delete from student where name='Li';
选择
SELECT 字段 FROM tb_name WHERE CONDITION
*: 全部字段
不指定WHERE:表示显示全部行;
mysql> select name,high from student where lesson='food';
+-------+------+
| name  | high |
+-------+------+
| Zhang |  162 | 
建立用户
create user 'username'@'host' [identified by 'password'];
删除用户

drop user 'username'@'host';
php

HOST:
IP:
HOSTNAME:
NETWORK:
通配符
_:匹配任意单个字符, 172.16.0._
%:匹配任意字符;

DCL:
受权用户
    grant pri1,pri2,... on db_name.tb_name to 'username'@'host' [identified by 'password'];不存在的话直接建立并受权
取消受权
    revoke pri1,pri2,... on db_name.tb_name from 'username'@'host';
查看用户的受权
show grants for 'username'@'host';   
ALL PRIVILEGES

添加用户密码后才能有登陆权限
mysql> create user 'jerry'@'%';
mysql> show grants for 'jerry'@'$';
ERROR 1141 (42000): There is no such grant defined for user 'jerry' on host '$'
 
mysql> create user 'tom'@'%' identified by 'tom';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'tom'@'%';
+---------------------------------------------------------------------------+
| Grants for tom@%                                                          |
+---------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'%' IDENTIFIED BY PASSWORD '675bd1463e544441' |
+---------------------------------------------------------------------------+

为用户设定密码:
一、mysql>set password for 'username'@'host'=password('password');   
 
二、# mysqladmin    -uusername -hhost -p password 'password'
 
三、mysql> update user set password= password('password')  where user='root' and host='127.0.0.1';
蓝色的password为函数
select User,Host,Password from user;
+-------+--------------+------------------+
| User  | Host         | Password         |
+-------+--------------+------------------+
| root  | localhost    | 565491d704013245 |
| root  | hiyang.com   | 565491d704013245 |
| root  | 127.0.0.1    |                  |
|       | localhost    |                  |
|       | hiyang.com   |                  |
| jerry | %            |                  |
| tom   | %            | 675bd1463e544441 |
| root  | 192.168.8.40 | 565491d704013245 |
+-------+--------------+------------------+     

mysql图形化客户端工具
一、phpMyAdmin
二、Workbench
三、MySQL Front
四、Navicat for MySQL
五、Toad

# yum install php53-php
测试php与mysql通讯
<?php
$connection=mysql_connect('localhost','root','123456');
if ($connection)
        echo "sucess...";
else
        echo "die..";
?>
phpmyadmin
将下载的文件解压到DocumentRoot下,在浏览器用路径访问便可

论坛
discuz,phpwind,phpbb
CMS快速建站工具
drupal,joomla

wordpress
DefaultChar UTF-8



相关文章
相关标签/搜索