1、MySQL常用命令
create database name; 創(chuàng)建數(shù)據(jù)庫
use database name; 選擇數(shù)據(jù)庫
drop database name 直接刪除數(shù)據(jù)庫,不提醒
show tables; 顯示表
describe tablename; 表的詳細(xì)描述
select * from tablename;
Mysql命令查詢一個表的記錄總數(shù)(三種方法)
select count(*) from tablename;
或者
select count(*) as num from tablename;
或者
select count(*) as total from tablename;
select 中加上distinct去除重復(fù)字段
mysqladmin drop databasename 刪除數(shù)據(jù)庫前,有提示霎冯。
顯示當(dāng)前mysql版本和當(dāng)前日期
select version(),current_date;
2蓬坡、修改mysql中root的密碼:?
shell>mysql -u root -p
mysql> update user set password=password(”xueok654123″) where user=’root’;
mysql> flush privileges //刷新數(shù)據(jù)庫
mysql>use dbname; 打開數(shù)據(jù)庫:
mysql>show databases; 顯示所有數(shù)據(jù)庫
mysql>show tables; 顯示數(shù)據(jù)庫mysql中所有的表:先use mysql沉迹;然后
mysql>describe user; 顯示表mysql數(shù)據(jù)庫中user表的列信息)浪谴;
3开睡、grant?
創(chuàng)建一個可以從任何地方連接服務(wù)器的一個完全的超級用戶,但是必須使用一個口令something做這個
mysql> grant all privileges on *.* to user@localhost identified by ’something’ with
增加新用戶
格式:grant select on 數(shù)據(jù)庫.* to 用戶名@登錄主機(jī) identified by “密碼”
GRANT ALL PRIVILEGES ON *.* TO monty@localhost IDENTIFIED BY ’something’ WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO monty@”%” IDENTIFIED BY ’something’ WITH GRANT OPTION;
刪除授權(quán):?
mysql> revoke all privileges on *.* from root@”%”;
mysql> delete from user where user=”root” and host=”%”;
mysql> flush privileges;
創(chuàng)建一個用戶custom在特定客戶端it363.com登錄苟耻,可訪問特定數(shù)據(jù)庫fangchandb
mysql >grant select, insert, update, delete, create,drop on fangchandb.* to custom@ it363.com identified by ‘ passwd’
重命名表:
mysql > alter table t1 rename t2;
4篇恒、mysqldump
備份數(shù)據(jù)庫
shell> mysqldump -h host -u root -p dbname >dbname_backup.sql
恢復(fù)數(shù)據(jù)庫
shell> mysqladmin -h myhost -u root -p create dbname
shell> mysqldump -h host -u root -p dbname < dbname_backup.sql
如果只想卸出建表指令,則命令如下:
shell> mysqladmin -u root -p -d databasename > a.sql
如果只想卸出插入數(shù)據(jù)的sql命令凶杖,而不需要建表命令胁艰,則命令如下:
shell> mysqladmin -u root -p -t databasename > a.sql
那么如果我只想要數(shù)據(jù),而不想要什么sql命令時智蝠,應(yīng)該如何操作呢腾么?
mysqldump -T./ phptest driver
其中,只有指定了-T參數(shù)才可以卸出純文本文件杈湾,表示卸出數(shù)據(jù)的目錄解虱,./表示當(dāng)前目錄,即與mysqldump同一目錄毛秘。如果不指定driver 表,則將卸出整個數(shù)據(jù)庫的數(shù)據(jù)。每個表會生成兩個文件叫挟,一個為.sql文件艰匙,包含建表執(zhí)行。另一個為.txt文件抹恳,只包含數(shù)據(jù)员凝,且沒有sql指令。?
5奋献、可將查詢存儲在一個文件中并告訴mysql從文件中讀取查詢而不是等待鍵盤輸入健霹。可利用外殼程序鍵入重定向?qū)嵱贸绦騺硗瓿蛇@項工作瓶蚂。例如糖埋,如果在文件my_file.sql 中存放有查 詢,可如下執(zhí)行這些查詢:?
例如窃这,如果您想將建表語句提前寫在sql.txt中:
mysql > mysql -h myhost -u root -p database < sql.txt
一 . 安裝與配置MYSQL
二 . 常用mysql命令行命令
1 .mysql的啟動與停止
啟動MYSQL服務(wù) net start mysql
停止MYSQL服務(wù) net stop mysql
2 . netstat –na | findstr 3306 查看被監(jiān)聽的端口 , findstr用于查找后面的端口是否存在
3 . 在命令行中登陸MYSQL控制臺 , 即使用 MYSQL COMMEND LINE TOOL
語法格式 mysql –user=root –password=123456 db_name
或 mysql –u root –p123456 db_name
4 . 進(jìn)入MYSQL命令行工具后 , 使用status; 或\s 查看運(yùn)行環(huán)境信息
5 . 切換連接數(shù)據(jù)庫的語法 : use new_dbname;
6 . 顯示所有數(shù)據(jù)庫 : show databases;
7 . 顯示數(shù)據(jù)庫中的所有表 : show tables;
8 . 顯示某個表創(chuàng)建時的全部信息 : show create table table_name;
9 . 查看表的具體屬性信息及表中各字段的描述
Describe table_name; 縮寫形式 : desc table_name;
三 瞳别、 MySql中的SQL語句
1 . 數(shù)據(jù)庫創(chuàng)建 : Create database db_name;
數(shù)據(jù)庫刪除 : Drop database db_name; 刪除時可先判斷是否存在,寫成 : drop database if exits db_name
2 . 建表 : 創(chuàng)建數(shù)據(jù)表的語法 : create table table_name (字段1 數(shù)據(jù)類型 , 字段2 數(shù)據(jù)類型);
例 : create table mytable (id int , username char(20));
刪表 : drop table table_name; 例 : drop table mytable;
3 . 添加數(shù)據(jù) : Insert into 表名 [(字段1 , 字段2 , ….)] values (值1 , 值2 , …..);
如果向表中的每個字段都插入一個值,那么前面 [ ] 括號內(nèi)字段名可寫也可不寫
例 : insert into mytable (id,username) values (1,’zhangsan’);
?4 . 查詢 : 查詢所有數(shù)據(jù) : select * from table_name;
查詢指定字段的數(shù)據(jù) : select 字段1 , 字段2 from table_name;
例 : select id,username from mytable where id=1 order by desc;多表查詢語句------------參照第17條實例
?5 . 更新指定數(shù)據(jù) , 更新某一個字段的數(shù)據(jù)(注意杭攻,不是更新字段的名字)
Update table_name set 字段名=’新值’ [, 字段2 =’新值’ , …..][where id=id_num] [order by 字段 順序]
例 : update mytable set username=’lisi’ where id=1;
Order語句是查詢的順序 , 如 : order by id desc(或asc) , 順序有兩種 : desc倒序(100—1,即從最新數(shù)據(jù)往后查詢),asc(從1-100)祟敛,Where和order語句也可用于查詢select 與刪除delete
6. 刪除表中的信息 :
刪除整個表中的信息 : delete from table_name;
刪除表中指定條件的語句 : delete from table_name where 條件語句 ; 條件語句如 : id=3;
7. 創(chuàng)建數(shù)據(jù)庫用戶
一次可以創(chuàng)建多個數(shù)據(jù)庫用戶如:
CREATE USER username1 identified BY ‘password’ , username2 IDENTIFIED BY ‘password’….
8 . 用戶的權(quán)限控制:grant
庫,表級的權(quán)限控制 : 將某個庫中的某個表的控制權(quán)賦予某個用戶
Grant all ON db_name.table_name TO user_name [ indentified by ‘password’ ];
9 . 表結(jié)構(gòu)的修改
≌捉狻(1)增加一個字段格式:
alter table table_name add column (字段名 字段類型); ----此方法帶括號
」萏(2)指定字段插入的位置:
alter table table_name add column 字段名 字段類型 after 某字段;
刪除一個字段:
alter table table_name drop字段名;
」Α(3)修改字段名稱/類型
alter table table_name change 舊字段名 新字段名 新字段的類型;
(4)改表的名字
alter table table_name rename to new_table_name;
〔壕蕖(5)一次性清空表中的所有數(shù)據(jù)
truncate table table_name; 此方法也會使表中的取號器(ID)從1開始
?10 . 增加主鍵,外鍵衣撬,約束乖订,索引。具练。乍构。。(使用方法見17實例)
】傅恪① 約束(主鍵Primary key哥遮、唯一性Unique、非空Not Null)
×昃俊② 自動增張 auto_increment
∶咭③外鍵Foreign key-----與reference table_name(col_name列名)配合使用,建表時單獨(dú)使用
⊥省④ 刪除多個表中有關(guān)聯(lián)的數(shù)據(jù)----設(shè)置foreign key 為set null ---具體設(shè)置參考幫助文檔
11. 查看數(shù)據(jù)庫當(dāng)前引擎
SHOW CREATE TABLE table_name;
修改數(shù)據(jù)庫引擎
ALTER TABLE table_name ENGINE=MyISAM | InnoDB;
12 . SQL語句運(yùn)用實例:
--1 建users表
create table users (id int primary key auto_increment,nikename varchar(20) not null unique,password varchar(100) not null,address varchar(200), reg_date timestamp not null default CURRENT_TIMESTAMP);
--2 建articles表,在建表時設(shè)置外鍵
create table articles (id int primary key auto_increment,content longtext not null,userid int,constraint foreign key (userid) references users(id) on delete set null);
-----------------------------------------------------------------------
--2.1 建articles表,建表時不設(shè)置外鍵
create table articles (id int primary key auto_increment,content longtext not null,userid int);
--2.2 給articles表設(shè)置外鍵
alter table articles add constraint foreign key (userid) references users(id) on delete set null;
--------------------------------------------------------------------- --3. 向users表中插入數(shù)據(jù),同時插入多條
insert into users (id,nikename,password,address) values (1,'lyh1','1234',null),(10,'lyh22','4321','湖北武漢'),(null,'lyh333','5678', '北京海淀');
--4. 向article中插入三條數(shù)據(jù)
insert into articles (id,content,userid) values (2,'hahahahahaha',11),(null,'xixixixixix',10),(13,'aiaiaiaiaiaiaiaiaiaiaiaia',1),(14,'hohoahaoaoooooooooo',10);
--5. 進(jìn)行多表查詢仪召,選擇users表中ID=10的用戶發(fā)布的所有留言及該用戶的所有信息
select articles.id,articles.content,users.* from users,articles where users.id=10 and articles.userid=users.id order by articles.id desc;
--6. 查看數(shù)據(jù)庫引擎類型
show create table users;
--7. 修改數(shù)據(jù)庫引擎類型
alter table users engine=MyISAM; ---因為users表中ID被設(shè)置成外鍵寨蹋,執(zhí)行此句會出錯
--8. 同表查詢,已知一個條件的情況下.查詢ID號大于用戶lyh1的ID號的所有用戶
select a.id,a.nikename,a.address from users a,users b where b.nikename='lyh1' and a.id>b.id;
------也可寫成
select id,nikename,address from users where id>(select id from users where nikename='lyh1');
9. 顯示年齡比領(lǐng)導(dǎo)還大的員工:
select a.name from users a,users b where a.managerid=b.id and a.age>b.age;
查詢編號為2的發(fā)帖人: 先查articles表,得到發(fā)帖人的編號,再根據(jù)編號查users得到的用戶名。
接著用關(guān)聯(lián)查詢.
select * from articles,users得到笛卡兒積,再加order by articles.id以便觀察
使用select * from articles,users where articles.id=2 篩選出2號帖子與每個用戶的組合記錄
再使用select * from articles,users where articles.id=2 and articles.userid=users.id選出users.id等于2號帖的發(fā)帖人id的記錄.
只取用戶名:select user where user.id=(select userid from articles where article.id =2)
找出年齡比小王還大的人:假設(shè)小王是28歲,先想找出年齡大于28的人
select * from users where age>(select age from users where name='xiaowang');
*****要查詢的記錄需要參照表里面的其他記錄:
select a.name from users a,users b where b.name='xiaowang' and a.age>b.age
表里的每個用戶都想pk一下.select a.nickname,b.nickname from users a,users b where a.id>b.id ;
更保險的語句:select a.nickname,b.nickname from (select * from users order by id) a,(se
lect * from users order by id) b where a.id>b.id ;
再查詢某個人發(fā)的所有帖子.
select b.* from articles a , articles b where a.id=2 and a.userid=b.userid
說明: 表之間存在著關(guān)系扔茅,ER概念的解釋已旧,用access中的示例數(shù)據(jù)庫演示表之間的關(guān)系.只有innodb引擎才支持foreign key,mysql的任何引擎目前都不支持check約束召娜。
四运褪、字符集出現(xiàn)錯誤解決辦法
出現(xiàn)的問題:
mysql> update users
-> set username='關(guān)羽'
-> where userid=2;
ERROR 1366 (HY000): Incorrect string value: '\\xB9\\xD8\\xD3\\xF0' for column 'usern
ame' at row 1
向表中插入中文字符時,出現(xiàn)錯誤玖瘸。
mysql> select * from users;
+--------+----------+
| userid | username |
+--------+----------+
| 2 | ???? |
| 3 | ???? |
| 4 | ?í?ù |
+--------+----------+
3 rows in set (0.00 sec)
表中的中文字符位亂碼秸讹。
解決辦法:
使用命令:
mysql> status;
--------------
mysql Ver 14.12 Distrib 5.0.45, for Win32 (ia32)
Connection id: 8
Current database: test
Current user: root@localhost
SSL: Not in use
Using delimiter: ;
Server version: 5.0.45-community-nt MySQL Community Edition (GPL)
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: gbk
Conn. characterset: gbk
TCP port: 3306
Uptime: 7 hours 39 min 19 sec
Threads: 2 Questions: 174 Slow queries: 0 Opens: 57 Flush tables: 1 Open ta
bles: 1 Queries per second avg: 0.006
--------------
查看mysql發(fā)現(xiàn)Server characterset,Db characterset的字符集設(shè)成了latin1雅倒,所以出現(xiàn)中文亂碼璃诀。
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| users |
+----------------+
1 row in set (0.00 sec)
更改表的字符集。
mysql> alter table users character set GBK;
Query OK, 3 rows affected (0.08 sec)
Records: 3 Duplicates: 0 Warnings: 0
查看表的結(jié)構(gòu):
mysql> show create users;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'users
' at line 1
mysql> show create table users;
+-------+-----------------------------------------------------------------------
------------------------------------------------------------------------------+
| Table | Create Table
|
+-------+-----------------------------------------------------------------------
------------------------------------------------------------------------------+
| users | CREATE TABLE `users` (
`userid` int(11) default NULL,
`username` char(20) character set latin1 default NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+-------+-----------------------------------------------------------------------
------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> desc users;
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| userid | int(11) | YES | | NULL | |
| username | char(20) | YES | | NULL | |
+----------+----------+------+-----+---------+-------+
2 rows in set (0.02 sec)
這時向表中插入中文然后有錯誤屯断。
mysql> insert into users values(88,'中文');
ERROR 1366 (HY000): Incorrect string value: '\\xD6\\xD0\\xCE\\xC4' for column 'usern
ame' at row 1
mysql> insert into users values(88,'中文');
mysql> insert into users values(88,'中文');
ERROR 1366 (HY000): Incorrect string value: '\\xD6\\xD0\\xCE\\xC4' for column 'usern
ame' at row 1
還要更改users表的username的字符集文虏。
mysql> alter table users modify username char(20) character set gbk;
ERROR 1366 (HY000): Incorrect string value: '\\xC0\\xEE\\xCB\\xC4' for column 'usern
ame' at row 1
mysql> alter table users modify username char(20) character set gbk;
ERROR 1366 (HY000): Incorrect string value: '\\xC0\\xEE\\xCB\\xC4' for column 'usern
ame' at row 1
因為表中已經(jīng)有數(shù)據(jù),所以更改username字符集的操作沒有成***
清空users表中的數(shù)據(jù)
mysql> truncate table users;
Query OK, 3 rows affected (0.01 sec)
從新更改user表中username的字符集
mysql> alter table users modify username char(20) character set gbk;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
這時再插入中文字符殖演,插入成***氧秘。
mysql> insert into users values(88,'中文');
Query OK, 1 row affected (0.01 sec)
mysql> select * from users;
+--------+----------+
| userid | username |
+--------+----------+
| 88 | 中文 |
+--------+----------+
1 row in set (0.00 sec)
mysql>
……...…………………………
?