MySQL基本命令

=====================================

MySQL數據庫

一、概念:
數據: data
數據庫: DB
數據庫管理系統:DBMS
數據庫系統:DBS
MySQL:數據庫
mysql:客戶端命令(用來連接服務或發送sql指令)
SQL:結構化查詢語言 ,其中MySQL支持這個。
SQL語言分爲4個部分:DDL、DML、DQL、DCL

======================================

二、連接數據庫:
mysql -h 主機名 -u 用戶名 -p密碼 庫名

C:>mysql –採用匿名賬號和密碼登陸本機服務
C:>mysql -h localhost -u root -proot –採用root賬號和root密碼登陸本機服務
C:>mysql -u root -p –推薦方式默認登陸本機
Enter password: **

C:>mysql -u root -p lamp61 –直接進入lamp61數據庫的方式登陸

=======================================

三、授權,刪除權限:
格式:grant 允許操作 on 庫名.表名 to ‘賬號’@’來源’ identified by ‘密碼’;

--實例:創建zhangsan賬號,密碼123,授權lamp61庫下所有表的增/刪/改/查數據,來源地不限
mysql> grant select,insert,update,delete on lamp61.* to 'zhangsan'@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

******************************************
*#新加用戶服務xcj,新加的用戶不能馬上生效
*mysql> grant all on *.* to 'xcj'@'%' identified by "xcj_passwd";
*Query OK, 0 rows affected (0.04 sec)

*#生效新加用戶xcj權限
*mysql> flush privileges;
*Query OK, 0 rows affected (0.03 sec)
*****************************************
-- 查看授權語句
mysql> show grants for zhangsan@'%';

--移除zhangsan用戶對lamp61庫的修改和添加權限
mysql> revoke update,insert on lamp61.* from zhangsan@'%';
    Query OK, 0 rows affected (0.00 sec)

--刪除了整個用戶及其權限(包括數據字典中的數據)
mysql> drop user zhangsan@'%';
    Query OK, 0 rows affected (0.00 sec)

============================================

–四、SQL的基本操作

mysql>\h – 快捷幫助
mysql>\c – 取消命令輸入
mysql>\s – 查看當前數據庫的狀態
mysql>\q – 退出mysql命令行

–顯示所有的數據庫
mysql> show databases;

–查看當前所在的數據庫
mysql> select database();

–創建數據庫
mysql> create database test;
mysql> create database if not exists test;

–刪除數據庫
mysql> drop database test;
mysql> drop database if exists test;

–使用數據庫
mysql> use test;

–查看建表的語句
mysql> show create table stu\G

–創建表
mysql> create table stu(
-> id int not null auto_increment primary key,
-> name varchar(16) not null,
-> sex enum(‘m’,’w’) not null default ‘m’,
-> age int not null);

–顯示當前數據庫下所有的表
mysql> show tables;

–查看錶結構
mysql> desc stu;

–表中插入數據
mysql> insert into stu(name,sex,age) values(‘zhangsan’,’m’,22),
-> (‘lisi’,’w’,’20’),
-> (‘wangwu’,’m’,’30’),
-> (‘zhaoliu’,’m’,’20’);

–顯示錶中的數據
mysql> select * from stu;

–刪除一條數據
mysql> delete from stu where id=1;

–更新一條數據
mysql> update stu set age=25 where id=2;

–添加一個字段
mysql> alter table stu add classid int not null;–注意一定要加類型

–修改字段屬性
mysql> alter table stu modify sex varchar(32) default ‘w’;–注意類型不能少
mysql> alter table stu change sex sex varchar(32) default ‘w’;??????????

–在某字段之後添加一個字段
mysql> alter table stu add idnum varchar(18) not null 【after age】;

–刪除一個字段
mysql> alter table stu drop idnum;

–添加索引
mysql> alter table stu add index(tel);

–刪除索引
mysql> alter table stu drop index tel;–注意此處不加()

–查看建表幫助
mysql> show create table s tu\G;

–修改表名
mysql> alter table stu rename as stu_msg;

– 重設表的自增id值(建議先刪除數據)
mysql> alter table users rement=1;

–查看當前時間
mysql> select now();

====================================================

五、 MySQL數據庫的數據類型:

MySQL的數據類型分爲四大類:數值類型、字串類型、日期類型、NULL。

5.1 數值類型:
    *tinyint(1字節)
    smallint(2字節)
    mediumint(3字節)
    *int(4字節)
    bigint(8字節)
    *float(4字節)   float(6,2)
    *double(8字節)   (可以數據放大幾倍,可以把小數點往後移動)
    decimal(自定義)字串形數值 用於價格,涉及錢的的字段(一般精確到8位)

 5.2 字串類型
    普通字串
    *char  定長字串 字符長度:
    *varchar 可變字串 規則:字節長度65535   字符長度與字符集長度
            1、最長長度遠遠大於平均長度
            2、數據修改少的情況下
            2、varchar處理複雜的數據比較有效。



    二進制類型
    tinyblob
    blob
    mediumblob
    longblob

    文本類型
    tinytext
    *text
    mediumtext
    longtext

    *enum枚舉 
    set集合

5.3 時間和日期類型:
    date  年月日
    time  時分秒
    datatime 年月日時分秒
    timestamp 時間戳
    year 年

5.4 NULL值
    NULL意味着“沒有值”或“未知值”
    可以測試某個值是否爲NULL
    不能對NULL值進行算術計算
    對NULL值進行算術運算,其結果還是NULL
    0或NULL都意味着假,其餘值都意味着真


零填充的作用:zero_fill  計算機底層處理時有零填充處理數據會很快。

=====================================================

六、 表的字段約束:
unsigned 無符號(正數)
zerofill 前導零填充
auto_increment 自增
default 默認值
not null 非空
PRIMARY KEY 主鍵
unique 唯一性
index 常規索引

==================================================

===================================================

MySQL第三天:

一、數據的增、刪、改:

--指定字段來添加
mysql> insert into stu(id,name,age,sex,classid) values(null,'ee',22,"m",'lamp64');

mysql> insert into stu values(null,'ww',22,"m",'lamp64');  --不指定字段名添加數據

mysql> insert into stu(age,name) values(21,'kkk'); --指定部分字段來添加

mysql> insert into stu(age,name) values(22,'aaa'),(23,'bbbb'),(24,"eee"); --批量添加
  1. 修改
    格式:update 表名 set 字段名=修改值[,字段名=修改值…] [where 條件][order by ..][limit..]

    – 將stu表中id等於11這條數據的age改爲20,sex改爲w、classid改爲lamp63。
    mysql> update stu set age=20,sex=’w’,classid=’lamp63’ where id=11;

  2. 刪除:
    格式: delete from 表名 [where 條件][order by ..][limit..]

    – 刪除stu表中id等於13的數據
    mysql> delete from stu where id=13;

  3. 查詢:
    格式: select 字段信息 from 表名
    [where 條件][group by 分組[having 子條件]][order by 排序][limit 部分數據]

    mysql> select * from stu; – 查看stu表中所有字段的所有信息

    mysql> select id,name,age from stu; –只查看部分字段信息

    mysql> select id,name,sex,”beijing” from stu; –追加一列輸出信息,值:beijing

    mysql> select id,name as username from stu; –爲name字段名起個別名(通過as)

    mysql> select id,name username,sex from stu; –同上,as關鍵字可以省略

    mysql> select id,name username,sex,”beijing” city from stu;

    mysql> select id,name,age,age+4 age2,classid from stu; –同時獲取4年後的年齡(別名age2)

    mysql> select id,concat(classid,”:”,name) stuname,age,sex from stu;
    –查詢stu表數據,整合classid和name字段爲一個值,並且起個別名爲stuname

    mysql> select distinct classid from stu; –查看stu表中classid字段的值,使用distinct去除重複值

    4.2 –where條件
    mysql> select * from stu where sex=’m’; –查看sex值爲m的所有信息

    mysql> select * from stu where sex=’m’ && classid=’lamp61’; –查看sex值爲m的並且classid爲lamp61的所有信息
    mysql> select * from stu where sex=’m’ and classid=’lamp61’; –推薦使用and與

    mysql> select * from stu where classid=’lamp61’ or classid=’lamp64’; –查看61期和64期信息

    mysql> select * from stu where classid !=’lamp61’; –不是lamp61期的學生信息

    mysql> select * from stu;
    +—-+———-+——+—–+———+
    | id | name | age | sex | classid |
    +—-+———-+——+—–+———+
    | 1 | zhangsan | 20 | m | lamp61 |
    | 2 | lisi | 22 | w | lamp61 |
    | 3 | wangwu | 21 | m | lamp61 |
    | 4 | qq | 24 | w | lamp62 |
    | 5 | aa | 20 | m | lamp62 |
    | 6 | bb | 25 | m | lamp63 |
    | 7 | dd | 23 | w | lamp62 |
    | 8 | ee | 22 | m | lamp64 |
    | 9 | ww | 22 | m | lamp64 |
    | 10 | kkk | 21 | m | NULL |
    | 11 | aaa | 20 | w | lamp63 |
    | 12 | bbbb | 23 | m | NULL |
    +—-+———-+——+—–+———+
    12 rows in set (0.00 sec)

    –查看年齡age在22歲以上的(含22)
    mysql> select * from stu where age>=22;

    –查看年齡age在22歲以上(含22)的所有男生(sex值爲m)
    mysql> select * from stu where age>=22 and sex=’m’;

    –查看年齡在22到24歲之間的所有學生信息
    mysql> select * from stu where age>=22 and age<=24;
    mysql> select * from stu where age between 22 and 24;

    –查看年齡在22歲以下或24以上的學生信息
    mysql> select * from stu where age<22 or age>24;
    mysql> select * from stu where age not between 22 and 24;

    –查看classid值爲null的學生信息
    mysql> select * from stu where classid is null;
    mysql> select * from stu where classid <=> null;

    –查看id號爲1,5,6,10的學生信息
    mysql> select * from stu where id=1 or id=5 or id=6 or id=10;
    mysql> select * from stu where id in (1,5,6,10);

    –like是模糊查詢,支持兩個特殊字符:”_”:任意一個字符 “%”:任意數量的任意字符
    –查詢姓名是由兩個字母構成的信息。
    mysql> select * from stu where name like ‘__’;
    –查看姓名裏含有a字母的。
    mysql> select * from stu where name like ‘%a%’;
    –查看姓名是由a字母開頭的。
    mysql> select * from stu where name like ‘a%’;

    4.3 – order by 排序 格式: ….. order by 被排序的字段名 [asc(升序,默認)|desc(降序)][,其他字段排序]
    –查看學生信息(按年齡從小到大排序)
    mysql> select * from stu order by age; –默認升序asc
    mysql> select * from stu order by age asc;

    –查看學生信息(按年齡從大到小排序)
    mysql> select * from stu order by age desc;

    –查看學生信息(按年齡從大到小排序),年齡相同的,按id從大到小排
    mysql> select * from stu order by age desc,id desc;

    –查看lamp61期學生信息,按年齡從大到小排序
    mysql> select * from stu where classid=’lamp61’ order by age desc;

    4.4 –limit 子句 用於截取部分數據,(分頁技術)
    – 格式: …limit n 或 …limit m,n (其中n表示取多少條數據,m表示起始位置)

    – 提取前3條數據
    mysql> select * from stu limit 3;

    – 獲取年齡最大的4位
    mysql> select * from stu order by age desc limit 4;

    – 獲取61期年齡最大的一位
    mysql> select * from stu where classid=”lamp61” order by age desc limit 1;

    mysql> select * from stu limit 0,4; –以4條/頁,獲取第一頁
    mysql> select * from stu limit 4,4; –以4條/頁,獲取第二頁
    mysql> select * from stu limit 8,4; –以4條/頁,獲取第三頁
    mysql> select * from stu limit 12,4; –以4條/頁,獲取第四頁

    –分頁公式:….. limit (當前頁-1)*頁大小,頁大小;

    4.5 –用於統計的統計函數(聚合函數):
    – count()統計條數, sum()求總和、 avg()平均、 max()最大、 min()最小
    –統計stu的總數據條數
    mysql> select count(id) from stu;
    mysql> select count(*) from stu;

    –統計61期學生條數、年齡總和、平均年齡、最大年齡、最小年齡
    mysql> select count(*),sum(age),avg(age),max(age),min(age) from stu where classid=’lamp61’;
    +———-+———-+———-+———-+———-+
    | count(*) | sum(age) | avg(age) | max(age) | min(age) |
    +———-+———-+———-+———-+———-+
    | 3 | 63 | 21.0000 | 22 | 20 |
    +———-+———-+———-+———-+———-+
    1 row in set (0.02 sec)

    4.6 –分組處理 group by
    –統計每個班都有多少人
    mysql> select classid,count(*) from stu group by classid;

    –統計每個班都有多少男生
    mysql> select classid,count(*) from stu where sex=’m’ group by classid;

    –統計每個班(去除null值)都有多少男生,要求按降序排序
    mysql> select classid,count(*) from stu where sex=’m’ and classid is not null
    group by classid order by count(*) desc;

    mysql> select classid,count(*) num from stu where sex=’m’ and classid is not null
    group by classid order by num desc;

    –統計每個班的平均年齡,從大到小排序
    mysql> select classid,avg(age) aa from stu group by classid order by aa desc;

    –統計每個班的平均年齡,從大到小排序,要求只保留22歲以上的。
    mysql> select classid,count(*) num from stu where sex=’m’ and classid is not null
    group by classid order by num desc;

二、 數據庫的導出
1. 導出:

    --導出lamp61數據庫到lamp61_20130328.sql文件中
    D:\>mysqldump -u root -p lamp61>lamp61_20130328.sql
    Enter password: ****

    --導出lamp61數據庫中stu表到lamp61_stu_20130328.sql文件中
    D:\>mysqldump -u root -p lamp61 stu>lamp61_stu_20130328.sql
    Enter password: ****

 2. 恢復:
    --恢復stu表
    D:\>mysql -u root -p lamp61<lamp61_stu_20130328.sql
    Enter password: ****

    --恢復整個庫
    D:\>mysql -u root -p lamp61<lamp61_20130328.sql
    Enter password: ****
發佈了28 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章