MySQL基本語法

来源:https://www.cnblogs.com/Super-Lee/archive/2019/03/03/10467038.html
-Advertisement-
Play Games

MySQL基本語法 資料庫操作 增 格式: create database 表名; 示例: 刪 格式: drop database 表名; 示例: 改 直接在文件夾中修改 查 格式: show databases; 示例: 數據表操作 註意:刪、改、查等操作是在使用use 表名之後進行的 增 格式: ...


MySQL基本語法

資料庫操作

格式:

create database 表名;

示例:

create database mydatabase;

格式:

drop database 表名;

示例:

drop database mydatabase;

直接在文件夾中修改

格式:

show databases;

示例:

show databases;

數據表操作

註意:刪、改、查等操作是在使用use 表名之後進行的

格式:

create table 表名(

欄位名稱 數據類型 [列的完整性約束],

欄位名稱 數據類型 [列的完整性約束],

...

)engine=myisam charset=utf8;

示例:

create table test_table(
id int(11),
username varchar(255),
password char(32)
)engine=myisam charset=utf8;

格式:

drop table 表名;

drop table if exists 表名;

示例:

drop table test_table;
或
drop table if exists test_table;    #表名不存在時不會報錯

格式:

alter table 表名 rename 新的表名;

或者:

alter table 表名 rename to 新的表名;

示例:

alter table test_table rename my_table;
或者:
alter table my_table rename to test_table;

格式:

查看所有的表

show tables;

查看創建表的語句

show create table 表名;

示例:

show tables;
show create table test_table;

欄位操作

語法:

1、預設

alter table 表名 add 欄位名稱 數據類型 [列的完整性約束];

2、添加到第一個

alter table 表名 add 欄位名稱 數據類型 [列的完整性約束] first;

3、添加到某個欄位之後

alter table 表名 add 欄位名稱 數據類型 [列的完整性約束] after 已有欄位名稱;

示例:

alter table test_table add test_field int;          /* 預設添加到最後 */
alter table test_table add test_field int first;    -- 添加到第一個
alter table test_table add test_field decimal(11,2) after my_field; # 添加到my_field欄位之後,在需要存入金錢信息時,使用decimal,11和2分別表示小數點之前和之後的位數

語法:

alter table 表名 drop 欄位名稱;

示例:

alter table test_table drop test_field;

語法:

1、只修改位置

alter table 表名 modify 欄位名稱 數據類型 位置信息;

2、只修改欄位名稱

alter table 表名 change 原有欄位名稱 新的欄位名稱 數據類型;

3、修改位置和數據類型

alter table 表名 modify 原有欄位名稱 新的數據類型 位置信息;

4、修改欄位名稱和數據類型

alter table 表名 change 原有欄位名稱 新的欄位名稱 新的數據類型;

示例:

/* 只修改位置 */
alter table test_table modify test_field int first;     -- 移動到最前面
alter table test_table modify test_field int after my_field;    # 移動到某個欄位之後
/* 只修改欄位名稱 */
alter table test_table change test_field test2_field int(11);   # 這裡的數據類型與原有相同
/* 修改位置和數據類型 */
alter table test_table modify test_field char(255) first;
/* 修改欄位名稱和數據類型 */
alter table test_table change test_field test2_field char(20);  # 這裡的數據類型與原有不同

語法:

desc 表名;

查詢內容的解釋:

Field 欄位名稱

Type 數據類型

NULL 是否允許為空

Key 是否是主鍵、唯一鍵

Default 預設值

Extra 擴展內容

示例:

desc test_table;        #查詢test_table表的所有信息

列的完整性約束

Null

NULL 允許為空

NOT NULL 不允許為空

Key

PRIMARY KEY 主鍵

UNIQUE 唯一鍵

Default

預設值

Extra

UNSIGNED 設置當前欄位不允許出現負數

AUTO_INCREMENT 設置當前欄位自增

使用列的完整性約束建立數據表

create table test_table(
field1 int(11) unsigned not null auto_increment,
field2 varchar(255) not null unique,
field3 char(32) not null,
field4 tinyint(1) not null default 0,
field5 date not null,
field6 date null,
primary key(id)
)engine=myisam charset=utf8 collate utf8_general_ci;

數據操作

註意:刪除和修改時必須加where語句

語法:

1、插入一條

insert into 表名 (欄位1,欄位2,欄位3,...) values (值1,值2,值3,...);

2、一次插入多條

insert into 表名 (欄位1,欄位2,欄位3,...) values (值1,值2,值3,...),(值1,值2,值3,...),...;

3、省略欄位

insert into 表名 values (值1,值2,值3,...);

註意:值需要按順序全部填寫

示例:

/*
插入時的註意事項:
1 欄位和值一一對應
2 主鍵和唯一鍵不能重覆
3 自動遞增的欄位會找到當前最大值加1
4 不能為空的欄位必須插入內容(主鍵自增可以不寫)
*/
/* 插入一條 */
insert into test_table (field1,field2,field3) values (val1,val2,val3);
/* 一次插入多條 */
insert into test_table (field1,field2,field3) values (val11,val12,val13),(val21,val22,val23);

語法:

delete from 表名 where語句;

示例:

delete from test_table where id=1;

語法:

update 表名 set 欄位1=值1,欄位2=值2,... where語句;

示例:

update test_table set field1=val2,field2=val1 where id=1;

1、查詢所有欄位對應的內容

select * from 表名;

2、查詢指定欄位對應的內容

select 欄位1,欄位2,欄位3,... from 表名;

3、查詢時過濾掉重覆的內容

select distinct(欄位1) from 表名;

4、同時查詢多個表

select * from 表1,表2,...;

select 表1.欄位1,表1.欄位2,表2.欄位1,表2.欄位1,... from 表1,表2,...;

示例:

/* 查詢所有欄位 */
select * from test_table;
/* 查詢指定欄位 */
select field1,field2,field3 from test_table;
/* 過濾重覆內容 */
select distinct(field1) from test_table;
/* 查詢多個表 */
select * from table1,table2;
select table1.field11,table1.field12,table2.field21,table2.field22 from table1,table2;

where條件

(可以使用在 查詢、修改、刪除語句中)

示例:

# 查詢
select * from test_table where id=1;                # id=1
select * from test_table where id!=1;               # id!=1
select * from test_table where id>1;                # id>1
select * from test_table where id<10;               # id<10
select * from test_table where id>2 and id<5;       # 2<id<5
select * from test_table where id<2 or id>5;        # id<2 || id>5
select * from test_table where field1 is null;
select * from test_table where field1 is not null;
select * from test_table where id in(1,2,3);        # id=1、id=2、id=3
select * from test_table where id not in(1,2,3);    # id!=1...
select * from test_table where id between 3 and 25; # 3<id<25
####################################################################
# count() 數量
select count(id) from test_table where id>3;    # 查詢id>3的數據條數
# sum() 總和
select sum(age) from test_table where id>3;     # 查詢id>3的所有age的總和
# avg() 平均
select avg(age) from test_table where id>3;     # 查詢id>3的所有age的平均值
# max() 最大值
select max(age) from test_table where id>3;     # 查詢id>3的age的最大值
# min() 最小值
select min(age) from test_table where id>3;     # 查詢id>3的age的最小值
# concat() 合併多列
select concat(field1,':',field2) from test_table where id>3;
####################################################################

limit條件

(限制查詢的條數)

示例:

select * from test_table limit 3;   # 查詢3條,預設從第一條開始,等於limit 0,3
select * from test_table where id>3 limit 3;    # 配合where使用,放在where之後
select * from test_table limit 3,4; # 從3開始,查詢4條(第1條為0)

order by條件

(查詢的順序,asc升序、desc降序,不寫預設升序)

示例:

select * from test_table order by id;       # 預設升序
select * from test_table order by id desc;  # 降序
select * from test_table order by id asc;   # 升序
select * from test_table where id>20 order by id desc limit 3;  # 配合where和limit使用

group by條件

(分組查詢)

示例:

select field1,count(id) from test_table group by field1;    #結果如下
# 聚合
# +--------+-----------+
# | field1 | count(id) |
# +--------+-----------+
# | aa     |         3 |
# | bb     |         2 |
# +--------+-----------+
select field1,count(id) from test_table where id>3 group by field1 order by id desc;    # 和其它條件配合使用

like條件

(模糊查詢,要和where一起使用)

示例:

select * from test_table where field1 like '___';   # 查詢field1中有三位的數據,eg. aaa,bbb,哇哈哈...
select * from test_table where field1 like 'aa_';   # 查詢的結果eg. aa1,aaa,aab,aar...
select * from test_table where field1 like 'a%';    # 查詢以a開頭的數據,eg. a1,aae,adf,adgfs...
select * from test_table where field1 like '%a';    # 查詢以a結尾的數據,eg. asfa,fjksa,9uioa...
select * from test_table where field1 like '%a%';   # 查詢包含a的數據,eg. adf,kdjfakj...

各個條件的順序

where --> group by --> order by --> limit

多表聯合查詢

語法:

select * from 表1,表2 where 表1.欄位=表2.欄位

select 表1.欄位,表1.欄位2,表2.欄位,表2.欄位2... from 表1,表2 where 表1.欄位=表2.欄位

left join ... on

select * from 表1 left join 表2 on 表1.欄位=表2.欄位

right join ... on

select * from 表1 right join 表2 on 表1.欄位=表2.欄位

子查詢

語法:

select field from table where id in (select field2 from table where field3=val);

給欄位、表取別名

語法:

1、表

select name1.field1,name2.field2 from table1 as name1,table2 as name2 where name1.field3=name2.field4

2、欄位類似

可以省略as,使用空格代替

select name1.field1,name2.field2 from table1 as name1,table2 as name2 where name1.field3=name2.field4;

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

-Advertisement-
Play Games
更多相關文章
  • windows本地安全策略實驗-遠程桌面連接鎖定賬戶 實驗環境: 服務端:Win7-1:10.10.10.136,開啟遠程桌面服務 客戶端:win7-2:10.10.10.153 確保客戶端和服務端能夠互通 實驗步驟: 一、本地策略,用戶許可權分配設置 1.首先在服務端創建一個普通用戶 2.打開本地安 ...
  • 用戶登錄信息查看命令 1、who命令 同一個賬號通過不同終端登錄也屬於不同的登錄信息,這裡不同的終端包含虛擬終端和模擬終端,因為一個用戶通過一個終端登錄屬於一個session 基本介紹 列印當前系統上所有登錄的會話 2、whoami命令 基本介紹 列印有效的用戶id信息或者顯示當前登錄的有效用戶 3 ...
  • 系統管理類命令 1、reboot、halt、poweroff命令 基本介紹 reboot命令、halt命令、poweroff命令:都表示重啟或者關閉系統 基本語法 reboot/halt/poweroff [options...] 都有類似常用選項 -f:不調用shutdown命令關機,不建議這麼使 ...
  • 對於使用 timestamp 的場景,MySQL 在訪問 timestamp 欄位時會做時區轉換,當 time_zone 設置為 system 時,MySQL 訪問每一行的 timestamp 欄位時,都會通過 libc 的時區函數,獲取 Linux 設置的時區,在這個函數中會持有mutex,當大量 ...
  • 一、卸載安裝(來自百度經驗) 完全卸載: 1. 停止相關服務 2. 運行Universal Installer,卸載產品 3. 清理註冊表 4. 重啟電腦,刪除目錄(Oracle文件夾和app文件夾) 安裝: 1. 運行setup.exe 2.取消勾選“接收安全更新”選項 3. 選擇創建和配置資料庫 ...
  • 在MySQL 5.6版本中引入參數explicit_defaults_for_timestamp設置,該參數會影響Timestamp的預設屬性。 同時在MySQL 5.6版本中中,去除一張表只能有一個TIMESTAMP列的限制,允許單表中使用多個時間戳列。 在MySQL 5.6中,當參數explic ...
  • Timestarmp列可以設置兩個屬性:1、DEFAULT CURRENT_TIMESTAMP 表示插入記錄行時,如果未對該列指定值,則使用當前時間來為該欄位賦值2、ON UPDATE CURRENT_TIMESTAMP 表示在更新記錄時,如果為更新該事件戳列,使用當前時間來更新該欄位 如果在定義時 ...
  • 下麵以存儲過程查詢所有為例,非存儲過程(或不是查詢所有將*替換為你想要查詢的列即可)更為簡單, 語法:select * from 表名 where 列名 like'%條件%' 拼接後的set @變數名 = 'select * from 表名 where ' + @條件 + ' like ' + '' ...
一周排行
    -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# ...