MySQL的學(xué)習(xí)筆記
一、安裝與配置
21分鐘 MySQL入門教程- wid -博客園http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html#c1
二、登錄數(shù)據(jù)庫
在 Windows 命令提示符下運(yùn)行:
啟動: net start MySQL
停止: net stop MySQL
卸載: sc delete MySQL
當(dāng) MySQL 服務(wù)已經(jīng)運(yùn)行時, 我們可以通過MySQL自帶的客戶端工具登錄到MySQL數(shù)據(jù)庫中, 首先打開命令提示符, 輸入以下格式的命名:
mysql -h 主機(jī)名 -u 用戶名 -p 密碼
命令窗口下:
1) Cd / //進(jìn)入命令模式
2) Mysql -uroot -p123 //用戶登錄
3) Show databases //查看已有數(shù)據(jù)庫
4) Use test //進(jìn)入數(shù)據(jù)庫
5) Show tables //查看表
三、建用戶,
MySQL添加用戶、刪除用戶與授權(quán) - wanghetao -博客園http://www.cnblogs.com/wanghetao/p/3806888.html
1.新建用戶
1.1 登錄MYSQL:
@>mysql -u root -p
@>密碼
1.2 創(chuàng)建用戶:
mysql> insert into mysql.user(Host,User,PassWord) values("localhost","test",password("1234"));
這樣就創(chuàng)建了一個名為:test密碼為:1234的用戶。
注意:此處的"localhost",是指該用戶只能在本地登錄,不能在另外一臺機(jī)器上遠(yuǎn)程登錄。如果想遠(yuǎn)程登錄的話,將"localhost"改為"%",表示在任何一臺電腦上都可以登錄。也可以指定某臺機(jī)器可以遠(yuǎn)程登錄。
1.3 然后登錄一下:
mysql>exit;
@>mysql -u test -p
@>輸入密碼
mysql>登錄成功
2.為用戶授權(quán)
授權(quán)格式:grant權(quán)限on 數(shù)據(jù)庫.* to用戶名@登錄主機(jī)identified by "密碼";
2.1 登錄MYSQL(有ROOT權(quán)限),這里以ROOT身份登錄:
@>mysql -u root -p
@>密碼
2.2 首先為用戶創(chuàng)建一個數(shù)據(jù)庫(testDB):
mysql>create database testDB;
2.3 授權(quán)test用戶擁有testDB數(shù)據(jù)庫的所有權(quán)限(某個數(shù)據(jù)庫的所有權(quán)限):
mysql>grant all PRivileges on testDB.* to test@localhost identified by '1234';
mysql>flush privileges;//刷新系統(tǒng)權(quán)限表
格式:grant 權(quán)限on數(shù)據(jù)庫.* to用戶名@登錄主機(jī)identified by "密碼";
2.4 如果想指定部分權(quán)限給一用戶,可以這樣來寫:
mysql>grant select,update on testDB.* to test@localhost identified by '1234';
mysql>flush privileges; //刷新系統(tǒng)權(quán)限表
2.5 授權(quán)test用戶擁有所有數(shù)據(jù)庫的某些權(quán)限:
mysql>grant select,delete,update,create,drop on *.* to test@"%" identified by "1234";
//test用戶對所有數(shù)據(jù)庫都有select,delete,update,create,drop權(quán)限。
//@"%"表示對所有非本地主機(jī)授權(quán),不包括localhost。(localhost地址設(shè)為127.0.0.1,如果設(shè)為真實(shí)的本地地址,不知道是否可以,沒有驗(yàn)證。)
//對localhost授權(quán):加上一句grant all privileges on testDB.* to test@localhost identified by '1234';即可。
3. 刪除用戶
@>mysql -u root -p
@>密碼
mysql>Delete FROM user Where User='test' and Host='localhost';
mysql>flush privileges;
mysql>drop database testDB; //刪除用戶的數(shù)據(jù)庫
刪除賬戶及權(quán)限:>drop user用戶名@'%';
>drop user 用戶名@ localhost;
4. 修改指定用戶密碼
@>mysql -u root -p
@>密碼
mysql>update mysql.user set password=password('新密碼') where User="test" and Host="localhost";
mysql>flush privileges;
5. 列出所有數(shù)據(jù)庫
mysql>show database;
6. 切換數(shù)據(jù)庫
mysql>use '數(shù)據(jù)庫名';
7. 列出所有表
mysql>show tables;
8. 顯示數(shù)據(jù)表結(jié)構(gòu)
mysql>describe 表名;
9. 刪除數(shù)據(jù)庫和數(shù)據(jù)表
mysql>drop database 數(shù)據(jù)庫名;
mysql>drop table 數(shù)據(jù)表名;
四、建數(shù)據(jù)庫,
使用 create database 語句可完成對數(shù)據(jù)庫的創(chuàng)建, 創(chuàng)建命令的格式如下:
create database 數(shù)據(jù)庫名 [其他選項];
例如我們需要創(chuàng)建一個名為 samp_db 的數(shù)據(jù)庫, 在命令行下執(zhí)行以下命令:
create database samp_db character set gbk;
Create database mydb;
Drop database mydb;
Use y1 //切換數(shù)據(jù)庫
要對一個數(shù)據(jù)庫進(jìn)行操作, 必須先選擇該數(shù)據(jù)庫, 否則會提示錯誤:
ERROR 1046(3D000): No database selected
兩種方式對數(shù)據(jù)庫進(jìn)行使用的選擇:
一: 在登錄數(shù)據(jù)庫時指定, 命令: mysql -D 所選擇的數(shù)據(jù)庫名 -h 主機(jī)名 -u 用戶名 -p
例如登錄時選擇剛剛創(chuàng)建的數(shù)據(jù)庫: mysql -D samp_db -u root -p
二: 在登錄后使用 use 語句指定, 命令: use 數(shù)據(jù)庫名;
use 語句可以不加分號, 執(zhí)行 use samp_db 來選擇剛剛創(chuàng)建的數(shù)據(jù)庫, 選擇成功后會提示: Database changed
五、建表
使用 create table 語句可完成對表的創(chuàng)建, create table 的常見形式:
create table 表名稱(列聲明);
以創(chuàng)建 students 表為例, 表中將存放 學(xué)號(id)、姓名(name)、性別(sex)、年齡(age)、聯(lián)系電話(tel) 這些內(nèi)容:
create table students
(
id int unsigned not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "-"
);
Create table user(
Id int,
Name varchar(30),
Pwd varchar(30),
Role varchar(30)
);
Desc user; //查看表結(jié)構(gòu)
六、對表的增刪改查
insert 語句可以用來將一行或多行數(shù)據(jù)插到數(shù)據(jù)庫表中, 使用的一般形式如下:
insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);
其中 [] 內(nèi)的內(nèi)容是可選的, 例如, 要給 samp_db 數(shù)據(jù)庫中的 students 表插入一條記錄, 執(zhí)行語句:
insert into students values(NULL, "王剛", "男", 20, "13811371377");
按回車鍵確認(rèn)后若提示 Query Ok, 1 row affected (0.05 sec) 表示數(shù)據(jù)插入成功。 若插入失敗請檢查是否已選擇需要操作的數(shù)據(jù)庫。
有時我們只需要插入部分?jǐn)?shù)據(jù), 或者不按照列的順序進(jìn)行插入, 可以使用這樣的形式進(jìn)行插入:
insert into students (name, sex, age) values("孫麗華", "女", 21);
select 語句常用來根據(jù)一定的查詢規(guī)則到數(shù)據(jù)庫中獲取數(shù)據(jù), 其基本的用法為:
select 列名稱 from 表名稱 [查詢條件];
例如要查詢 students 表中所有學(xué)生的名字和年齡, 輸入語句 select name, age from students; 執(zhí)行結(jié)果如下:
mysql> select name, age from students;
+--------+-----+
| name | age |
+--------+-----+
| 王剛 | 20 |
| 孫麗華 | 21 |
| 王永恒 | 23 |
| 鄭俊杰 | 19 |
| 陳芳 | 22 |
| 張偉朋 | 21 |
+--------+-----+
6 rows in set (0.00 sec)
mysql>
也可以使用通配符 * 查詢表中所有的內(nèi)容, 語句: select * from students;
where 關(guān)鍵詞用于指定查詢條件, 用法形式為: select 列名稱 from 表名稱 where 條件;
以查詢所有性別為女的信息為例, 輸入查詢語句: select * from students where sex="女";
where 子句不僅僅支持 "where 列名 = 值" 這種名等于值的查詢形式, 對一般的比較運(yùn)算的運(yùn)算符都是支持的, 例如 =、>、<、>=、<、!= 以及一些擴(kuò)展運(yùn)算符 is [not] null、in、like 等等。 還可以對查詢條件使用 or 和 and 進(jìn)行組合查詢, 以后還會學(xué)到更加高級的條件查詢方式, 這里不再多做介紹。
示例:
查詢年齡在21歲以上的所有人信息: select * from students where age > 21;
查詢名字中帶有 "王" 字的所有人信息: select * from students where name like "%王%";
查詢id小于5且年齡大于20的所有人信息: select * from students where id<5 and age>20;
update 語句可用來修改表中的數(shù)據(jù), 基本的使用形式為:
update 表名稱 set 列名稱=新值 where 更新條件;
使用示例:
將id為5的手機(jī)號改為默認(rèn)的"-": update students set tel=default where id=5;
將所有人的年齡增加1: update students set age=age+1;
將手機(jī)號為 13288097888 的姓名改為 "張偉鵬", 年齡改為 19: update students set name="張偉鵬", age=19 where tel="13288097888";
delete 語句用于刪除表中的數(shù)據(jù), 基本用法為:
delete from 表名稱 where 刪除條件;
使用示例:
刪除id為2的行: delete from students where id=2;
刪除所有年齡小于21歲的數(shù)據(jù): delete from students where age<20;
刪除表中的所有數(shù)據(jù): delete from students;
Select * from user;
Insert into user(id,name,pwd,role) values(1,”test”,”123”,”admin”);
Insert into user values(2,”shm”,”shm”,”test”);
Update user set role=”admin” where name=”shm”;
Delete from user where id=1;
七、創(chuàng)建后表的修改
alter table 語句用于創(chuàng)建后對表的修改, 基礎(chǔ)用法如下:
基本形式: alter table 表名 add 列名 列數(shù)據(jù)類型 [after 插入位置];
示例:
在表的最后追加列 address: alter table students add address char(60);
在名為 age 的列后插入列 birthday: alter table students add birthday date after age;
基本形式: alter table 表名 change 列名稱 列新名稱 新數(shù)據(jù)類型;
示例:
將表 tel 列改名為 telphone: alter table students change tel telphone char(13) default "-";
將 name 列的數(shù)據(jù)類型改為 char(16): alter table students change name name char(16) not null;
基本形式: alter table 表名 drop 列名稱;
示例:
刪除 birthday 列: alter table students drop birthday;
基本形式: alter table 表名 rename 新表名;
示例:
重命名 students 表為 workmates: alter table students rename workmates;
基本形式: drop table 表名;
示例: 刪除 workmates 表: drop table workmates;
基本形式: drop database 數(shù)據(jù)庫名;
示例: 刪除 samp_db 數(shù)據(jù)庫: drop database samp_db;
按照本文的安裝方式, root 用戶默認(rèn)是沒有密碼的, 重設(shè) root 密碼的方式也較多, 這里僅介紹一種較常用的方式。
使用 mysqladmin 方式:
打開命令提示符界面, 執(zhí)行命令: mysqladmin -u root -p password 新密碼
執(zhí)行后提示輸入舊密碼完成密碼修改, 當(dāng)舊密碼為空時直接按回車鍵確認(rèn)即可。
盡管我們可以在命令提示符下通過一行行的輸入或者通過重定向文件來執(zhí)行mysql語句, 但該方式效率較低, 由于沒有執(zhí)行前的語法自動檢查, 輸入失誤造成的一些錯誤的可能性會大大增加, 這時不妨試試一些可視化的MySQL數(shù)據(jù)庫管理工具, MySQL Workbench 就是 MySQL 官方 為 MySQL 提供的一款可視化管理工具, 你可以在里面通過可視化的方式直接管理數(shù)據(jù)庫中的內(nèi)容, 并且 MySQL Workbench 的 SQL 腳本編輯器支持語法高亮以及輸入時的語法檢查, 當(dāng)然, 它的功能強(qiáng)大, 絕不僅限于這兩點(diǎn)。
數(shù)據(jù)庫的導(dǎo)入與導(dǎo)出etc:
MySQL教程_w3cschool
http://www.w3cschool.cn/mysql/
八、前臺連接與調(diào)用
新聞熱點(diǎn)
疑難解答
圖片精選