資料庫 表單運用

何周钦發表於2024-10-26

一、進入資料庫操作介面
1、mysql -u root -p 敲回車 ,輸入密碼 ,進入資料庫操作介面
2、show databases 檢視所有的資料(如果沒有資料庫:建立資料庫 create database 庫名稱)
3、use 資料庫名 使用資料庫
4、show tables 顯示庫中的所有表
5、建表語句
格式: create table 表名(欄位名1 字元型別(字元長度),欄位名2 字元型別(字元長度));
案例:create table aa(id int(10),name varchar(20));
6、檢視錶結構:
desc 表名
7、在navicat 中=點選庫名點選查詢新建查詢===在新建查詢中輸入sql語句


8、插入資料:
(1)插入方式一:
格式:INSERT INTO 表名 VALUES(值1,值2);

(2)插入方式二:(插入部分欄位)
格式:INSERT into 表名(欄位名) VALUES(欄位值)
案例:INSERT into aa(id) VALUES("4")
(3)插入的中文字元變成?號
解決方案:
在建表時的語句後面新增:
DEFAULT charset=utf8;

9、刪除表格
drop table 表名

二、資料型別
1、數值型別
int 儲存型別
float 浮點數

2、字元型別
char
varchar

3、時間型別
date
time
datetime
year

約束:
約束用於對錶中欄位進行限制,保證表中資料的正確性和唯一性
1、primary key 主鍵約束
非空,唯一,用於唯一標識的記錄,類似身份證。
一個表中只用一個主鍵

2、not null 非空約束

3、 unique 唯一索引
保證欄位值具有唯一性,並且能為空,一個表中可以有多個唯一索引

4、default 預設值約束
定義:預設給欄位指定預設值

5、auto_increment 自增長約束(一般都是和主鍵同時使用)
作用:在整數型別,欄位預設值從1開始自增
(1)一般和主鍵約束一起使用,主要針對id
(2)每插入一條資料,就是在欄位上自動+1,

2、add 新增欄位
格式:ALTER TABLE 表名 add 欄位名 字元型別(字元長度);
案例:ALTER TABLE student2 add dcs int(20);

3、change 修改欄位
格式:ALTER TABLE 表名 change 舊欄位名 新欄位名 字元型別(字元長度);
案例:ALTER table student2 change dcs hzdcs int(19);

4、 drop 刪除欄位
格式:ALTER table 表名 drop 欄位名 ;
案例:ALTER table student2 drop hzdcs ;

5、rename 修改表名

6、modify after 欄位的調換
格式:ALTER table 表格 MODIFY 變動的欄位 欄位型別(欄位長度) after 指定欄位 ;
案例:ALTER table hz MODIFY math int(10) after id ;

7、first 新增欄位到第一位
格式:alter table 表名 add 表欄位 字元型別(字元長度) first ;
案例:alter table hz add no int(20) first ;

資料庫匯中:增、刪、改、查

一、查詢語句:
(1)查詢一個表中的所有資料
格式:select * from 表名 ; * 表示所有的欄位
案例:select * from hz ;
(2)查詢部分欄位(多個欄位用,分割)
格式:select 欄位1,欄位2 from hz ;
案例:select id,name from hz ;

(3)查詢欄位可以透過as 取別名
格式:
案例1( as寫,):
select id as " 編號",name as "姓名" from hz ;
案例2(可以省略 as不寫):
select id " 編號",name "姓名" from hz ;

(4)指定條件查詢內容:
where +條件
條件1:
比較運算:>,<,=,!=,<>,>=,<=
條件2:
and ,or ,between ....and ,in , is not null
案例1:= 等於
select id ,name from hz where id=1;
案例2:> 大於
select id ,name from hz where id>1;
案例3:<小於
select id ,name from hz where id<2;
案例4:<=小於等於
select id ,name from hz where id<=2;
(5)
案例5:>=大於等於
select id ,name from hz where id>=2;
(6)!=不等於
案例6:select id ,name from hz where id != 2;
(7)<>不等於
select id ,name from hz where id <> 2;

8)and 同時滿足條件
案例8; and 是同時滿足多個條件
select id ,name,math from hz where id > 2 and math>90;
(9)or 只要滿足其中一個條件 就顯示
select id ,name,math from hz where id > 6 or math>90;
(10)between 。。。and 在什麼範圍之間
案例:select * from hz where id BETWEEN 3 and 6 ;
備註:包含了本身,
(11)in 在一組資料中選擇(在資料彙總匹配)
案例:select * from hz where id in (1,3,8)
(12)not in 不在一組資料中選
案例:select * from hz where id NOT in (1,3,8)
(13)is null 為空的資料
select * from hz where class is null;
(14)is not nu 不為空的資料
select * from hz where class is not null;

order by 排序
(1)降序 (大到小)
order by desc
案例:select * from hz order by id desc ;

(2)升序(小到大)
asc 或不寫
案例:
select * from hz order by id asc ;
select * from hz order by id ;
(3)二次排序
案例:select * from hz order by math desc ,id desc;

like 模糊匹配查詢
%:表示匹配1個字元或多個字元
_ : 下滑線表示一個字元
案例1:匹配xx開頭的資料
select * from hz where math like "7%"; # 匹配7開頭的資料
案例2:匹配xx結尾資料
select * from hz where math like "%7"; #匹配7結尾的資料
案例3:匹配含有xx結尾資料
select * from hz where math like "%7%"; #匹配含有7的資料
案例4:匹配指定位數的資料
select * from hz where math like "7_"; #匹配具體位數的資料

limit (索引位,步長) 顯示指定的資料,限制;
根據索引位置來取值,從0開始,一個表第一行的索引就是0,第二行就是1
select * from hz limit 2; #表示取兩行資料, 2 表示步長
select * from hz limit 1,2#表示從索引1開始第二行,2表示步長2行
select * from hz limit 4,3 ;# 表示從索引4開始取值,第五行開始,取三行,

sql 聚合函式
max 最大數
案例1:select max(math) from hz ;
min最小數
案例2:select min(math) from hz ;
avg 平均值
案例3:
select avg(math) from hz ;
sum 求和
案例4:
select sum(math) from hz ;
count 統計
案例5:select count(math) from hz ;
distinct 去重
案例6:
select DISTINCT(math) from hz ;

group by ....... having
group by 是分組,一般不會單獨使用,通常和聚合函式組合使用
案例1:分組
select sum(math),class from hz GROUP BY class ;
案例2:分組 在條件 having
(1)select sum(math) s,class from hz GROUP BY class having s>200 ;
2)select sum(math) s,class from hz GROUP BY class having sum(math)>200 ;
注意:having 一般接在group by 後面

改:
update ......set......
格式:update 表名 set 欄位名=新值 where條件;

刪除:
(1)delete
格式:DELETE from 表名 where 條件;
DELETE from hz where id=1;
(2) truncate 快速刪除資料
格式:
truncate 表名 ;
案例:
truncate ff ;
(3)drop 刪除
格式:drop table 表名
案例:drop table emp ;
drop >truncate> delete

單行註釋:ctrl +/
取消註釋:shift+ctrl+/
多行註釋:選中多行 ,ctrl +/
取消註釋:選中多行 shift+ctrl+/

備份:
(1)備份表結構:
格式:create table 新表名 like 舊錶名;
create table emp_new like emp;

(2)備份表資料
格式:
INSERT into 新表結構 select * from 舊錶有資料 ;
(3)備份部分資料
格式:INSERT into 表名(欄位1,欄位2) select 欄位1,欄位2 from 舊錶 ;

(4)備份表結構和資料
格式:
create table 新表 as (select * from 原表);

在linux 中:
備份:
格式:mysqldump -u root -p 原庫>新sql指令碼名
案例:mysqldump -u root -p hz017>/home/hz17.sql
還原:
格式:mysql -u root -p 新庫<備份好的指令碼
案例:mysql -u root -p new</home/hz17.sql

相關文章