欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mysql?sql常用語句大全

 更新時間:2022年06月18日 08:44:27   作者:神奇的老黃  
這篇文章主要介紹了mysql?sql常用語句大全,主要包括操作數(shù)據(jù)庫的命令,修改表的命令及對數(shù)據(jù)庫的操作,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下

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

show databases; 查看所有的數(shù)據(jù)庫
create database test; 創(chuàng)建一個叫test的數(shù)據(jù)庫
drop database test;刪除一個叫test的數(shù)據(jù)庫
use test;選中庫 ,在建表之前必須要選擇數(shù)據(jù)庫
show tables; 在選中的數(shù)據(jù)庫之中查看所有的表
create table 表名 (字段1 類型, 字段2 類型);
desc 表名;查看所在的表的字段
drop table 表名; 刪除表
show create database 庫名;查看創(chuàng)建庫的詳細信息
show create table 表名; 查看創(chuàng)建表的詳細信息

二、修改表的命令

修改字段類型 alter table 表名 modify 字段 字段類型;
添加新的字段 alter table 表名 add 字段 字段類型
添加字段并指定位置  alter table 表名 add 字段 字段類型   after 字段;
刪除表字段  alter table 表名 drop 字段名;
修改指定的字段  alter table 表名 change 原字段名字  新的字段名字 字段類型

三、對數(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ù)會被全部刪除
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 升序(默認)
    5.1 通過字段來排序
    例如 :select * from star order by money desc, age asc;   
    5.2 多字段排序
    select 字段 from 表 order by 字段1  desc |asc,...字段n desc| asc;
6.常用的統(tǒng)計函數(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)計: select count(sex) from star group by sex;
7.分組 select * from 表名  limit 偏移量,數(shù)量
    說明:
        8.1.不寫偏移量的話就是默認的為0
        8.2.實現(xiàn)分頁的時候必須寫偏移量
        偏移量怎么計算?:
        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;
兩個表同時更新
update user u, goods g set u.gid=12,g.price=1 where u.id=2 and u.gid=g.gid;

五、DCL 數(shù)據(jù)控制語言

創(chuàng)建用戶:create user'xiaoming'@'localhost' identified by '666666';
授權(quán)用戶:grant all on test.*to'xiaoming'@'localhost';
刷新權(quán)限:flush privileges;
取消授權(quán):revoke all on test.* from 'xiaoming'@'localhost';
刪除用戶: drop user'xiaoming'@'localhost';

六、DTL 數(shù)據(jù)事務語言

開啟事務:set autocommit=0;
操作回滾:rollback;
提交事務:commit;

下面看下mysql更新語句數(shù)量較多時的處理

示例

update user1 a,user2 b set a.createid = 2 where a.id in (b.id) ?

到此這篇關(guān)于mysql sql常用語句大全的文章就介紹到這了,更多相關(guān)mysql sql常用語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論