【MySQL】5分鐘學會數據庫基本操作(一)

(全文約5500字,預計閱讀時間5分鐘)


一、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操作

#Create
create database hzyc;
create database if not exists hzyc98 character set gbk;
#Retrieve
show databases;
show create database hzyc98;
#Update
alter database hzyc98 character set gbk;
#Delete
drop database hzyc98;
drop database if exists hzyc98; 
#查看當前使用的數據庫
select database();
show tables;
use hzyc98

1.操作表list:

表名/表頭爲:zoomlist

#查
show tables; -- show tables_in_hzyc98
desc 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;

/*設置類型:*/
 - intdouble(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:自定義名字展示,也可以不寫as
SELECT Name,scroe1+IFNULL(scroe2,0) 總分 FROM zoomlist; --ifnull遇到沒有值的直接給賦值爲0
SELECT Name,score1,score2,scroe1+IFNULL(scroe2,0) AS 總分 --顯示錶頭
FROM zoomlist,peoplelist; --從zoomlist、peoplelist裏面獲取

b.where條件:

* ><<=>==!=<>--不等號
* andornot --關鍵字比&&、||、!好用推薦
* 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的數據就不參與計算,不會報錯!

  1. count:統計個數
  2. 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)<6;

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個數據

四、約束

  • 約束:
    1. 主鍵約束:primary key
    2. 非空約束:not null
    3. 唯一約束:unique
    4. 外鍵約束:foreign key

1.非空約束: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; 

實例操作:

唯一約束不可以直接modify刪除
drop刪除unique約束成功

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 key
alter 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_increment
alter table employee modify id int; 

五、總結

我們學習了SQL是什麼,做了一個簡單的入門,也列舉了一些MySQL的基本操作,還有查詢、約束是怎麼一回事。

但是我也是剛剛纔接觸MySQL,所以基本的操作手還比較生,要多學多練多去實踐才能出真知。

在之後我們還會學習到MySQL的多重關係、多表查詢、事務(還不太清楚是什麼)、JDBC各個語句、數據庫連接池druid、JDBCTemplate……還有好多東西要學,但是現在的任務還是在於把基本的東西梳理好,把基礎鞏固了纔是硬道理!!!

錯誤實例:

如添加數據的時候不寫列名,那必須給所有列值,不然報錯!
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章