柚子快報邀請碼778899分享:數(shù)據(jù)庫 SQL語句
柚子快報邀請碼778899分享:數(shù)據(jù)庫 SQL語句
目錄
創(chuàng)建數(shù)據(jù)庫
創(chuàng)建庫時判斷數(shù)據(jù)庫是否存在,不存在則創(chuàng)建
查看所有數(shù)據(jù)庫
使用指定數(shù)據(jù)庫
查看當前指定數(shù)據(jù)庫包含的數(shù)據(jù)表
查看數(shù)據(jù)庫的結(jié)構(gòu)定義信息
刪除數(shù)據(jù)庫
修改數(shù)據(jù)庫的字符集為UTF-8
創(chuàng)建表
查看表結(jié)構(gòu)
查看創(chuàng)建表的sql語句
修改表名
添加一個新的字段
修改字段名
修改字段類型(注意類型修改前后數(shù)據(jù)是否兼容 )
刪除一個字段
刪除表
刪除表時判斷表是否存在,若存在則刪除
DML(數(shù)據(jù)操作語言)
插入數(shù)據(jù)(insert into)
修改數(shù)據(jù)(update)
刪除數(shù)據(jù)(delete)
DCL(數(shù)據(jù)控制語言)
創(chuàng)建用戶
給用戶授權(quán)
撤銷授權(quán)
查看用戶權(quán)限
刪除用戶
修改用戶密碼(以root身份)
DQL(數(shù)據(jù)查詢語言)
基礎(chǔ)查詢
條件查詢
模糊查詢
字段控制查詢
數(shù)據(jù)相加查詢
給列名添加列名
排序
聚合函數(shù)(count、max、min、sum、avg)
分組查詢(group by)
limit :限定查詢結(jié)果的起始行、以及總行數(shù)。
多表連接查詢:表連接分為內(nèi)連接和外連接
主鍵(添加、刪除)
索引(創(chuàng)建、刪除)
創(chuàng)建數(shù)據(jù)庫
create database 庫名;
創(chuàng)建庫時判斷數(shù)據(jù)庫是否存在,不存在則創(chuàng)建
create database if not exists 庫名;
查看所有數(shù)據(jù)庫
show databases;
使用指定數(shù)據(jù)庫
use 庫名;
查看當前指定數(shù)據(jù)庫包含的數(shù)據(jù)表
show tables;
查看數(shù)據(jù)庫的結(jié)構(gòu)定義信息
show create database 庫名;
刪除數(shù)據(jù)庫
drop database 庫名;
修改數(shù)據(jù)庫的字符集為UTF-8
alter database 庫名 character set? utf8;
創(chuàng)建表
create table 表名(字符1 類型1;....);
查看表結(jié)構(gòu)
desc 表名;
查看創(chuàng)建表的sql語句
show create table 表名;
修改表名
alter table 表名 rename to 新表名;
添加一個新的字段
alter table 表名 add 字段;字段類型;
修改字段名
alter table 表名 rename column 字段名 to 新字段名;
修改字段類型(注意類型修改前后數(shù)據(jù)是否兼容 )
alter table 表名 modify column 字段名? 新字段類型;
刪除一個字段
alter table 表名 drop 字段名;
刪除表
drop table 表名;
刪除表時判斷表是否存在,若存在則刪除
drop table if exists 表名;
DML(數(shù)據(jù)操作語言)
插入數(shù)據(jù)(insert into)
insert into 表名(列名1,列名2,);
insert into 表名 values (值1 ,值2,);
INSERT INTO stu VALUES('s_1002', 'liSi', 32, 'female');
修改數(shù)據(jù)(update)
update 表名 set 列名1=值1,列名n=值n[where 條件];
UPDATE stu SET sname=’liSi’, age=’20’WHERE age>50 AND gender=’male’;
刪除數(shù)據(jù)(delete)
delete from 表名 [where 條件];
DELETE FROM stu WHERE sname=’chenQi’ OR age > 30;
truncate table 表名;(刪除記錄無法回滾)
DCL(數(shù)據(jù)控制語言)
創(chuàng)建用戶
create user '用戶名'@地址 identfied by '密碼';
CREATE USER ‘user2’@’%’ IDENTIFIED BY ‘123’;
給用戶授權(quán)
grant 權(quán)限1,權(quán)限n on 數(shù)據(jù)庫 .*to '用戶名’ @地址;
GRANT CREATE,ALTER,DROP,INSERT,UPDATE,DELETE,SELECT ON mydb1.* TO 'user1'@localhost; GRANT ALL ON mydb1.* TO user2@localhost;
撤銷授權(quán)
revoke 權(quán)限 1,權(quán)限n on 數(shù)據(jù)庫.*from '用戶名'@地址;
查看用戶權(quán)限
show grants for '用戶名'@地址;
刪除用戶
drop user '用戶名'@地址;
修改用戶密碼(以root身份)
use mysql
alter user '用戶名'@localhost identified by '新密碼';
DQL(數(shù)據(jù)查詢語言)
基礎(chǔ)查詢
select * from 表名;(查詢所有列)
select 列名 1,列名n from 表名;(查詢指定列)
條件查詢
在where中運算:
=、!=、<>、<、<=、>、>=;BETWEEN…AND;IN(set);IS NULL;AND;OR;NOT;
模糊查詢
select 字段 from where 某字段 like 條件
%?:表示任意 0 個或多個字符。若是中文,請使用兩個百分號(%%)表示
_: 表示任意單個字符。匹配單個任意字符,它常用來限制表達式的字 符長度語句。
字段控制查詢
select distinct 123?from 456;
數(shù)據(jù)相加查詢
select *,a + b from abc;
若b內(nèi)有空,select *,a + ifnull(b,0) from abc;
給列名添加列名
select *,a+ifnull(b,0)as t from abc;
可省略as? select *,a+ifnull(b,0)t from abc;
排序
升序:select * from abc order? by a asc;
降序:select * from abc order by b desc;
升序+降序:select * from abc order by a desc,b asc;
聚合函數(shù)(count、max、min、sum、avg)
統(tǒng)計數(shù):select count(*) as a from abc;
select count(a) a from abc;
求和:select? sum(a) from abc;
平均值:select avg(a) from abc;
查詢最高與最低:select max(a),min(a) from abc;
分組查詢(group by)
select ab? from abc group by a;
HAVING 子句:SELECT deptno, SUM(sal)
? ? ? ? ? ? ? ? ? ? ? ? ? ?FROM emp
? ? ? ? ? ? ? ? ? ? ? ? ? ?GROUP BY deptno
? ? ? ? ? ? ? ? ? ? ? ? ? ?HAVING SUM(sal) > 9000;
limit :限定查詢結(jié)果的起始行、以及總行數(shù)。
select * from abc limit 0,5;
多表連接查詢:表連接分為內(nèi)連接和外連接
內(nèi)連接:選出兩張表中相互匹配的記錄
select staff.name,deptname from staff,deptno where staff.name=deptno.name;
外連接:選出其他不匹配的記錄
左連接:select staff.name,deptname from staff left join deptno onstaff.name=deptno.name;
由連接:select deptname,deptno.name from staff right join deptno ondeptno.name=staff.name;
主鍵(添加、刪除)
alter table table_name add primary key (column_name);
alter table table_name drop primary key (column_name);
索引(創(chuàng)建、刪除)
create index index_name on table_name(column_name);
create unique index index_name on table_name(column_name);
drop index index_name;
柚子快報邀請碼778899分享:數(shù)據(jù)庫 SQL語句
好文閱讀
本文內(nèi)容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。