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應該具備的功能
1、數據庫創建、刪除、修改
2、表創建、刪除、修改
3、索引的創建、刪除
4、用戶和權限
5、數據增、刪、改
6、查
相關命令
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

安裝
  1. 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,...)
  1. 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';

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' |
+---------------------------------------------------------------------------+

爲用戶設定密碼:
1、mysql>set password for 'username'@'host'=password('password');   
 
2、# mysqladmin    -uusername -hhost -p password 'password'
 
3、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圖形化客戶端工具
1、phpMyAdmin
2、Workbench
3、MySQL Front
4、Navicat for MySQL
5、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





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章