1. 程式人生 > 其它 >盤點一個`07Apr2022`格式日期轉換的基礎題目

盤點一個`07Apr2022`格式日期轉換的基礎題目

總結+參考整理有關MySQL相關知識

資料庫增刪改查(平時我們很少幾乎不會用到)

1.建立一個數據庫     create database 庫名;
1.2建立一個db2資料庫並設定預設編碼為utf8    create database db2 default charset='utf8';
2.顯示有哪些資料庫   show databases;
3.進入某個資料庫    use 庫名;
4.刪除某個資料庫   drop database 庫名;

表結構知識整理+增刪改查(幾乎不會用到)

1、表中常用的資料型別

Int   儲存整數(整型:正負10位整數)              int(M)   M指最大顯示寬度    varchar 儲存字元
bigint    儲存整數(大整型:(正負20位整數)  char(M)  M指最大儲存字元       char    儲存字元
float      儲存小數                     date    儲存日期(年月日)         datetime 儲存日期(年月日時分秒)
2、約束

約束用於對錶中欄位進行限制,保證表中資料的正確性和唯一性
2.1.primary key     主鍵約束
說明:非空,唯一,用於唯一標識對應的記錄。類似身份證。
2.2 foreign key      外來鍵約束(說明:用於表與表建立關係模型,使表與表緊密的結合起來)
2.3 not null    非空約束 說明:欄位值不能為Null
2.4 default     預設值約束 說明:預設給欄位指定預設值
2.5 auto_increment      自增約束(說明:作用在整數型別,欄位預設從1開始自增)

2、建立一個表
create table 表名(欄位1名稱,資料型別,約束,備註,欄位2名稱,資料型別,約束,備註,欄位3名稱,資料型別,約束,備註,)

eg:建立一張學生表tb_user,學號id, 姓名name. 

create table student(id int(2) primary key comment'使用者編號id',name varchar(30)comment'使用者姓名',)engine=INNODB default CHARSET=utf8 comment'學生表';

3、查看錶結構 :desc 表名   

2.修改表名  alter table 表名 rename 新表名
3.修改表字段  alter table 表名 change 原欄位名 新欄位名 資料型別,約束
4.新增表字段,並放到第一個欄位前  alter table 表名 add 欄位名 資料型別 約束 first 


5.新增表字段,並放到某個欄位後  alter table 表名 add 欄位名 資料型別 約束 after 欄位名
6.同時新增兩個欄位,預設新增到欄位最後  alter table 表名 add(欄位1 資料型別,欄位2 資料型別)
7.刪除表字段  alter table 表名 drop 欄位
8.刪除表兩個欄位  alter table 表名 drop 欄位1,drop 欄位2
9.修改主鍵id為自增長  alter table 表名 change 欄位名 資料型別 auto_increment
10.刪除表  drop table 表名
11.建立資料庫設定編碼  create database db1 character set utf8
12.修改表修改表的編碼 alter table 表名 convert to character set utf8
13.修改表的備註 alter table 表名 comment '備註說明'
14.修改表字段的備註 alter table 表名 modify 欄位名 資料型別 comment '備註說明'
表內容知識整理+增刪改查(常用)

單表查詢

1、單篩選條件 select * from  表名  where   接滿足條件(欄位值=value,欄位值 != value或欄位值 ><value) (*代表所有欄位,同時如何想查某個欄位直接替代*就可以,同樣多欄位原理相同)

2、多篩選條件  select * from  表名  where  條件1 or 條件2 條件1 and 條件2  (or至少一個滿足,and必須兩個同時滿足)

3、查詢一個條件範圍內的資料  select * from 表名 where 欄位 between m and n  between...and ... 指定一個範圍

4、查詢欄位滿足在(不在)指定的集合中的資料  select *   from 表名 where 欄位 in / not in(值1,值2,值3)

5、查詢欄位值為(不為)空的資料  select * from 表名 where 欄位 is /is not null 

13.查詢某個欄位模糊匹配成功的資料;  select * from 表名 where 欄位 like  “%值%”   ( %用於匹配欄位開頭和結尾);

14.查詢限定的數量的資料;select *  from 表名 where 欄位 limit m,n  (m 指下標,n指限定的數量,下標為m的開始的n條資料)

15.  查詢的資料根據某個欄位從小到大排序  select * from 表名 order by 欄位 asc  (order by ...asc 從小到大排序)

16.  查詢的資料根據某個欄位從大到小排序  select * from 表名 order by 欄位 desc  order by ... desc    從大到小排序

17.  查詢的資料根據某個欄位進行分組  select * from 表名 group by 欄位  (group by ...    根據條件分組)

18.  查詢的資料根據某個欄位進行分組再條件過濾  select *   from 表名 group by 欄位 having 條件  (having跟在group by 後面,作用相當於where)

單表統計/求和/平均值/最大值/最小值/欄位值去重

1. 統計查詢資料的數量;  select count(*) from 表名
2. 查詢某個欄位求和;     select sum(欄位) from 表名
3. 查詢某個欄位進行平均值;   select avg(欄位)      from 表名
4. 查詢某個欄位最大值;    select max(欄位) from 表名
5. 查詢某個欄位最小值;    select min(欄位) from 表名
6. 對某個欄位進行去重;    select distinct(欄位) from 表名

Mysql: 增刪改語句

1.表中插入資料  insert into 表名 values(欄位1value,欄位2value,欄位3value...)

 2.一次性表中插入多條資料  insert into 表名 values(欄位1value,欄位2value,欄位 3value...),(欄位1value,欄位2value,欄位3value...)

3. 對錶中指定欄位插入資料  insert into 表名(欄位1,欄位2) values(欄位1值,欄位2值)

4. 刪除表中指定資料  delete from 表名 where 條件

5. 更新表中指定欄位資料  update 表名 set 欄位名=值 where 條件

Mysql: 備份表,備份資料

1.備份表,建立一個表與某個表相同(備份表結構);   create table 表1  like    表2

2.備份資料,把一個表的資料插入到另一個表; insert into 表名 select * from 表名    (注意點:插入的表必須要存在)

3.把一個表的某些欄位插入到一個新表中  insert into 表1(欄位1,欄位2) select 欄位1,欄位2 from 表2

4.複製表結構及資料到新表;   create table表1 as select * from表2;

子查詢

什麼是子查詢?

一個查詢巢狀另一個查詢 子查詢分

  1. 標量子查詢(返回一個值)  2. 列子查詢  (返回一個列)  3. 行子查詢  (返回一行多列)  4. 表子查詢  (返回一個表)

  1.標量子查詢: 把一個sql執行返回的一個值,作為另一個sql的一個條件
  2. 列子查詢: 執行一個sql把返回的一個列作為另一個sql條件  select *  from score a where a.id  in( select id from student where  name=‘zhangsan' or name='lisi')  列子查詢返回的是兩個欄位的值,列子查詢使用符號 in or  not  in
  4. 表子查詢:執行一個sql返回的是一個表;select name from (select * from student )a    子查詢返回的是一個表
   select * from student ,取了一個別名a 主查詢是從表a中查詢name

多表連線

1. 內連線inner  join  2.左連線left  join     3.右連線right  join

1. 內連線查詢(查詢兩個表都符合條件 的資料)
  基本格式;  select 欄位列表 from 表1 inner join 表2  on 表1.欄位=表2.欄位
  查詢學生的姓名和成績    select name,score from student inner join score  on student.id=score.id
2. 左連線查詢(左邊表中資料顯示全部)
  左邊表中顯示全部,右邊表符合條件 的顯示,不符合條件的以null填充
  基本格式;    select 欄位列表from 表1 left join 表2  on 表1.欄位=表2.欄位
  查詢學生的姓名和成績    select name,score from student left join score  on student.id=score.id
3. union連線(將兩條sql語句查詢的值連線在一起展示,得相同數量的列
  union連線的兩個表中,若有相同的記錄,則只顯示一條(即會去重);
  union all不會去重,直接把兩個表的全部內容抖列出來索引

索引

  一個索引是儲存在表中的資料結構,索引在表的列名 上建立。索引中包含了一個列的值,這些值儲存在一 個數據結構中。重點:索引是一                                        種資料結構
1 索引的作用    可以利用索引快速訪問資料庫中的特定資訊.
 2.索引分類
  a. 普通索引  b. 唯一索引  c. 主鍵索引
3檢視有哪些索引    show index from 表名
4.建立一個索引
  alter table table_name add index index_name(column_list); #建立普通索引
  alter table table_name add unique index_uni_name(column_list); #建立唯一索引
  alter table table_name add primary key(column_list); #建立主鍵索引
6.刪除一個普通索引    alter table 表名 drop index 索引名;
7.刪除主鍵索引    alter table 表名 drop primary key;
主鍵和外來鍵
主鍵和外來鍵是把多個表組織成一個有效的關係型資料庫的粘 合劑,主鍵和外來鍵對資料庫的效能和可用性起著很大作用
主鍵的作用
1. 唯一的標識一行  2. 作為一個被外來鍵有效引用的物件
外來鍵的作用
一張表記錄的資料太過於冗餘儘量讓表的資料單一化 儲存資料的一致性和完整性
注意點
資料庫型別必須是INNODB型別,MYSIAM型別不支援使用 外來鍵
.建立表時增加外來鍵約束
CREATE TABLE `student` (
  `sid` int(5) NOT NULL DEFAULT '0' COMMENT '學生id',
  `name` varchar(10) NOT NULL COMMENT '學生姓名',
  `age` int(3) DEFAULT '18' COMMENT '學生年齡',
  KEY `i_sid` (`sid`))ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='學生表';
 
CREATE TABLE `score` (
  `sid` int(5) NOT NULL DEFAULT '0' COMMENT '學生id',
  `score` int(3) DEFAULT NULL COMMENT '成績',
foreign key(sid)references student(sid) on update cascade on delete cascade
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='成績表';
外來鍵約束
主表student表 ,       從表score表
        
2.增加外來鍵約束(從表的欄位型別和大小要與主表的一致,主表的欄位必須有索引)
alter table score add constraint fk_student_sid foreign
key(sid) references student(sid) on update cascade on delete cascade;
3.檢視mysql所有的主鍵,外來鍵約束名稱
select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE;
4.刪除一個外來鍵
alter table score drop foreign key fk_student_sid

儲存過程

1.     什麼是儲存過程    

  儲存過程是完成特定功能的sql語句集合。通過編譯後儲存在資料 庫中,通過指定的儲存過程名稱呼叫執行它。儲存過程=sql語句集合+控制 語句
2.     Mysql5.0版本後支援儲存過程 

3.使用儲存過程的優點
  1.     儲存過程建立可以多次呼叫,不需要重新編寫儲存過程語句。儲存過程支援接收引數,返回輸出值  儲存過程加快程式的執行速度
  2.     儲存過程增加sql語句的功能和靈活性
  4.建立一個儲存過程

注意點:有4個地方不能加分號:儲存過程名後面,begin、end後面,while∙∙∙do後面         2.迴圈自增變數,要帶冒號和set 如: set i:=i+1;
 While迴圈:
drop procedure if exists sum;      #如果sum儲存過程存在則刪除
create procedure sum(n int)
begin
         declare sum int default 0;   #定義一個變數並設預設值為0
         declare i int default 0;      #定義一個變數並設預設值為0
         while i<=n do     #while迴圈
                set sum:=sum+i;
                set i:=i+1;
         end while;
         select sum;
end

If判斷:
drop procedure if exists panduan;
create procedure panduan(n int)
begin
            if (n>=0 && n<=59)then
              select '不及格';
            else if (n>=60 && n<=80)then
              select '良好';
            else if (n>=81 && n<=90)then
              select '優秀';
            else if (n>=91 && n<=100)then
              select '非常優秀';
            else
              select '輸入錯誤';
            end if; end if;     end if;     end if;
end
If判斷和while迴圈:
drop procedure if exists insert_2;
create procedure insert_2(n int)
begin     
            declare i int default 1;
            declare num int default 0;
            truncate student;#清空student表
            while i<=n do 
              select count(1) into num from student;#將sql查出來的統計值賦值給num
              if (num<100) then
                     insert into student(sid,name)values(i,concat('name',i));
              else
                     select 'student 表中已有1萬條資料,不能繼續添加了';
              end if;     set i:=i+1;
            end while;
            select * from student;
end
5.     呼叫一個儲存過程
call 儲存過程名稱()
6.     刪除一個儲存過程
drop procedure 儲存過程名稱
drop procedure if exists 儲存過程名稱 #加強程式碼的健壯性
7.查詢資料庫中的儲存過程
show procedure status
8.檢視儲存過程的建立程式碼
show create procedure儲存過