1、安裝環(huán)境:
Windows XP
Mysql 4.0.17 從 sql.com">http://www.mysql.com下載
EMS Mysql Query 1.6.0.1
本機(jī)IP:172.5.1.183
2、安裝Mysql,采用默認(rèn)安裝即可
目錄選擇在c:\mysql,不要修改默認(rèn)目錄
3、啟動(dòng)Mysql
安裝為服務(wù):c:\mysql\bin\mysqld --install
啟動(dòng)Mysql: net start mysql
停止Mysql: net stop mysql
4、系統(tǒng)創(chuàng)建的數(shù)據(jù)庫有mysql,test
mysql保存系統(tǒng)數(shù)據(jù)
test數(shù)據(jù)庫用來測試
5、默認(rèn)登陸方式
在本機(jī)
c:\mysql\bin\mysql -uroot
c:\mysql\bin\mysql
遠(yuǎn)程
mysql -h 172.5.1.183 -uroot
這些是在mysql.user表中,系統(tǒng)默認(rèn)存在4條數(shù)據(jù)
use mysql
select host,user,password from user;
+-----------+------+----------+
host user password
+-----------+------+----------+
localhost root
% root
localhost
%
+-----------+------+----------+
這些數(shù)據(jù)代表的意義:
用戶名為root,密碼為空的用戶可以從本機(jī)和任何遠(yuǎn)程主機(jī)登陸
任何用戶名,密碼為空的用戶可以從本機(jī)登陸
用戶名為空,密碼為空的用戶不可以從遠(yuǎn)程登陸 (user中后面的字段為N,所以無法登陸)
修改root密碼
mysql -uroot
use mysql
update user set password=PASSWORD("root") where user=’root’ and host=’localhost’
下次就需要用mysql -uroot -proot才可以登陸
在遠(yuǎn)程或本機(jī)可以使用 mysql -h 172.5.1.183 -uroot 登陸,這個(gè)根據(jù)第二行的策略確定
權(quán)限修改生效:
1)net stop mysql
net start mysql
2)c:\mysql\bin\mysqladmin flush-privileges
3)登陸mysql后,用flush privileges語句
6、創(chuàng)建數(shù)據(jù)庫staffer
create database staffer;
7、下面的語句在mysql環(huán)境在執(zhí)行
顯示用戶擁有權(quán)限的數(shù)據(jù)庫 show databases;
切換到staffer數(shù)據(jù)庫 use staffer;
顯示當(dāng)前數(shù)據(jù)庫中有權(quán)限的表 show tables;
顯示表staffer的結(jié)構(gòu) desc staffer;
8、創(chuàng)建測試環(huán)境
1)創(chuàng)建數(shù)據(jù)庫staffer
mysql> create database staffer
2)創(chuàng)建表staffer,department,position,depart_pos
create table s_position
(
id int not null auto_increment,
name varchar(20) not null default ’經(jīng)理’, #設(shè)定默認(rèn)值
description varchar(100),
primary key PK_positon (id) #設(shè)定主鍵
);
create table department
(
id int not null auto_increment,
name varchar(20) not null default ’系統(tǒng)部’, #設(shè)定默認(rèn)值
description varchar(100),
primary key PK_department (id) #設(shè)定主鍵
);
create table depart_pos
(
department_id int not null,
position_id int not null,
primary key PK_depart_pos (department_id,position_id) #設(shè)定復(fù)和主鍵
);
create table staffer
(
id int not null auto_increment primary key, #設(shè)定主鍵
name varchar(20) not null default ’無名氏’, #設(shè)定默認(rèn)值
department_id int not null,
position_id int not null,
unique (department_id,position_id) #設(shè)定唯一值
);
3)刪除
mysql>
drop table depart_pos;
drop table department;
drop table s_position;
drop table staffer;
drop database staffer;
9、修改結(jié)構(gòu)
mysql>
#表position增加列test
alter table position add(test char(10));
#表position修改列test
alter table position modify test char(20) not null;
#表position修改列test默認(rèn)值
alter table position alter test set default ’system’;
#表position去掉test默認(rèn)值
alter table position alter test drop default;
#表position去掉列test
alter table position drop column test;
#表depart_pos刪除主鍵
alter table depart_pos drop primary key;
#表depart_pos增加主鍵
alter table depart_pos add primary key PK_depart_pos (department_id,position_id);
10、操作數(shù)據(jù)
#插入表department
insert into department(name,description) values(’系統(tǒng)部’,’系統(tǒng)部’);
insert into department(name,description) values(’公關(guān)部’,’公關(guān)部’);
insert into department(name,description) values(’客服部’,’客服部’);
insert into department(name,description) values(’財(cái)務(wù)部’,’財(cái)務(wù)部’);
insert into department(name,description) values(’測試部’,’測試部’);
#插入表s_position
insert into s_position(name,description) values(’總監(jiān)’,’總監(jiān)’);
insert into s_position(name,description) values(’經(jīng)理’,’經(jīng)理’);
insert into s_position(name,description) values(’普通員工’,’普通員工’);
#插入表depart_pos
insert into depart_pos(department_id,position_id)
select a.id department_id,b.id postion_id
from department a,s_position b;
#插入表staffer
insert into staffer(name,department_id,position_id) values(’陳達(dá)治’,1,1);
insert into staffer(name,department_id,position_id) values(’李文賓’,1,2);
insert into staffer(name,department_id,position_id) values(’馬佳’,1,3);
insert into staffer(name,department_id,position_id) values(’亢志強(qiáng)’,5,1);
insert into staffer(name,department_id,position_id) values(’楊玉茹’,4,1);
11、查詢及刪除操作
#顯示系統(tǒng)部的人員和職位
select a.name,b.name department_name,c.name position_name
from staffer a,department b,s_position c
where a.department_id=b.id and a.position_id=c.id and b.name=’系統(tǒng)部’;
#顯示系統(tǒng)部的人數(shù)
select count(*) from staffer a,department b
where a.department_id=b.id and b.name=’系統(tǒng)部’
#顯示各部門的人數(shù)
select count(*) cou,b.name
from staffer a,department b
where a.department_id=b.id
group by b.name;
#刪除客服部
delete from department where name=’客服部’;
#將財(cái)務(wù)部修改為財(cái)務(wù)一部
update department set name=’財(cái)務(wù)一部’ where name=’財(cái)務(wù)部’;
12、備份和恢復(fù)
備份數(shù)據(jù)庫staffer
c:\mysql\bin\mysqldump -uroot -proot staffer>e:\staffer.sql
得到的staffer.sql是一個(gè)sql腳本,不包括建庫的語句,所以你需要手工
創(chuàng)建數(shù)據(jù)庫才可以導(dǎo)入
恢復(fù)數(shù)據(jù)庫staffer,需要?jiǎng)?chuàng)建一個(gè)空庫staffer
c:\mysql\bin\mysql -uroot -proot staffer<staffer.sql
如果不希望后來手工創(chuàng)建staffer,可以
c:\mysql\bin\mysqldump -uroot -proot --databases staffer>e:\staffer.sql
mysql -uroot -proot >e:\staffer.sql
但這樣的話系統(tǒng)種就不能存在staffer庫,且無法導(dǎo)入其他名字的數(shù)據(jù)庫,
當(dāng)然你可以手工修改staffer.sql文件
13、從文本向數(shù)據(jù)庫導(dǎo)入數(shù)據(jù)
1)使用工具c:\mysql\bin\mysqlimport
這個(gè)工具的作用是將文件導(dǎo)入到和去掉文件擴(kuò)展名名字相同的表里,如
staffer.txt,staffer都是導(dǎo)入到staffer表中
常用選項(xiàng)及功能如下
-d or --delete 新數(shù)據(jù)導(dǎo)入數(shù)據(jù)表中之前刪除數(shù)據(jù)數(shù)據(jù)表中的所有信息
-f or --force 不管是否遇到錯(cuò)誤,mysqlimport將強(qiáng)制繼續(xù)插入數(shù)據(jù)
-i or --ignore mysqlimport跳過或者忽略那些有相同唯一
關(guān)鍵字的行, 導(dǎo)入文件中的數(shù)據(jù)將被忽略。
-l or -lock-tables 數(shù)據(jù)被插入之前鎖住表,這樣就防止了,
你在更新數(shù)據(jù)庫時(shí),用戶的查詢和更新受到影響。
-r or -replace 這個(gè)選項(xiàng)與-i選項(xiàng)的作用相反;此選項(xiàng)將替代
表中有相同唯一關(guān)鍵字的記錄。
--fields-enclosed- by= char
指定文本文件中數(shù)據(jù)的記錄時(shí)以什么括起的, 很多情況下
數(shù)據(jù)以雙引號(hào)括起。 默認(rèn)的情況下數(shù)據(jù)是沒有被字符括起的。
--fields-terminated- by=char
指定各個(gè)數(shù)據(jù)的值之間的分隔符,在句號(hào)分隔的文件中,
分隔符是句號(hào)。您可以用此選項(xiàng)指定數(shù)據(jù)之間的分隔符。
默認(rèn)的分隔符是跳格符(Tab)
--lines-terminated- by=str
此選項(xiàng)指定文本文件中行與行之間數(shù)據(jù)的分隔字符串
或者字符。 默認(rèn)的情況下mysqlimport以newline為行分隔符。
您可以選擇用一個(gè)字符串來替代一個(gè)單個(gè)的字符:
一個(gè)新行或者一個(gè)回車。
mysqlimport命令常用的選項(xiàng)還有-v 顯示版本(version),
-p 提示輸入密碼(password)等。
這個(gè)工具有個(gè)問題,無法忽略某些列,這樣對(duì)我們的數(shù)據(jù)導(dǎo)入有很大的麻煩,雖然
可以手工設(shè)置這個(gè)字段,但會(huì)出現(xiàn)莫名其妙的結(jié)果,我們做一個(gè)簡單的示例
我們定義如下的depart_no.txt,保存在e盤,間隔為制表符\t
10 10
11 11
12 24
執(zhí)行如下命令
c:\mysql\bin\mysqlimport -uroot -proot staffer e:\depart_pos.txt
在這里沒有使用列的包圍符號(hào),分割采用默認(rèn)的\t,因?yàn)椴捎脛e的符號(hào)會(huì)有問題,
不知道是不是windows的原因
2)Load Data INFILE file_name into table_name(column1_name,column2_name)
這個(gè)命令在mysql>提示符下使用,優(yōu)點(diǎn)是可以指定列導(dǎo)入,示例如下
c:\mysql\bin\mysql -uroot -proot staffer
mysql>load data infile "e:/depart_no.txt" into depart_no(department_id,position_id);
這兩個(gè)工具在Windows下使用都有問題,不知道是Windows的原因還是中文的問題,
而且不指定的列它產(chǎn)生了空值,這顯然不是我們想要的,所以謹(jǐn)慎使用這些工具