|
由于新手入門PHP+Mysql技術,必定對Mysql操作存有疑問。 盡管PHPMyadmin管理數(shù)據(jù)庫對新手來說也不妨為一種很好的方式。但更多的實際使用還要靠大家對Myql語句格式的精通和熟練的操作。 為了方便新手盡快入門,掌握Mysql的奧妙。在下費了一個禮拜時間的調試和整理,終于完稿了這份比較完善的入門級操作實例的指南。希望能給新入門的朋友揚帆指路,送一滿帆的順風。 Mysql初級解讀 安裝注意事項 在安裝的過程中,請記好您的密碼,這是將來登錄mysql的鑰匙。 一、Mysql數(shù)據(jù)庫服務器登錄 登錄mysql需要切換到dos下的mysql的bin目錄,進行如下操作: 語法格式:mysql -h host_name -u user_name -p password (本機的話-h 和host_name可省,即:mysql -u root -p) My Experience: C:program filesmysqlmysql server 4.1bin>mysql -u root -p Enter password:****** 先輸入用戶名和密碼登陸要求(加上-p),回車后等出現(xiàn)"Enter password:",再輸入密碼回車,這樣就可以 成功登陸mysql,否則將會登陸失敗。 登陸成功后會顯示如下信息: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 1 to server version: 4.1.10-nt Type 'help;' or 'h' for help. Type 'c' to clear the buffer. mysql> 標識"mysql>",當你看到這個出現(xiàn)在窗口的最左邊的頂格時,這就是告訴你,你可以輸入命令進行操作了。 mysql> s 查看版本信息 mysql> q or mysql> quit 退出mysql數(shù)據(jù)庫服務器 mysql> h or mysql> help 查看幫助(其他的數(shù)據(jù)庫服務器相關命令) doc 下停止mysql: net stop mysql doc 下啟動mysql : net start mysql 二、數(shù)據(jù)庫操作 所要注意的是,所有舉例的顯示結果,均為下面操作步驟按順序進行的結果。 2.1 查詢語句 2.1.1 查看Mysql數(shù)據(jù)庫的版本號和服務器的當前日期 mysql> select version(),current_date; (操作方式一) mysql> select version() -> ,current_date; (操作方式二) ※:操作語句間用","隔開,用";"來表示操作結束,操作語句輸入過程中,換行不影響操作過程。 2.1.2 查看服務器中的所有數(shù)據(jù)庫 mysql> show databases; 2.1.3 顯示MySQL數(shù)據(jù)庫里表的概要 mysql> show table status from 數(shù)據(jù)庫名G 2.2 創(chuàng)建數(shù)據(jù)庫 2.2.1 創(chuàng)建數(shù)據(jù)庫(當然,數(shù)據(jù)庫名"asb"不能是已經(jīng)存在的) mysql> create database asb; 2.2.2 選用數(shù)據(jù)庫,使其成為當前操作的數(shù)據(jù)庫 mysql> use asb; 成功選中后會有如下顯示: Database changed 甚至可以在窗口命令登陸mysql數(shù)據(jù)庫的時候直接選中要操作的數(shù)據(jù)庫(當然前提是asb數(shù)據(jù)庫存在) bin>mysql asb -u uesername -p Enter password:****** 2.2.3 查看當前所操作的數(shù)據(jù)庫名稱 mysql> select database(); 運行后結果如下: +------------+ | database() | +------------+ | asb | +------------+ 1 row in set (0.00 sec) 2.3 創(chuàng)建表 2.3.1 用"create table"創(chuàng)建表(確認是要在當前數(shù)據(jù)庫中創(chuàng)建,不是的話就先用'use'選中要創(chuàng)建表的那個數(shù)據(jù)庫) mysql> create table emp(emp_id varchar(6) -> ,emp_name varchar(10) -> ,emp_age int -> ,emp-sal int -> ,emp_bir date -> ,emp_sex varchar(5) -> ); 2.3.2 查看當前數(shù)據(jù)庫中的表(可以檢驗上例的"emp"表是否成功建立) mysql> show tables; 如果是成功將是如下顯示: +---------------+ | Tables_in_asb | +---------------+ | emp | +---------------+ 1 row in set (0.00 sec) 2.3.3 查看數(shù)據(jù)表的結構 mysql> describe emp; 運行后結果會如如下顯示: +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | emp_id | varchar(6) | YES | | NULL | | | emp_name | varchar(10) | YES | | NULL | | | emp_age | int(11) | YES | | NULL | | | emp_sal | int(11) | YES | | NULL | | | emp_bir | date | YES | | NULL | | | emp_sex | varchar(5) | YES | | NULL | | 2.4 插入數(shù)據(jù) 2.4.1 使用INSERT語句進行插入記錄操作(數(shù)據(jù)庫表中每一行就是一個記錄,插入記錄實際上就是向表中插入一行) 格式:INSERT INTO table_name(數(shù)據(jù)表名) VALUES(值1,值2,值3...); 在VALUES關鍵字后括號中的數(shù)值和字符段,必須和你所建的表所定義的字段數(shù)據(jù)類型必須一致~ mysql> insert into emp values -> ('100005','嘯天',27,3000,'1979-07-10','male'); 插入成功后會有如下信息顯示: Query OK, 1 row affected (0.03 sec) 2.4.2 查看表中的數(shù)據(jù)(可以確定數(shù)據(jù)是否已經(jīng)成功插入) mysql> select * from emp; 成功插入后的數(shù)據(jù)顯示如下: +--------+----------+---------+---------+------------+---------+ | emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex | +--------+----------+---------+---------+------------+---------+ | 100005 | 嘯天 | 27 | 3000 | 1979-07-10 | male | +--------+----------+---------+---------+------------+---------+ 1 row in set (0.00 sec) 一次性插入多行的格式:INSERT INTO table_name(數(shù)據(jù)表名) VALUES(值1,值2,值3...),(值1,值2,值3...); mysql> insert into emp values ('100001','紅楓',29,8000,'1977-01-01','male'), ('100002','麗鵑',27,7000,'1979-12-31','fmale'); 可以查看插入后的數(shù)據(jù)結果: mysql> select * from emp; +--------+----------+---------+---------+------------+---------+ | emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex | +--------+----------+---------+---------+------------+---------+ | 100005 | 嘯天 | 27 | 3000 | 1979-07-10 | male | | 100004 | 紅楓 | 29 | 8000 | 1977-01-01 | male | | 100002 | 麗鵑 | 27 | 7000 | 1979-12-31 | fmale | +--------+----------+---------+---------+------------+---------+ 3 rows in set (0.00 sec) 2.5 修改數(shù)據(jù) 2.5.1 使用UPDATE語句來更新表中的數(shù)據(jù) mysql> update emp set emp_id=100001 where emp_name='紅楓'; 修改結果顯示如下: +--------+----------+---------+---------+------------+---------+ | emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex | +--------+----------+---------+---------+------------+---------+ | 100005 | 嘯天 | 27 | 3000 | 1979-07-10 | male | | 100001 | 紅楓 | 29 | 8000 | 1977-01-01 | male | | 100002 | 麗鵑 | 27 | 7000 | 1979-12-31 | fmale | +--------+----------+---------+---------+------------+---------+ 3 rows in set (0.00 sec) 如果該語句沒有后面的where限制,將會對表中所有的記錄都進行修改。 如,給全部人加薪1000,可以如下修改: mysql> update emp set emp_sal=emp_sal+1000; 修改結果顯示如下: +--------+----------+---------+---------+------------+---------+ | emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex | +--------+----------+---------+---------+------------+---------+ | 100005 | 嘯天 | 27 | 4000 | 1979-07-10 | male | | 100001 | 紅楓 | 29 | 9000 | 1977-01-01 | male | | 100002 | 麗鵑 | 27 | 8000 | 1979-12-31 | fmale | +--------+----------+---------+---------+------------+---------+ 3 rows in set (0.00 sec) 2.6 高級查詢方法 2.6.1 記錄查詢 查詢emp表中,emp_name為嘯天的全部信息 mysql> select * from emp where emp_name='嘯天'; 查詢結果顯示如下: +--------+----------+---------+---------+------------+---------+ | emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex | +--------+----------+---------+---------+------------+---------+ | 100005 | 嘯天 | 27 | 4000 | 1979-07-10 | male | +--------+----------+---------+---------+------------+---------+ 1 row in set (0.00 sec) 查詢emp表中,emp_sal,工資在5000以上的全部信息 mysql> select * from emp where emp_sal>5000; 查詢結果顯示如下: +--------+----------+---------+---------+------------+---------+ | emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex | +--------+----------+---------+---------+------------+---------+ | 100001 | 紅楓 | 29 | 9000 | 1977-01-01 | male | | 100002 | 麗鵑 | 27 | 8000 | 1979-12-31 | fmale | +--------+----------+---------+---------+------------+---------+ 2 rows in set (0.00 sec) 查詢emp表中在1978年1月1日之后出生的 mysql> select * from emp where emp_bir>'1978-01-01'; 查詢結果顯示如下: +--------+----------+---------+---------+------------+---------+ | emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex | +--------+----------+---------+---------+------------+---------+ | 100005 | 嘯天 | 27 | 4000 | 1979-07-10 | male | | 100002 | 麗鵑 | 27 | 8000 | 1979-12-31 | fmale | +--------+----------+---------+---------+------------+---------+ 2 rows in set (0.00 sec) 查詢emp表中在1979年12月1日之前出生,工資在5000以上的 mysql> select * from emp where emp_bir<'1979-12-01' and emp_sal>5000; 查詢結果顯示如下: +--------+----------+---------+---------+------------+---------+ | emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex | +--------+----------+---------+---------+------------+---------+ | 100001 | 紅楓 | 29 | 9000 | 1977-01-01 | male | +--------+----------+---------+---------+------------+---------+ 1 row in set (0.00 sec) 2.6.2 字段查詢 CEO查看員工工資情況 mysql> select emp_name,emp_sal from emp; 查詢結果顯示如下: +----------+---------+ | emp_name | emp_sal | +----------+---------+ | 嘯天 | 4000 | | 紅楓 | 9000 | | 麗鵑 | 8000 | +----------+---------+ 3 rows in set (0.00 sec) 查看1978年后出生的人的姓名、工資和性別 mysql> select emp_name,emp_sal,emp_sex from emp where emp_bir>"1977-12-31"; 查詢結果顯示如下: +----------+---------+---------+ | emp_name | emp_sal | emp_sex | +----------+---------+---------+ | 嘯天 | 4000 | male | | 麗鵑 | 8000 | fmale | +----------+---------+---------+ 2 rows in set (0.00 sec) 2.6.3 查詢結果排序 用ORDER BY語句對emp表中所有員工工資高低順序查詢結果(默認是從低到高——升序) mysql> select * from emp order by emp_sal; 查詢結果顯示如下: +--------+----------+---------+---------+------------+---------+ | emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex | +--------+----------+---------+---------+------------+---------+ | 100005 | 嘯天 | 27 | 4000 | 1979-07-10 | male | | 100002 | 麗鵑 | 27 | 8000 | 1979-12-31 | fmale | | 100001 | 紅楓 | 29 | 9000 | 1977-01-01 | male | +--------+----------+---------+---------+------------+---------+ 3 rows in set (0.00 sec) 用DESC關鍵字來進行從高到低排序——降序 mysql> select * from emp order by emp_sal desc; 查詢結果顯示如下: +--------+----------+---------+---------+------------+---------+ | emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex | +--------+----------+---------+---------+------------+---------+ | 100001 | 紅楓 | 29 | 9000 | 1977-01-01 | male | | 100002 | 麗鵑 | 27 | 8000 | 1979-12-31 | fmale | | 100005 | 嘯天 | 27 | 4000 | 1979-07-10 | male | +--------+----------+---------+---------+------------+---------+ 3 rows in set (0.00 sec) 2.6.4 查詢結果數(shù)量的限制 用LIMIT查看emp表中工資收入排名前兩個員工的資料: mysql> select * from emp order by emp_sal desc limit 2; 查詢結果顯示如下: +--------+----------+---------+---------+------------+---------+ | emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex | +--------+----------+---------+---------+------------+---------+ | 100001 | 紅楓 | 29 | 9000 | 1977-01-01 | male | | 100002 | 麗鵑 | 27 | 8000 | 1979-12-31 | fmale | +--------+----------+---------+---------+------------+---------+ 2 rows in set (0.00 sec) 查看工資排名第2到第3的員工資料: mysql> select * from emp order by emp_sal desc limit 1,2; 查詢結果顯示如下: +--------+----------+---------+---------+------------+---------+ | emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex | +--------+----------+---------+---------+------------+---------+ | 100002 | 麗鵑 | 27 | 8000 | 1979-12-31 | fmale | | 100005 | 嘯天 | 27 | 4000 | 1979-07-10 | male | +--------+----------+---------+---------+------------+---------+ 2 rows in set (0.01 sec) 使用rand()抽樣調查,隨機抽取2個員工,查看其資料 mysql> select * from emp order by rand() limit 2; 如如下結果:(隨機的) +--------+----------+---------+---------+------------+---------+ | emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex | +--------+----------+---------+---------+------------+---------+ | 100005 | 嘯天 | 27 | 4000 | 1979- 07-10 | male | | 100001 | 紅楓 | 29 | 9000 | 1977-01-01 | male | +--------+----------+---------+---------+------------+---------+ 2 rows in set (0.01 sec) 2.6.5 查詢結果的字段聯(lián)合和重新命名 mysql> select concat(emp_id," ",emp_name) from emp; 查詢結果: +------------------------------+ | concat(emp_id," ",emp_name) | +------------------------------+ | 100005 嘯天 | | 100001 紅楓 | | 100002 麗鵑 | +------------------------------+ 3 rows in set (0.00 sec) 用AS關鍵字重新給輸出結果命名標題 mysql> select concat(emp_id," ",emp_name) as info from emp; 查詢結果如下顯示: +----------------+ | info | +----------------+ | 100005 嘯天 | | 100001 紅楓 | | 100002 麗鵑 | +----------------+ 3 rows in set (0.00 sec) 2.6.6 日期查詢的相關運算 可以通過YEAR()、MONTH()、DAYOFMONTH()函數(shù)來提取日期的組成元素 查詢7月份出生的員工資料: mysql> select * from emp where month(emp_bir)=7; 查詢結果顯示如下: +--------+----------+---------+---------+------------+---------+ | emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex | +--------+----------+---------+---------+------------+---------+ | 100005 | 嘯天 | 27 | 4000 | 1979-07-10 | male | +--------+----------+---------+---------+------------+---------+ 1 row in set (0.00 sec) 可以利用英文月份來查詢: mysql> select * from emp where monthname(emp_bir)="January"; 查詢結果顯示如下: +--------+----------+---------+---------+------------+---------+ | emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex | +--------+----------+---------+---------+------------+---------+ | 100001 | 紅楓 | 29 | 9000 | 1977-01-01 | male | +--------+----------+---------+---------+------------+---------+ 1 row in set (0.00 sec) 利用TO_DAYS()函數(shù)可以查詢出職工們從出生到現(xiàn)在所經(jīng)歷的時間,單位是天數(shù) mysql> select to_days(current_date) - to_days(emp_bir) as livingdays from emp; 查詢后結果如下: +------------+ | livingdays | +------------+ | 9425 | | 10345 | | 9251 | +------------+ 3 rows in set (0.00 sec) 計算從現(xiàn)在開始經(jīng)歷100天后的日期 mysql> select date_add(now(),interval 100 day); 查詢結果如下: +----------------------------------+ | date_add(now(),interval 100 day) | +----------------------------------+ | 2005-08-07 13:56:58 | +----------------------------------+ 1 row in set (0.00 sec) 計算從現(xiàn)在開始經(jīng)歷100天前的日期 mysql> select date_sub(now(),interval 100 day); 查詢結果如下: +----------------------------------+ | date_sub(now(),interval 100 day) | +----------------------------------+ | 2005-01-19 14:00:20 | +----------------------------------+ 1 row in set (0.00 sec) 2.6.7 數(shù)據(jù)統(tǒng)計 使用COUNT()函數(shù)計算表中的數(shù)據(jù)數(shù)目(比如emp表中的員工數(shù)目) mysql> select count(*) from emp; 查詢結果如下: +----------+ | count(*) | +----------+ | 3 | +----------+ 1 row in set (0.01 sec) 統(tǒng)計工資上5000的數(shù)目 mysql> select count(*) from emp where emp_sal>5000; 查詢結果如下: +----------+ | count(*) | +----------+ | 2 | +----------+ 1 row in set (0.00 sec) 統(tǒng)計男女職工數(shù)目:(GROUP BY語句分類) mysql> select emp_sex,count(*) from emp group by emp_sex; 查詢結果如下: +---------+----------+ | emp_sex | count(*) | +---------+----------+ | fmale | 1 | | male | 2 | +---------+----------+ 2 rows in set (0.01 sec) 使用數(shù)據(jù)統(tǒng)計函數(shù)(MIN(),MAX(),SUM(),AVG()) mysql> select -> min(emp_sal) as min_salary, -> max(emp_sal) as max_salary, -> sum(emp_sal) as sum_salary, -> avg(emp_sal) as avg_salary, -> count(*) as employee_num -> from emp; 查詢結果如下: +------------+------------+------------+------------+--------------+ | min_salary | max_salary | sum_salary | avg_salary | employee_num | +------------+------------+------------+------------+--------------+ | 4000 | 9000 | 21000 | 7000.0000 | 3 | +------------+------------+------------+------------+--------------+ 1 row in set (0.00 sec) 2.6.8 從多個數(shù)據(jù)表中檢索信息 根據(jù)前面的方法,分別進行如下操作: 1). 在數(shù)據(jù)庫asb中建立一個新表dept,表中有兩項元素: dept_id --> varchar(6) dept_name --> varchar(10) 2). 在表emp中插入如下一行新記錄: +--------+----------+---------+---------+------------+---------+ | emp_id | emp_name | emp_age | emp_sal | emp_bir | emp_sex | +--------+----------+---------+---------+------------+---------+ | 100003 | 小紅 | 30 | 8000 | 1976-11-11 | fmale | +--------+----------+---------+---------+------------+---------+ 3). 在新表dept中,輸入如下記錄 +---------+-----------+ | dept_id | dept_name | +---------+-----------+ | 100005 | MTD | | 100001 | MTD | | 100002 | MTD | | 100003 | HR | +---------+-----------+ 查詢emp和dept這兩個表中,員工的姓名和部門信息 mysql> select emp.emp_name,dept.dept_name from emp,dept -> where emp.emp_id=dept.dept_id; 查詢結果如下: +----------+-----------+ | emp_name | dept_name | +----------+-----------+ | 嘯天 | MTD | | 紅楓 | MTD | | 麗鵑 | MTD | | 小紅 | HR | +----------+-----------+ 4 rows in set (0.00 sec) 多表查詢時注意: 1). FROM子句必須給出所查詢的表的全部名稱 2). 選擇字段時候注明其所屬表的名稱(如emp表中的emp_id要表示為emp.emp_id) 3). 在Where子句中必須指明查詢的條件(如,emp.emp_id和dept.dept_id是相同意義的元素) 2.7 刪除表單數(shù)據(jù) 2.7.1 使用DELETE語句刪除表單中的數(shù)據(jù)記錄 小紅不在了哦,刪了吧 mysql>delete from emp where emp_name='小紅'; 執(zhí)行成功后會如下顯示: Query OK, 1 row affected (0.06 sec) 省略where是刪除全部表中的記錄 2.7.2 使用DRO刪除表 (先隨便建立一個數(shù)據(jù)庫dt,并建張臨時表fuck) mysql>drop table abc; 2.7.3 使用DRO刪除數(shù)據(jù)庫 mysql>drop database dt; 2.8 改變數(shù)據(jù)表的結構 先建立一個新表id (內帶一個屬性id_name varchar(6)),輸入一行數(shù)據(jù)(xgw) 2.8.1 對表重新命名 mysql>alter table 數(shù)據(jù)表名 rename as 數(shù)據(jù)表的新名字; 把表id改名成name mysql> alter table id rename as name; 2.8.2 給數(shù)據(jù)表增加一個字段 mysql>alter table 數(shù)據(jù)表名 add 字段名稱 字段類型; 在改過名的新表name中增加一個字段(id int(6)) mysql>alter table name add id int(6); 增加成功后有如下顯示: Query OK, 1 row affected (0.26 sec) Records: 1 Duplicates: 0 Warnings: 0 2.8.3 更改已經(jīng)建立的字段類型 alter table 數(shù)據(jù)表名 modify 字段名稱 字段類型; 把name表中id屬性的類型改成10個長度的字符類型 mysql> alter table name modify id varchar(10);
信息發(fā)布:廣州名易軟件有限公司 http://www.jetlc.com
|