MySQL基本原生常用語句

  • 常用操作數(shù)據(jù)庫的命令

1.show databases; 查看所有的數(shù)據(jù)庫
2.create database test; 創(chuàng)建一個(gè)叫test的數(shù)據(jù)庫
3.drop database test;刪除一個(gè)叫test的數(shù)據(jù)庫
4.use test;選中庫 ,在建表之前必須要選擇數(shù)據(jù)庫
5.show tables; 在選中的數(shù)據(jù)庫之中查看所有的表
6.create table 表名 (字段1 類型, 字段2 類型);
7.desc 表名;查看所在的表的字段
8.drop table 表名; 刪除表
9.show create databases 庫名;查看創(chuàng)建庫的詳細(xì)信息
10.show create table 表名; 查看創(chuàng)建表的詳細(xì)信息
  • 修改表的命令

1.修改字段類型 alter table 表名 modify 字段 字段類型;
2.添加新的字段 alter table 表名 add 字段 字段類型
3.添加字段并指定位置  alter table 表名 add 字段 字段類型   after 字段;
4.刪除表字段  alter table 表名 drop 字段名;
5.修改指定的字段  alter table 表名 change 原字段名字  新的字段名字 字段類型
  • 對(duì)數(shù)據(jù)的操作

1.增加數(shù)據(jù)(insert)3種方式
    1.1 insert into 表名 values(值1,值2,...)(很少用)
    1.2 insert into 表名(字段1,字段2...) values(值1,值2,....);(較常用)
    1.3 insert into 表名(字段1,字段2...) values(值1,值2,....),(值1,值2,....),(值1,值2,....);
2.刪除數(shù)據(jù)(delete) delete from 表名 where 條件 注意:where 條件必須加,否則數(shù)據(jù)會(huì)被全部刪除
3.更新數(shù)據(jù)(update)  update 表名 set字段1 = 值1, 字段2 = 值2 where 條件
4.查詢數(shù)據(jù)(select)
    4.1 查詢表中的所有數(shù)據(jù)   select * from 表名
    4.2 指定數(shù)據(jù)查詢    select 字段 from 表名 
    根據(jù)條件查詢出來的數(shù)據(jù)  select 字段 from 表名 where 條件 (最常用的)
    where 條件后面跟的條件
     關(guān)系:>,<,>=,<=,!=  
     邏輯:or, and 
     區(qū)間:id between 4 and 6 ;閉區(qū)間,包含邊界
5.排序
select 字段 from 表 order by 字段  排序關(guān)鍵詞(desc | asc)
排序關(guān)鍵詞 desc 降序 asc 升序(默認(rèn))
    5.1 通過字段來排序
    例如 :select * from star orser by money desc, age asc;   
    5.2 多字段排序
    select 字段 from 表 order by 字段1  desc |asc,...字段n desc| asc;
6.常用的統(tǒng)計(jì)函數(shù) sum,avg,count,max,min
    只分組:select * from 表 group by 字段
    例子: select count(sex) as re,sex from star group by sex having re > 3;
    分組統(tǒng)計(jì): select count(sex) from star group by sex;
7.分組 select * from 表名  limit 偏移量,數(shù)量
    說明:
        8.1.不寫偏移量的話就是默認(rèn)的為0
        8.2.實(shí)現(xiàn)分頁的時(shí)候必須寫偏移量
        偏移量怎么計(jì)算?:
        limit (n-1)*數(shù)量 ,數(shù)量 
  • 多表聯(lián)合查詢

1.內(nèi)連接
隱式內(nèi)連接 select username,name from user,goods where user,gid=gods,gid;
顯示內(nèi)連接
select username,from user inner join goods on user.gid=goods.gid;
select * from user left join goods on user.gid=goods.gid;
2.外鏈接
左連接 包含所有的左邊表中的記錄以及右邊表中沒有和他匹配的記錄
右連接 
select * from user where gid in(select gid from goods);
select * from user right jOin goods on user.gid=goods.gid;
子嵌套查詢
數(shù)據(jù)聯(lián)合查詢
select * from user left join goods on user.gid=goods.gid union select * from user right join goods on user.gid=goods.gid;
兩個(gè)表同時(shí)更新
update user u, goods g set u.gid=12,g.price=1 where u.id=2 and u.gid=g.gid;
五、DCL 數(shù)據(jù)控制語言

1.創(chuàng)建用戶:create user'xiaoming'@'localhost' identified by '666666';
2.授權(quán)用戶:grant all on test.*to'xiaoming'@'localhost';
3.刷新權(quán)限:flush privileges;
4.取消授權(quán):revoke all on test.* from 'xiaoming'@'localhost';
5.刪除用戶: drop user'xiaoming'@'localhost';
  • DTL 數(shù)據(jù)事務(wù)語言

開啟事務(wù):set autocommit=0;
操作回滾:rollback;
提交事務(wù):commit;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ORA-00001: 違反唯一約束條件 (.) 錯(cuò)誤說明:當(dāng)在唯一索引所對(duì)應(yīng)的列上鍵入重復(fù)值時(shí),會(huì)觸發(fā)此異常。 O...
    我想起個(gè)好名字閱讀 5,974評(píng)論 0 9
  • 今天看到一位朋友寫的mysql筆記總結(jié),覺得寫的很詳細(xì)很用心,這里轉(zhuǎn)載一下,供大家參考下,也希望大家能關(guān)注他原文地...
    信仰與初衷閱讀 4,834評(píng)論 0 30
  • 第三章 數(shù)據(jù)庫系統(tǒng) 3.1 數(shù)據(jù)庫管理系統(tǒng)的類型 通常有多個(gè)分類標(biāo)準(zhǔn)。如按數(shù)據(jù)模型分類、按用戶數(shù)分類、按數(shù)據(jù)庫分布...
    步積閱讀 3,115評(píng)論 0 7
  • 一、MySQL優(yōu)化 MySQL優(yōu)化從哪些方面入手: (1)存儲(chǔ)層(數(shù)據(jù)) 構(gòu)建良好的數(shù)據(jù)結(jié)構(gòu)??梢源蟠蟮奶嵘覀僑...
    寵辱不驚丶?xì)q月靜好閱讀 2,654評(píng)論 1 8
  • 索引 數(shù)據(jù)庫中的查詢操作非常普遍,索引就是提升查找速度的一種手段 索引的類型 從數(shù)據(jù)結(jié)構(gòu)角度分 1.B+索引:傳統(tǒng)...
    一凡呀閱讀 3,217評(píng)論 0 8

友情鏈接更多精彩內(nèi)容