mysql指令按顺序排列_mysql基本语法大全
1.備份數(shù)據(jù)庫:
1.1備份數(shù)據(jù)庫中的表:
mysqldump -u root -p test a b >d:\bank_a.sql
//分別備份數(shù)據(jù)庫test下a和b表
1.2備份一個數(shù)據(jù)庫
mysqldump -u root -p test > d:\testbk.sql
1.3備份多個數(shù)據(jù)庫
mysqldump -u root -p --databases test mysql > D:\data.sql
1.4備份所有的數(shù)據(jù)庫
mysqldump -u -root -p --all-databases > D:\all.sql
1.5直接復(fù)制整個數(shù)據(jù)庫目錄(物理備份)
前提條件是停止mysql服務(wù),然后復(fù)制mysql下的data目錄下數(shù)據(jù)庫目錄。
2.還原數(shù)據(jù):
2.1使用mysql命令恢復(fù)
還原數(shù)據(jù)庫文件:
mysql -u root -p < d:\backup.sql
還原數(shù)據(jù)庫表文件:
mysql -u root -p test
2.2使用source命令恢復(fù)數(shù)據(jù)
還原數(shù)據(jù)庫文件:
mysql>source d:\testbk.sql
還原數(shù)據(jù)庫表文件:
mysql>use test
mysql>source d:\testbk.sql
2.3先停止mysql服務(wù),然后拷貝備份的整個test數(shù)據(jù)庫目錄到目標(biāo)目錄。
2.4網(wǎng)絡(luò)上遠(yuǎn)程還原數(shù)據(jù)可以用
mysqldump -h x.x.x.x -u root -p test >tesbk.sql
2.5mysql忘記密碼:
1.停止mysql服務(wù)
net stop mysql或者相應(yīng)的進(jìn)程
2.進(jìn)入mysql下bin目錄:
mysqld --skip-grant-tables //跳過驗(yàn)證登錄
3.另外窗口打開:
mysql //直接可用進(jìn)入系統(tǒng)
4.更改密碼
use mysql;
update user set password=password('123456') where user='root' and host='localhost';
5.注銷系統(tǒng),再進(jìn)入,開MySQL,使用用戶名root和剛才設(shè)置的新密碼123456登陸
2.6修改密碼:
1.你的root用戶現(xiàn)在沒有密碼,你希望的密碼修改為123456,那么命令是:
mysqladmin -u root password 123456
2.如果你的root現(xiàn)在有密碼了(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具有插入,更新,刪除,創(chuàng)建,刪除對于數(shù)據(jù)庫所有表
GRANT ALL PRIVILEGES ON *.* TO 'backlion'@'%' IDENTIFIED BY 'backlion123' WITH GRANT OPTION;
grant all privileges on *.* to test@loclhost identified by "test";
創(chuàng)建主鍵:
Alter table test add primary key(code,curlum); //用code和curlum作為一組聯(lián)合主鍵來約束
3.數(shù)據(jù)庫操作:
3.1顯示數(shù)據(jù)庫
show databases;
3.2選擇數(shù)據(jù)庫
use examples;
3.3創(chuàng)建數(shù)據(jù)庫并設(shè)置編碼utf8 多語言
create database bk default character set utf8 collate utf8_general_ci;
3.4修改數(shù)據(jù)庫字符集為utf8
use mysql;
alter database character set utf8;
3.5刪除數(shù)據(jù)庫
drop database bk;
3.6查看數(shù)據(jù)庫狀態(tài):
status;
4.數(shù)據(jù)表的操作:
4.1顯示數(shù)據(jù)表
show tables;
4.2查看數(shù)據(jù)表的結(jié)構(gòu)屬性(字段,類型)
describe test;
desc test;
4.3復(fù)制表結(jié)構(gòu)(里面沒有數(shù)據(jù),結(jié)構(gòu)一樣)
create table newtest like oldtest; //創(chuàng)建新表newtest和舊表oldtest數(shù)據(jù)表結(jié)構(gòu)一樣
4.4復(fù)制表中的數(shù)據(jù)
insert into nettest select * from oldtest; //將舊表oldtest的數(shù)據(jù)復(fù)制到新表newtest里面
4.6重命名表名
alter table old_name rename new_name
4.7顯示當(dāng)前mysql版本和當(dāng)前日期
select version(),current_date;
4.8創(chuàng)建表:
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.常用的數(shù)據(jù)類型為int,varchar,date,text這4個數(shù)據(jù)類型
2.字段(行)的屬性有:數(shù)據(jù)類型(數(shù)據(jù)長度) 是否為空 是否為主鍵 是否為自動增加 默認(rèn)值
如:int(25) not null primary key auto_increment default 12
3.每個字段之間用逗號分開,最后那個字段不需要用逗號
4.以分號結(jié)束
5.可以設(shè)置簡單數(shù)據(jù)表結(jié)構(gòu)如:
字段名數(shù)據(jù)類型 數(shù)據(jù)長度 是否為空 是否主鍵是否自動增加 默認(rèn)值
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刪除數(shù)據(jù)表:
drop table bk; //包括結(jié)構(gòu)和數(shù)據(jù)都刪除
10.數(shù)據(jù)庫字段的操作:
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插入表數(shù)據(jù)
insert into test (id11,email,ip,state,bk)value(2,'601462930@qq.com','10.192.16.12',1314,567);
//如果是字符型對應(yīng)的值需要用單引號引起來,數(shù)字型不需要
11.5刪除數(shù)據(jù)
delete from test //刪除整個表test的數(shù)據(jù),結(jié)構(gòu)保留
delete from test where id11=2; 刪除數(shù)據(jù)表來自某個主鍵字段。就等于刪除整條數(shù)據(jù)
11.6修改表字段數(shù)據(jù)信息(數(shù)據(jù))
Update table_name set 字段名=’新值’ [, 字段2 =’新值’ , …..][where id=id_num] [order by 字段 順序]
update test set email='895098355@qq.com' where id11=2;
11.7修改字段的屬性(數(shù)據(jù)類型)
alter table test modify class varchar(30) not null; //修改表test中字段class的屬性為varchar(30)
12.查詢數(shù)據(jù):
12.1查詢所有數(shù)據(jù):
select * form test;
12.2查詢兩個字段的數(shù)據(jù)
select id,number from test;
12.3查詢前2行數(shù)據(jù):
select * from test limit 0,2;
12.4按增序排列查詢
select * from test order by id11 asc;
select * from test order by id11 //默認(rèn)為增序查詢
12.5按降序排列查詢
select * from test order by id11 desc;
12.6模糊查詢
select * from test where email '%qq%'; //查詢test表中,條件是email的數(shù)據(jù)中包含qq的數(shù)據(jù)
12.7查詢某個字段下面的數(shù)據(jù)
select email as emaildata from test ; //選擇email字段作為emalidata統(tǒng)計(jì)顯示出的數(shù)據(jù)
12.8.條件查詢
select * form test where id=12;
13.多表查詢:
13.1用法一:where條件聯(lián)合查詢
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 條件聯(lián)合查詢
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記錄聯(lián)合:
select語句1 union[all] select語句2
select * from testA union select id from testB;
總結(jié)
以上是生活随笔為你收集整理的mysql指令按顺序排列_mysql基本语法大全的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小森生活手套怎么获得
- 下一篇: 阀门多少钱一个啊?