MySQL
- 1 針對資料庫進行操作
- 2 針對資料表進行操作
- 3 資料的增刪改查
- 3.1 資料的插入
- 3.2 資料的查找 (重要)
- 3.3 資料的修改
- 3.4 資料的洗掉
- 4 資料庫約束
- 4.1 約束型別
- 4.2 null 約束
- 4.3 unique 唯一約束
- 4.4 default 默認值約束
- 4.5 primary key 主鍵約束
- 4.6 foreign key 外鍵約束
??資料庫,是一類組織管理存盤資料的軟體,資料存盤在磁盤上,而mySQL是一個 C / S 結構的軟體,關系型資料庫,是以 資料表 的形式來進行組織資料的,
?
1 針對資料庫進行操作
(1) 創建資料庫 create database [資料庫名];
(2) 查看資料庫 show databases;
(3) 洗掉資料庫 drop database [資料庫名];
a. MySQL 支持 bin log
b. 備份,mysqldump 工具
c. 基于磁盤檔案進行還原,洗掉操作后,資料被標記為無效,如果用工具進行搶救可以找回,但若是創建了新的資料將其覆寫,便無法找回了,
(4) 選中資料庫 use [資料庫名];
?
2 針對資料表進行操作
(1) 查看都有什么表 show tables;
(2) 創建表 create table [表名] (列);
常用資料型別:int 、decimal、varchar、datetime;
(3) 查看表結構 desc [表名];
(4) 洗掉表 drop table [表名];
?
3 資料的增刪改查
3.1 資料的插入
insert into [表名] (列名) values (值);
?
3.2 資料的查找 (重要)
(1). 全列查找 select * from [表名];
(2). 指定列查找 select [列名] from [表名];
(3). 帶有運算式的查找 select id,name,english + 10 from [表名];
(4). 查找時給指定列起別名 select [列名] as [別名] from [表名];
(5). 查找某一列并且去重 select distinct [列名] from [表名];
(6). 排序:利用 order by 指定某一列進行排序,默認按照升序排序 select name,math from exam_result order by math;加上 desc 就是降序排序 select name,math from exam_result order by math desc;
(7).條件查詢
?a.基本查詢
select name,english from exam_result where english < 60;
select name,chinese + math + english from exam_result where chinese + math + english < 200;
?b.and 和 or
select name,chinese,english from exam_result where chinese > 80 and english > 80;
select name,chinese,english from exam_result where chinese > 80 or english > 80;
(如果 and 和 or 同時存在,注意and 優先級比 or 高)
?c.范圍查詢 —— between…and…
select name,chinese from exam_result where chinese between 80 and 90;
?d.范圍查詢 —— in
select name,chinese from exam_result where math in (70,89,90);
?e.模糊查找(只要有一部分相同即可,效率較低)
select name from exam_result where name like ‘j%’; (找出以 j 開頭的字串)
select name from exam_result where name like ‘%j’; (找出以 j 結尾的字串)
select name from exam_result where name like ‘%j%’; (找出包含 j 的字串)
select name from exam_result where name like ‘j_’; (找出 j 開頭的并且后面跟著一個字符的符串,每個下劃線代表一個字符)
select name from exam_result where chinese like ‘8%’; (找出 chinese 值為 80 多的資料)
?f.空值查詢
select * from exam_result where chinese = math;(不安全的等號,所以結果大概率有誤)
?? select * from exam_result where chinese <=> math;(安全的等號,選擇出 chinese值 和 math值 相等的)
select * from exam_result where chinese is null;
(8). 分頁查詢
select * from exam_result limit 3; (查詢前三行)
select * from exam_result limit 3 offset 0;(查詢 0 開始的三條資料)
select * from exam_result limit 2,3;(從第 2 條開始查找三條資料)
?
3.3 資料的修改
update exam_result set chinese = 99 where name = ‘tom’;(將 tom 的 chinese值 改為99)
update exam_result set math = math + 10 order by chinese + math + english limit 3;(將總成績后三名的同學的數學成績加 10 分)
?
3.4 資料的洗掉
delete from exam_result where name = ‘tom’ and id = 10;
?
4 資料庫約束
4.1 約束型別
not null :指某列不能存盤空值;
unique:保證某列的每行必須有唯一的值;
default:規定沒有給列賦值時的默認值;
primary key:not null 和 unique 的結合,確保某列(或兩個列多個列的結合有唯一標識,有助于更容易、更快速地找到表中的一個特定的記錄);
foreign key:保證一個表中的資料匹配另一個表中的值的參照完整性;
4.2 null 約束
在創建表的時候指定某列不為空:
create table student (id int not null,name varchar(50));
4.3 unique 唯一約束
指定某列,要求其每一行都是唯一的、不重復的:
create table student (id int not null,name varchar(50) unique);
4.4 default 默認值約束
例如,指定插入資料時,name 列為空,默認值為 unkown:
create table student (id int not null,name varchar(50) unique default ‘unkown’);
4.5 primary key 主鍵約束
設定 id 為主鍵,主鍵是 not null 和 unique 的結合,是一條記錄的唯一身份標識,一個表中只能有一個主鍵:
create table student (id int primary key,name varchar(50) unique);
對于整數型別的 主鍵,搭配 auto_increment 來使用,插入資料對應欄位不給定值時,使用最大值 + 1,
create table student (id int primary key auto_increment,name varchar(50) unique);
4.6 foreign key 外鍵約束
把上面的學生表洗掉,我們再來創建一個班級表 class ,id 為主鍵 :
create table class (id int primary key ,name varchar(50));
create table student (id int primary key auto_increment,name varchar(50),classId int,foreign key (classId) references class(id));
外鍵用于關聯其他表的主鍵或唯一鍵:
foreign key (classId) references class(id) ,其中對當前表的classId 這一列進行約束,class(id) 和另外一個 class 表建立其中的 id 列關聯起來,(外鍵約束就是要求當前表里面的 classId 欄位的值,必須在 class 表的 id 中出現過才可以,并且注意:id 必須是 class 表的主鍵)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/276260.html
標籤:其他
