ubuntu SQL 學(xué)習(xí)筆記 (基礎(chǔ)語法+mysql安裝)
mysql? 學(xué)習(xí)筆記
1.安裝mysql(ubuntu下)
sudo apt install mysql-server
sudo mysql_secure_installation? ? #初始化安全設(shè)置
systemctl status mysql.service? #查看mysql運行情況
systemctl start mysql.service? #啟動mysql
systemctl stop mysql.service? ?#停止mysql
mysql -u root -p? ?#登錄mysql 的root用戶
datagrip? Ubuntu安裝? ?
2.數(shù)據(jù)模型? ‘
關(guān)系型數(shù)據(jù)庫 (RDBMS)? 通過表來存儲數(shù)據(jù)
mysql? 的數(shù)據(jù)模型? (mysql數(shù)據(jù)管理系統(tǒng)DBMS)
3.SQL 的語法??
SQL分類? ??
1.DDL? 數(shù)據(jù)定義語言? ?定義數(shù)據(jù)庫對象(庫 表 字段? 索引)
2.DML? 數(shù)據(jù)操作語句? ?對于表當(dāng)中的數(shù)據(jù)進(jìn)行增刪改
3.DQL? 數(shù)據(jù)查詢語言? ?查詢數(shù)據(jù)庫中表的記錄
4.DCL? 數(shù)據(jù)控制語言? ?創(chuàng)建數(shù)據(jù)庫用戶,控制用戶的權(quán)限
? ?4.查詢
? ? ?show databases;? //查詢所有數(shù)據(jù)庫??
select database();? //查詢當(dāng)前數(shù)據(jù)庫
創(chuàng)建數(shù)據(jù)庫
create database [if not exists] 數(shù)據(jù)庫名?
刪除數(shù)據(jù)庫
drop database [if exists] 數(shù)據(jù)庫名;
使用數(shù)據(jù)庫
use 數(shù)據(jù)庫名 ;
查詢當(dāng)前數(shù)據(jù)所有表?
show tables;? //需要先進(jìn)入數(shù)據(jù)庫
查詢表結(jié)構(gòu)
desc 表名;? ?
查詢指定表的建表語句
show create table 表名;
創(chuàng)建表 e'x
create table 表名(
字段 字段類型 [comment 字段1注釋],
.......
)[comment 表注釋];
表操作??
添加字段
alter table 表名 add 字段名 類型(長度) [comment 注釋] [約束]
修改數(shù)據(jù)類型
alter table 表名 modify 字段名 新數(shù)據(jù)類型(長度);
修改字段名 和 字段類型??
alter table 表名 change 舊字段名 新字段名 類型(長度) [comment 注釋] [約束]
刪除字段
alter table 表名 drop 字段名;
修改表名
alter table 表名 rename to 新表名;
刪除表
drop table [if exists] 表名
刪除指定表
truncate table 表名;
表添加數(shù)據(jù)
1.給指定字段添加數(shù)據(jù)
insert into 表名(字段名1,字段名2,...) values(值1,值2,...);
2.給全部字段添加數(shù)據(jù)
insert into 表名 values(值1,值2,...);
3.批量添加數(shù)據(jù)
insert into 表名(字段名1,字段名2,...) values(值1,值2,...),(值1,值2,...);
insert into 表名 values(值1,值2,...),(值1,值2,..);
表修改數(shù)據(jù)
1.update 表名 set 字段名1=值1,字段名2=值2,..[where 條件];
刪除表中數(shù)據(jù)
2.delete from 表名 [where 條件];
查詢表中的記錄
select?
字段列表
from 表名列表
where?
條件列表
having?
分組后條件列表
orderby?
排序字段列表
limit?
分頁參數(shù)
查詢語法
查詢多個字段
select 字段1,字段2,... from 表名
設(shè)置別名
select 字段1 [as 別名]... from 表名
去除重復(fù)記錄
select distinct 字段列表 from 表名
條件查詢
select 字段 from 表 where 條件列表;
聚合函數(shù)
count? max min avg sum?
select 聚合函數(shù)(字段列表) from 表名;
分組查詢
select 字段列表 from 表名 [where 條件] group by 分組字段名 [having 分組后過濾條件];?
? 排序查詢? 方式 asc升序 desc 降序
select 字段列表 from 表名 order by 字段 排序方式;
分頁查詢
select 字段列表 from 表名 limit 起始索引,查詢記錄;? ??
起始索引從0開始 起始索引=(查詢頁碼-1)*每頁顯示記錄數(shù)
數(shù)據(jù)庫控制 (管理用戶訪問權(quán)限)
管理用戶
1.查詢用戶
use mysql;
select * from user;
2.創(chuàng)建用戶
create user '用戶名'@'主機(jī)名' identified by '密碼'
3.修改用戶密碼
alter user '用戶名'@'主機(jī)名' identified with mysql_native_password by '新密碼';
4.刪除用戶
drop user '用戶名'@'主機(jī)名';
用戶權(quán)限控制
權(quán)限類型
all? 所有權(quán)限? ? ?select? 查詢數(shù)據(jù)? ?insert? 插入語句? update? 修改數(shù)據(jù)? delete 刪除數(shù)據(jù)
alter 修改表? drop 刪除數(shù)據(jù)庫/表/視圖? ?create 創(chuàng)建數(shù)據(jù)庫/表
語法
1.查詢權(quán)限
show grants for '用戶名'@'主機(jī)名';
2.授予權(quán)限
grant 權(quán)限列表 on 數(shù)據(jù)庫名.表名 to '用戶名'@'主機(jī)名';
3.刪除權(quán)限
revoke 權(quán)限列表 on 數(shù)據(jù)庫名.表名 from '用戶名'@'主機(jī)名';