共計 4527 個字符,預計需要花費 12 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
這篇文章給大家分享的是有關 mysql 基本操作有那些的內容。丸趣 TV 小編覺得挺實用的,因此分享給大家做個參考。一起跟隨丸趣 TV 小編過來看看吧。
文章目錄
一、SQL 是什么?分類:二、關于數據庫 CRUD 操作 1. 操作表 list:2. 對表內數據進行操作:a. 查詢 b.where 條件:三、查詢 1. 排序查詢 2. 聚合函數(列的計算)3. 分組查詢 4. 排序查詢四、約束 1. 非空約束:not null2. 唯一約束實例操作:3. 主鍵約束:primary key4. 自動增長:auto_increment 五、總結錯誤實例一、SQL 是什么?
structured Query Language:結構化查詢語言
分類:
1) DDL(Data Definition Language)數據定義語言
用來定義數據庫對象:數據庫,表,列等。關鍵字:create, drop,alter 等
2) DML(Data Manipulation Language) 數據操作語言
用來對數據庫中表的數據進行增刪改。關鍵字:insert, delete, update 等
3) DQL(Data Query Language) 數據查詢語言
用來查詢數據庫中表的記錄(數據)。關鍵字:select, where 等
4) DCL(Data Control Language) 數據控制語言(了解)
用來定義數據庫的訪問權限和安全級別,及創建用戶。關鍵字:GRANT,REVOKE 等
二、關于數據庫 CRUD 操作
#Createcreate database hzyc;create database if not exists hzyc98 character set gbk;#Retrieveshow databases;show create database hzyc98;#Updatealter database hzyc98 character set gbk;#Deletedrop database hzyc98;drop database if exists hzyc98; #查看當前使用的數據庫 select database();show tables;use hzyc98
1. 操作表 list:
表名 / 表頭為:zoomlist
# 查 show tables; -- show tables_in_hzyc98desc zoomlist;# 增 create table zoomlist (Name varchar(30),
Age int,
ID int,
Height double(5,1))# 刪 drop table if exists zoomlist;# 改 alter table zoomlist rename to newzoomlist;alter table zoomlist character set gbk;alter table zoomlist add Name varchar(20);# 加列 alter table zoomlist change Age newAge int;alter table zoomlist modify Age char(8);alter table zoomlist drop Name;/* 設置類型:*/
- int、double(5,1)、varchar(20)
- date #yyyy-MM-dd
- datetime #yyyy-MM-dd HH:mm:ss
- timestamp# 時間戳 yyyy-MM-dd HH:mm:ss
2. 對表內數據進行操作:
# 除了數字,其他都需要引號來賦值 insert into zoomlist (Name, Age, ID, Height) value(美洲豹 ,5, 20201207 ,3.2);insert into zoomlist (美洲豹 ,5, 20201207 ,3.2);# 刪除 delete from zoomlist where [條件];delete from zoomlist;TRUNCATE TABLE zoomlist;# 修改 update zoomlist set Name = 大笨象 Age = 12 where address = 深圳 update zoomlist set address = 深圳
a. 查詢
# 查詢 #盡量不要用 * 先 desc 一下表里面有啥,然后在決定展示什么東西。SELECT * FROM zoomlist; SELECT Name,Age FROM zoomlist; -- 只顯示某個列, 方便查看!SELECT DISTINCT Name FROM zoomlist; -- 去除結果中 [完全重復] 的 SELECT Name,score1,score2,scroe1+scroe2 FROM zoomlist;--as: 自定義名字展示, 也可以不寫 asSELECT Name,scroe1+IFNULL(scroe2,0) 總分 FROM zoomlist; --ifnull 遇到沒有值的直接給賦值為 0SELECT Name,score1,score2,scroe1+IFNULL(scroe2,0) AS 總分 -- 顯示表頭 FROM zoomlist,peoplelist; -- 從 zoomlist、peoplelist 里面獲取
b.where 條件:
*、、=、=、=、!=、-- 不等號 * and、or、not -- 關鍵字比、||、!好用推薦 * BETWEEN...AND -- 范圍內都符合就行 * IN(集合) -- 特定值的范圍 * LIKE:模糊查詢(1)_: 單個任意字符;(2)%:多個任意字符 * IS NULL 例子:select Name, Age from Student where age between 12 and 20;select Name, Age from Student where age in (12,14,16,18);select Name, Age from Student where name like % 牛 % -- 查名字里面包含了牛的學生 select Name, Age from Student where name is not null; -- 查詢學生:名字空的不查
三、查詢 1. 排序查詢
select * from employee order by age;select * from employee order by age asc; -- 升序 select * from employee order by age desc; -- 降序 select * from employee order by age desc height desc; -- 第一個一樣的時候,才會用第二個方法排序(age 降序,身高降序)
2. 聚合函數(列的計算)
排除了 null 數據,并且有 null 的數據就不參與計算,不會報錯!
count:統計個數 min、max、sum、avg:求值
select count(*) from student;select count(ifnull(age,20)) from student; select count(age) from student;-- 如果沒有就不記錄 select count(id) from student; -- 我們一般選用主鍵來統計個數 select max(age) from student;select min(age) from student;select sum(age) from student;select avg(age) from student;
3. 分組查詢
group by 之后就是兩個不同的組別了,他們不能再去查看一個獨立的個體了。
分組之后查詢的字段:分組字段、聚合函數。where 和 having 的區別?
where 在分組前限定,having 在分組之后限定;where 不符合條件的不參與分組,having 不符合條件不會顯示;只有 having 可以后跟聚合函數判斷。
select sex,count(name) from employee group by sex having count(name) select sex,count(name) from employee where name = 張四 group by sex ;
4. 排序查詢
limit 是一個 MySQL 的方言,用于分頁
SELECT * FROM student LIMIT 0,5; -- 第 1 頁,從 0 索引開始,讀 5 個數據 SELECT * FROM student LIMIT 7,10; -- 第 2 頁,從 7 索引開始(第 8 個數據),讀 10 個數據
四、約束約束:主鍵約束:primary key 非空約束:not null 唯一約束:unique 外鍵約束:foreign key1. 非空約束:not null
-- 建表時添加非空約束:create table employee(name char(30),
sex char(8) not null
alter table employee modify sex char(8) not null; -- 添加非空約束
alter table employee modify sex char(8); -- 破除非空約束
2. 唯一約束
只可以有一個 null 值,不能再多了;
刪除約束只可以用 drop index 來刪除 unique 約束
-- 建表時添加唯一約束:create table employee(name char(30),
sex char(8),score int unique -- 分數要唯一
-- 添加唯一約束 alter table employee modify name char(8) unique;
-- 破除唯一約束 -- alter table employee modify sex char(8); 不可用 -- 破除 name 身上的 unique 約束用 drop index 除去索引 alter table employee drop index name;
實例操作:
3. 主鍵約束:primary key
一個表只有一個 primary key,非空且唯一
做記錄的唯一標識,相當于 index
-- 建表時添加主鍵約束:create table employee(
id int primary key, -- 給 id 加上主鍵約束
name char(30),
-- 添加唯一約束 alter table employee modify id int primary key;
-- 破除唯一約束 -- alter table employee modify id int; 不可用!-- 破除 id 身上的 primary key 約束只能用 drop primary keyalter table employee drop primary key;
4. 自動增長:auto_increment
只對數值有用,而且一般可以放給主鍵做自動增長
-- 建表時添加 auto_increment:create table employee(
id int auto_increment, -- 給 id 加上 auto_increment
name char(30),
-- 添加 auto_increment,自動從 1 開始 alter table employee modify id int auto_increment;-- 設置初值 alter table employee auto_increment = 100;
-- 破除 auto_incrementalter table employee modify id int;
感謝各位的閱讀!關于 mysql 基本操作有那些就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
向 AI 問一下細節
丸趣 TV 網 – 提供最優質的資源集合!