1.備份數據庫:
1.1備份數據庫中的表:
mysqldump -u root -p test a b >d:\bank_a.sql
//分別備份數據庫test下a和b表
1.2備份一個數據庫
mysqldump -u root -p test > d:\testbk.sql
1.3備份多個數據庫
mysqldump -u root -p --databases test mysql > D:\data.sql
1.4備份所有的數據庫
mysqldump -u -root -p --all-databases > D:\all.sql
1.5直接復制整個數據庫目錄(物理備份)
前提條件是停止mysql服務,然后復制mysql下的data目錄下數據庫目錄。
2.還原數據:
2.1使用mysql命令恢復
還原數據庫文件:
mysql -u root -p < d:\backup.sql
還原數據庫表文件:
mysql -u root -p test
2.2使用source命令恢復數據
還原數據庫文件:
mysql>source d:\testbk.sql
還原數據庫表文件:
mysql>use test
mysql>source d:\testbk.sql
2.3先停止mysql服務,然后拷貝備份的整個test數據庫目錄到目標目錄。
2.4網絡上遠程還原數據可以用
mysqldump -h x.x.x.x -u root -p test >tesbk.sql
2.5mysql忘記密碼:
1.停止mysql服務
net stop mysql或者相應的進程
2.進入mysql下bin目錄:
mysqld --skip-grant-tables //跳過驗證登錄
3.另外窗口打開:
mysql //直接可用進入系統
4.更改密碼
use mysql;
update user set password=password('123456') where user='root' and host='localhost';
5.注銷系統,再進入,開MySQL,使用用戶名root和剛才設置的新密碼123456登陸
2.6修改密碼:
1.你的root用戶現在沒有密碼,你希望的密碼修改為123456,那么命令是:
mysqladmin -u root password 123456
2.如果你的root現在有密碼了(123456),那么修改密碼為abcdef的命令是:
mysqladmin -u root -p password 123456
2.7添加用戶:
grant select,insert,update,delete,create,drop on stud.* to user1@localhost identified by "user1"; //添加用戶名user1密碼為user1具有插入,更新,刪除,創建,刪除對于數據庫所有表
GRANT ALL PRIVILEGES ON *.* TO 'backlion'@'%' IDENTIFIED BY 'backlion123' WITH GRANT OPTION;
grant all privileges on *.* to test@loclhost identified by "test";
創建主鍵:
Alter table test add primary key(code,curlum); //用code和curlum作為一組聯合主鍵來約束
3.數據庫操作:
3.1顯示數據庫
show databases;
3.2選擇數據庫
use examples;
3.3創建數據庫并設置編碼utf8 多語言
create database bk default character set utf8 collate utf8_general_ci;
3.4修改數據庫字符集為utf8
use mysql;
alter database character set utf8;
3.5刪除數據庫
drop database bk;
3.6查看數據庫狀態:
status;
4.數據表的操作:
4.1顯示數據表
show tables;
4.2查看數據表的結構屬性(字段,類型)
describe test;
desc test;
4.3復制表結構(里面沒有數據,結構一樣)
create table newtest like oldtest; //創建新表newtest和舊表oldtest數據表結構一樣
4.4復制表中的數據
insert into nettest select * from oldtest; //將舊表oldtest的數據復制到新表newtest里面
4.6重命名表名
alter table old_name rename new_name
4.7顯示當前mysql版本和當前日期
select version(),current_date;
4.8創建表:
create table bk(
id int(10) unsigned zerofill not null auto_increment,
email varchar(40) not null,
ip varchar(15) not null,
state int(10) not null default '-1',
primary key (id)
);
/*
1.常用的數據類型為int,varchar,date,text這4個數據類型
2.字段(行)的屬性有:數據類型(數據長度) 是否為空 是否為主鍵 是否為自動增加 默認值
如:int(25) not null primary key auto_increment default 12
3.每個字段之間用逗號分開,最后那個字段不需要用逗號
4.以分號結束
5.可以設置簡單數據表結構如:
字段名數據類型 數據長度 是否為空 是否主鍵是否自動增加 默認值
id int 12 NOT NULL primary key auto_increment
name varchar 30NOT NULL
password varchar30 NOT NULL
time date 30 NOT NULL
jianyi text 400
*/
4.9刪除數據表:
drop table bk; //包括結構和數據都刪除
10.數據庫字段的操作:
11.1添加表字段
alter table test add bk varchar(32) not null; //向表test中添加bk字段(列)
11.2修改表字段
alter table test change id id1 varchar(10) not null; //將表test中字段(列)id更改為id1
11.3刪除字段(列)
alter table test drop cn;
11.4插入表數據
insert into test (id11,email,ip,state,bk)value(2,'601462930@qq.com','10.192.16.12',1314,567);
//如果是字符型對應的值需要用單引號引起來,數字型不需要
11.5刪除數據
delete from test //刪除整個表test的數據,結構保留
delete from test where id11=2; 刪除數據表來自某個主鍵字段。就等于刪除整條數據
11.6修改表字段數據信息(數據)
Update table_name set 字段名=’新值’ [, 字段2 =’新值’ , …..][where id=id_num] [order by 字段 順序]
update test set email='895098355@qq.com' where id11=2;
11.7修改字段的屬性(數據類型)
alter table test modify class varchar(30) not null; //修改表test中字段class的屬性為varchar(30)
12.查詢數據:
12.1查詢所有數據:
select * form test;
12.2查詢兩個字段的數據
select id,number from test;
12.3查詢前2行數據:
select * from test limit 0,2;
12.4按增序排列查詢
select * from test order by id11 asc;
select * from test order by id11 //默認為增序查詢
12.5按降序排列查詢
select * from test order by id11 desc;
12.6模糊查詢
select * from test where email '%qq%'; //查詢test表中,條件是email的數據中包含qq的數據
12.7查詢某個字段下面的數據
select email as emaildata from test ; //選擇email字段作為emalidata統計顯示出的數據
12.8.條件查詢
select * form test where id=12;
13.多表查詢:
13.1用法一:where條件聯合查詢
select 表1.字段 [as 別名],表n.字段 from 表1 [別名],表n where 條件;
select testA.username as username,testB.id from testA,testB where testA.uid=testB.uid
13.2用法二:inner join on 條件聯合查詢
select 表1.字段 [as 別名],表n.字段 from 表1 INNER JOIN 表n on 條件;
select testA.username as uername ,testB.id from testA inner join testB on testA.uid=testB.uid
13.3記錄聯合:
select語句1 union[all] select語句2
select * from testA union select id from testB;