標籤:
mysql -h127.0.0.1 -uroot -p密碼 // 命令列串連mysql
show databases; // 查看所有資料庫
use 資料庫名; // 選擇資料庫
show tables; // 查看所有資料表
describe 資料表名; // 查看錶結構
mysqldump -uroot -p密碼 study(資料庫名) > c:\mystudy.sql // 匯出資料庫
注意:此命令要在dos命令列執行,不能在mysql命令列執行
create databases study1; // 匯入資料庫
use study1;
mysql -uroot -p密碼 <c:\mystudy.sql
select * from 資料表名; // 查詢顯示所有記錄
select count(*) from 資料表名; // 統計記錄條數
select id, name, age from 資料表名 where sex=‘男‘ // 所有男生的id,姓名,年齡
select avg(age) from 資料表名 where sex=‘男‘ // 所有男生的平均年齡
select sum(age) from 資料表名 where sex=‘男‘ // 所有男生的總年齡
select id, name, age from 資料表名 where sex=‘男‘ order by age desc limit 0,3 // 查詢男生中年齡最大的3個人的資訊
order by 排序
desc 降序
select id, name, age from 資料表名 where sex=‘男‘ order by age asc limit 0,3 // 查詢男生中年齡最小的3個人的資訊
asc 升序
select count(distinct age) from 資料表名 // 年齡有多少階段
distinct 去掉重複值
select avg(age) from 資料表名 group by sex // 按性別分組,統計出每組使用者的平均年齡
group by 分組
select avg(age) from 資料表名 where age<25 group by sex having avg(age)>20 // 年齡小於25的使用者中,按性別分組且每 組平均年齡必須大於20,查出每組使用者 的平均年齡
update 資料表名 set age=22, sex=‘女‘ where id=5 // 將id等於5的使用者年齡改為22,性別改為女
delete from 資料表名 where age>=25 // 刪除所有年齡大於等於25的使用者
delete from 資料表名
insert into 資料表名(name, sex, age) values(‘張三‘, ‘男‘, 25)
建立表:
命令:create table <表名> (<欄位名 1> <類型 1> [,..<欄位名 n> <類型 n>]);
例子:
mysql> create table MyClass(
> id int(4) not null primary key auto_increment,
> name char(20) not null,
> sex int(4) not null default ‘0‘,
> degree double(16,2));
Mysql 資料庫補充內容:
四種常用索引:【1. 索引主要作用:對部分關鍵字段建立索引,也相當於排序,能提高查詢速度
注意:索引也會有代價的,不是索引欄位越多越好。建立索引能提高上10倍的查詢速度。對大資料表特別有用。一 般是針對 where, order by 中常用的欄位建立索引
】
1. 主索引 primary key 一個表只能建立一個主索引,主鍵預設主索引,具有唯一性
2.唯一索引 unique key 欄位中的值具有唯一性,一個表可以有多個
3.普通索引 index key 一般的索引,沒有唯一性要求,一個表可以有多個
4.全文索引 fulltext key 針對 text 等類型欄位建立,像新聞內容欄位
資料庫引擎:
Myisam:預設引擎,適合 select 查詢操作,查詢速度快。不支援事務。表鎖機制。
InnoDB:支援事務,行鎖機制 ,適合 update, insert 等操作。
2016-08-09 隨筆總結mysql相關