MySql的基操勿六

来源:https://www.cnblogs.com/dabaine/archive/2018/12/10/10094485.html
-Advertisement-
Play Games

2018/12/6 星期四 19:34:07 authot by dabaine 資料庫註釋; 這就是註釋 / ..... / 這也是註釋 創建庫; create databse [if not exists] dabaine [character set "utf8"]; 查看所有資料庫; sho ...


2018/12/6 星期四 19:34:07

authot by dabaine

資料庫註釋;

 -- 這就是註釋
/*.....*/ 這也是註釋

創建庫;

create databse [if not exists] dabaine [character set "utf8"];

查看所有資料庫;

show databses;

查看資料庫結構:

show create database dabaine;

查看當前資料庫;

select database();

修改資料庫;

alter database dabaine [character set "gbk"];

刪除資料庫;

drop database [if exists] dabaine;

使用資料庫;

use database;

創建表;

create table dabaine(
    id smallint(10) primary key not null auto_increment,          
    name varchar(25) not null,
    gender boolean not null
);

刪除表;

drop table dabaine;

查看表結構;

eg1:show create table dabaine;
eg2:show columns from dabaine;

查看表的全部信息;

desc dabaine;

修改表結構;

增加欄位:
alter table dabaine add [column],add [column]......;
修改類型:
alter table dabaine modify colum_name attribute [first|after column_name] colum_name;
修改列名:
alter table dabaine change column_name new_column_name type [約束條件];
刪除欄位:
alter table dabaine drop [column]; 
重命名:
rename table table_name to new_table_name;

修改表內容;

插入:
eg1:insert into dabaine (id, name) values(1,"dabaine");
eg2:insert into dabaine set id = 2,name="dabaine";
更新:
update dabaine set name="cody" where name="dabaine";    
刪除:
eg1:delete from dabaine where name = "cody";
eg2:truncate table dabaine; --把表摧毀,重新創建一張新表;

查詢順序;

select [distinct] *|field ... from dabaine
    where (不分組篩選)
    group by field
    having (分組後篩選)
    order by field
    limit 

查詢別名;

selct distinct id + 10 as id from dabaine;

執行順序;

from,where,select,group by,having, order by

聚合函數;

select name, sum(grade) from dabaine group by name;
ifnull(grade,0) --如果grade為空,則給它定為0;

外鍵約束;

創建主表:
create table class(
    id int(10) primary key auto_increment,
    name varchar(20),
    age int(5)
);
主表添加數據(多條):
insert into class(name,age) values
        ("cody",18),
        ("solider",19),
        ("guan",21),
        ("lee",22),
        ("strong",28),
        ("pig",38);
創建子表:
create table student(
    id int(10) primary key auto_increment,
    name varchar(20),
    age int(5),
    teacher_id int(10), --綁定外鍵的欄位要和主表中的欄位類型保持一致;
    constraint dabaine --給外鍵命名大白訥
    foreign key (teacher_id) --給子表的屬性選擇外鍵綁定 
    references class(id) --映射主表的屬性(追隨主表的id欄位)
);
子表添加數據:
insert into student(name,age,teacher_id) values
    ("cody",18,1),
    ("solider",19,2),
    ("guan",21,3),
    ("lee",22,4),
    ("strong",28,5),
    ("pig",38,6);
這時,主表和子表已經有關聯了,不可以隨便刪除主表的記錄;
增加外鍵:
alter table son_table_name add constraint cody
        foreign key(son_table_field)
        references primary_table(field);
刪除外鍵:
alter table son_table_name drop foreign key cody;

級聯刪除(cascade);

create table studentNew(
    id int(10) primary key auto_increment,
    name varchar(20),
    age int(5),
    teacher_id int(10),
    constraint cody foreign key (teacher_id) 
    references class(id) 
    on delete cascade --級聯刪除
);
    constraint cody foreign key (teacher_id) 
    references class(id) 
    on delete set null --主表刪除後,子表記錄設置為空值,且子表的欄位屬性不能設置為not null;
            on delete restrict --拒絕對主表進行更新刪除操作;
    on delete no action --類似於restrict

多表查詢;

笛卡爾積連接:
        A表中的全部數據m條 * B表中的全部數據n條;
連接查詢~內連接:
    inner join
    eg1:select tableA.id,tableA.name,tableB.name from
            tableA,tableB where tableA.id = tableB.tableA_id
    eg2:select tableA.id,tableA.name,tableB.name from tableA 
            inner join tableB on tableA.id = tableB.tableA_id
            +---------+----+---------+
            | name    | id | name    |
            +---------+----+---------+
            | cody    |  1 | cody    |
            | solider |  2 | solider |
            | guan    |  3 | guan    |
            | cody    |  4 | lee     |
            | strong  |  5 | strong  |
            | lee     |  6 | pig     |
            +---------+----+---------+
連接查詢~左外連接(左連接):
    left join
    select tableA.id,tableA.name,tableB.name from tableA 
            left join tableB on tableA.id = tableB.tableA_id
    --左連接以左表為主,select所選擇的欄位,左表中的記錄會全部顯示,而右表會去匹配左表裡的記錄,沒有的則顯示空值;
            +----+---------+---------+
            | id | name    | name    |
            +----+---------+---------+
            |  1 | cody    | cody    |
            |  2 | solider | solider |
            |  3 | guan    | guan    |
            |  4 | lee     | cody    |
            |  5 | strong  | strong  |
            |  6 | pig     | lee     |
            +----+---------+---------+
連接查詢~右外連接(右連接):
    right join
    類似左連接,以右表為主;
            +------+---------+---------+
            | id   | name    | name    |
            +------+---------+---------+
            |    1 | cody    | cody    |
            |    4 | lee     | cody    |
            |    2 | solider | solider |
            |    3 | guan    | guan    |
            |    6 | pig     | lee     |
            |    5 | strong  | strong  |
            | NULL | NULL    | pig     |
            +------+---------+---------+

嵌套;

查詢嵌套:
    select * from table_name where  field in (select field from table_name);
複製表:
    create table new_table(select * from old_table); --原表中的約束不會複製過來,需要重新添加
    selcet * from table_name where exists 
        (selcet field from table_name where....)
    --exists 後面的語句會返回一個布爾值,true則執行前面的select語句,
                flase 則返回空值;

索引;

    unique(唯一索引),fulltext(全局索引),spatial(空間索引),index|key(普通索引)
添加索引:
    eg1:create 
        [unique|fulltext|spatial] index|key 
        index_name on table_name (欄位名[(長度)] [asc|desc]);
    eg2:alter table table_name 
        add [unique|fulltext|spatial] index|key index_name (欄位名[(長度)] [asc|desc]);
刪除索引:
    drop index index_name on table_name;
    unique:唯一索引的欄位不能重覆;
    多列索引:給多個欄位添加索引 (field1,field2...)

事務;

     start transaction; --開啟事務
 Rollback; --回滾事務(撤銷)
 Commit;  --提交事務;
 savepoint; 保留點,事務處理中的臨時占位符;

 savepoint name;
 rollback to svaepoint_name;

存儲過程;


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1、 描述GPT是什麼,應該怎麼使用 Linux中磁碟分區分為MBR和GPT。 MBR全稱為Master Boot Record,為主引導記錄,是傳統的分區機制,應用於絕大多數使用的BIOS的PC設備。 MBR分區的特點: 1、MBR支持32位和64位的系統 2、MBR支持分區數量有限 3、MBR只 ...
  • 前一陣子租了一臺伺服器主機來玩,正好周末有時間研究了一下怎麼搭建ftp server。 準備。首先要下載filezilla client和filezilla server, 下載地址: server: https://filezilla-project.org/download.php?type=s ...
  • Rsync服務常見問題彙總 1 客戶端的錯誤現象:No route to host rsync服務端開啟的iptables防火牆 [root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup rsync: faile ...
  • 今天寫定時腳本時,用到監控伺服器是否備份成功,配置sentmail和postfix總是出問題,原本想只是接受個信息,沒必要那麼麻煩,直接配置mailx就能滿足了,具體配置如下: 1、安裝mailx yum install mailx -y 2、編輯發送的配置文件(修改/etc/mail.rc) vi ...
  • 個人原創,轉自請在文章頭部顯眼位置註明出處:https://www.cnblogs.com/sunshine5683/p/10091341.html find命令的各種搜索 一、根據文件名進行查找 命令:find [路徑,其中/表示根目錄搜索,即全盤搜索] -name [文件名] 實例: 二、根據文 ...
  • 今天寫定時任務時,出現奇怪的提示,有的時候每敲一下回車,也出現奇怪的提示 You have new mail in /var/spool/mail/root 阿西吧........表示很煩...究竟是為什麼呢?在網上一查,原來是 Linux 系統經常會自動發出一些郵件來提醒用戶系統中出了哪些問題(收 ...
  • 記憶體管理的一種頁面置換演算法,對於在記憶體中但又不用的數據塊(記憶體塊)叫做LRU,操作系統會根據哪些數據屬於LRU而將其移出記憶體而騰出空間來載入另外的數據。 什麼是LRU演算法? LRU是Least Recently Used的縮寫,即最近最少使用,常用於頁面置換演算法,是為虛擬頁式存儲管理服務的。 關於操 ...
  • 文件和目錄 cd /home 進入 '/ home' 目錄' cd .. 返回上一級目錄 cd ../.. 返回上兩級目錄 cd 進入個人的主目錄 cd ~user1 進入個人的主目錄 cd - 返回上次所在的目錄 pwd 顯示工作路徑 ls 查看目錄中的文件 ls -F 查看目錄中的文件 ls - ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...