mariadb數據庫增刪改查

1.常用數據類型

1)整數:int, bit

2)小數:decimal   ??? #decimal(5,2)表示共有五位數,保留兩位小數

3)字符串:varchar, char  ??????????????????????

4)日期時間:date, time, datetime

5)枚舉類型(enum)

?

2.約束

1)主鍵primary key:物理上存儲的順序

2)非空not null:此字段不能為空

3)唯一unique:此字段不允許重復

4)默認default:當不填寫此值時會使用默認值,如果填寫則已填寫為準

5)外鍵foreign key:對關系字段進行約束,當為關系字段填寫值時,會到關聯的表中查詢此值是否存在,如果存在則填寫成功,如果不存在則填寫失敗并拋出異常

數值類型
類型字節大小有符號范圍無符號范圍
TINYINT1-127~1270~255
SMALLINT2-32768~327670~65535
MEDIUMINT3-8388608~83886070~16777215
INT/INTEGER4-2147483648~21474836470~4294967265
BIGINT8-9223372036854775808~-92233720368547758070~18446744073709551615
字符串
類型字節大小示例
CHAR0-255char(3)不管輸入幾個字節都會占3個字節
VARCHAR0-255varchar(3)輸入比三小的字節會占用實際字節大小
TEXT0-65535大文本
日期時間類型
DATE4‘2020-01-01’
TIME3’12:05:34’
DATETIME8‘2020-01-01? 12:05:34’
YEAR1‘2019’
TIMESTAMP4‘1970-01-01? 00:00:01’UTC~‘2038-01-01? 00:00:01’UTC

3.sql語句alter

顯示當前時間

select now();

?

創建classes表(id, name)

create table zzzz(

??? id int primary key not null auto_increment,

??? name varchar(20),

??? age int

);

?

查看表結構

desc zzzz

?

1)創建students表(id, name, age, high, gender, cls_id)

create table students (

??? id int unsigned not null auto_increment primary key,

??? name varchar(20),

??? age tinyint unsigned default 0,

??? high decimal(5,2),

??? gender enum('男', '女', '中性', '保密') default '保密',

??? cls_id int unsigned

);

?

創建classes表(id, name)

create table classes(

??? id int unsigned not null auto_increment primary key,

??? name varchar(20)

);

?

2)修改表屬性

修改表-添加字段

alter table 表名 add 列名 類型;

alter table students add birthday datetime;

?

修改表-修改字段:不重命名版

?alter table 表名 modify 列名 類型及約束;

alter table students modify birthday date;

?

修改表-修改字段:重命名版

alter table 表名 change 原名 新名 類型及約束;

alter table students change birthday birth date;

?

3)修改表-刪除字段

alter table 表名 drop 列名;

alter table students drop birthday;

?

4) 刪除表

drop table 表名;

drop table students;

?

4.sql語句增加insert

1)全列插入

insert into 表名 values(..)??? 主鍵字段 可以用0 null default 來站位

向students表里插入 一個學生信息

insert into students values (0,'小明',19,188.999,'男', 1);

?????????????

2)部分插入

??????? insert into students(id, name, age) values (0,'綠帽子',19);

??????? 部分插入(多條記錄)

??????? insert into students(id, name, age) values (0,'綠帽子',19),(0,'小跳蚤',21);

???????

5.sql語句修改update

update 表名 set 列1=值1, 列2=值2... where 條件;

update students set age=100 where id=1;

update students set age=100,cls_id=77 where id=1;

?

6.sql語句刪除delete與truncate

1)物理刪除

delete from 表名 where 條件

delete from students where cls_id=88;???

?

2)邏輯刪除

用一條字段來表示 這條信息是否已經不能在使用了

給students表添加一個is_delete字段 bit 類型

alter table students add is_delete bit default 0;

update students set is_delete=1 where id=6;

?

3)truncate

清空表,連同id字段自增重置為1,重新插入的數據id默認會從1開始,用truncate刪除的數據無法恢復

truncate students

?

7.sql語句查看select

查詢基本使用(條件,排序,聚合函數,分組,分頁)

1)查詢所有列

select * from 表名

select * from students;

?

一定條件查詢(where)

select * from where id=5;

???

查詢制定列

select id,name from students;

???

使用as給字段起別名

select id,name as '姓名', age, high, gender from students;

???

通過表名字段查詢

select students.name from students;

???

給表起別名查詢

select s.id,s.name,s.age from students as s;

???

消除重復行

distinct

select distinct age from students;

?

2)條件查詢

查詢年紀大于18歲的信息

select * from students where age > 18;

???????

18歲到28歲之間(and)

select * from students where age >= 18 and age =< 28;??? 允許使用&&

select * from students where age between 18 and 28

?

在18歲以上或者身高180以上的人(or)

select * from students where age > 18 or high > 180;??? 允許使用||

?

3)模糊查詢like

% 替代1個或者多個甚至是沒有

查詢姓名中有‘小’的所有名字

select * from students where name like '%小%';

?

查詢兩個字人的名字

select * from students where name like '__';??? 兩個下劃線

?

查詢至少有2個字的名字

select * from students where name like '%__%';

?

4)范圍查詢

in (1,3,8)表示在一個非連續的范圍內

查詢年紀為18和34的人

select * from students where age in (18, 34);

?

查詢 年齡在17歲到34歲之間的信息

select * from students where age between 17 and 34;

?

查詢 年紀不在18到34歲的信息

select * from students where age not between 17 and 34;

?

5)空判斷

判斷is null

查詢身高為空的人的信息

select * from students where high is null;

?

6)排序order by

asc從小到大排列,即升序

desc從大到小排序,即降序

order by支持多字段

?

查詢年紀在18到34歲之間的男性,按照年紀從小到大

select * from students where gender=1 and age between 18 and 34 order by age;

?

查詢年紀在18到34歲之間的女性,身高從高到矮

select * from students where gender=2 and age between 18 and 34 order by high desc;

?

查詢年紀在18到34歲的男性,身高從高到矮排序,如果身高相同的情況下按照年紀從小到大排序,如果年齡也相等那么按照id從小到大排序;

select * from students where age between 18 and 34 and gender=1 order by high desc,age,id;

???????

8.聚合函數

1)總數count

查詢男性有多少人

select count(*) from students where gender=1;

?

2)最大值max

查詢最大的年紀

select max(age) from students;

?

查詢女性的最高身高

select max(high) from students where gender=2;

?

3)最小值 min

select min(high) from students;

?

4)求和sum

計算所有人的年齡總和

select sum(age) from students;

?

5)平均值avg

計算平均年紀 sum(age)/count(*)

select sum(age)/count(*) from students;

select avg(age),2 from students;

?

保留2位小數

select round(avg(age),2) from students;

???

6)分組group by

按照性別分組,查詢所有的性別

select gender from students group by gender;

?

計算每組性別的人數

select gender, count(*) from students group by gender;

?

查詢男性組中的姓名 group_concat

select gender,group_concat(name) from students where gender=1 group by gender;??? group_concat()按照拼接后的字符串顯示

?

7)having

查詢每個性別平均年紀超過30歲的性別,以及姓名 having avg(age) > 30

select gender, group_concat(name) from students group by gender having avg(age) > 30;

?

查詢每種性別中的人數多于4個的組的信息

select gender,group_concat(name) from students group by gender having count(*)>4;

?

8)分頁limit

顯示5頁

select * from students limit 5;

?

分頁顯示,每頁顯示2條數據

select * from students limit 0, 2;

?

按照身高從高到矮排序,查找出所有女性,并且分頁顯示,每頁顯示2條數據

select * from students where gender=2 order by high desc limit 0,2;

?

轉載于:https://www.cnblogs.com/Agnostida-Trilobita/p/11134115.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/448506.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/448506.shtml
英文地址,請注明出處:http://en.pswp.cn/news/448506.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

為什么你工作努力卻沒有起色?

成為職場達人&#xff0c;未必要經常挑燈夜戰。相反&#xff0c;注意到下面幾條&#xff0c;會讓你少走彎路。 1&#xff09;成長的機會永遠比眼前的待遇重要——做重要的事比多拿錢重要。 我知道在水木bbs上的worklife版本&#xff0c;每天都在上演的就是比較自己的第一個o…

《 Spring 實戰 》(第4版) 讀書筆記 (未完結,更新中...)

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 Pxx 表示在書的第 xx 頁。 Spring 框架的核心是 Spring 容器。 1. (P7.) 構造器注入是依賴注入的方式之一。 緊耦合&#xff1a;在 …

數據結構排序法之希爾排序法(Shell Sort)

希爾排序&#xff0c;也叫遞減增量排序&#xff0c;是插入排序的一種更高效的改進版本。希爾排序是不穩定的排序算法。 希爾排序是基于插入排序的以下兩點性質而提出改進方法的&#xff1a; 1、插入排序在對幾乎已經排好序的數據操作時&#xff0c;效率高&#xff0c;即可以達…

Windows To Ghost系統封裝之必備軟件集 - 好壓

好壓壓縮軟件&#xff08;HaoZip&#xff09;是強大的壓縮文件管理器&#xff0c;是完全免費的新一代壓縮軟件&#xff0c;相比其它壓縮軟件系統資源占用更少&#xff0c;有更好的兼容性&#xff0c;壓縮率比較高。 它提供了對ZIP、7Z和TAR文件的完整支持&#xff0c;能解壓RAR…

js 彈窗并定時關閉

1. $(input).click(function() {prompt(點擊成功, 2000) })function prompt(newName, time, fn) {var $div $(<div></div>);$div.css({position: fixed,top: 0,left: 0,width: 100%,height: 100%,z-index: 200,background-color: rgba(0,0,0,0.4),// background-c…

數據結構排序法之插入法

插入排序是一種簡單直觀的排序算法。它的工作原理非常類似于我們抓撲克牌。 對于未排序數據(右手抓到的牌)&#xff0c;在已排序序列(左手已經排好序的手牌)中從后向前掃描&#xff0c;找到相應位置并插入。 插入排序在實現上&#xff0c;通常采用in-place排序&#xff08;即…

XSLT學習筆記

1. 樣式聲明&#xff1a;<xsl:stylesheet>或<xsl:transform> 2. XSLT常用元素&#xff1a; 2.1 <xsl:template>&#xff1a;創建模板 Match屬性的作用是使模板和XML元素相關聯 e.g.:<xsl:template match"\">......</xsl:template&g…

職場:人生從沒有最佳時機!一個離職客服人員的領悟

每個人都有感到失落迷惘的時候。 人生用專制又霸道的方式運行著&#xff0c;每當我們心想一切塵埃落定、生活穩固的時候&#xff0c;生活總愛給我們驚喜&#xff0c;粉碎我們短暫的安逸&#xff0c;讓我們不得不重新思考。 「我走對路了嗎?」 「我能夠賺更多錢、爬到更高的地位…

VS Code 的常用快捷鍵

VS Code 的常用快捷鍵和插件 一、vs code 的常用快捷鍵 前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1、注釋&#xff1a; a) 單行注釋&#xff1a;[ctrlk,ctrlc] 或 ctrl/ b) 取消…

vue-axios interceptors

import axios from axios import cookie from js-cookie const options {baseURL: window.location.protocol process.env.BASE_API,headers: {},timeout: 20000 } const fetch axios.create(options)// request攔截器 fetch.interceptors.request.use(config > {if (coo…

數據結構排序法之雞尾酒排序法he快速排序法

雞尾酒排序&#xff0c;也叫定向冒泡排序&#xff0c;是冒泡排序的一種改進。此算法與冒泡排序的不同處在于從低到高然后從高到低&#xff0c;而冒泡排序則僅從低到高去比較序列里的每個元素。他可以得到比冒泡排序稍微好一點的效能。 // 兩兩互換 void swap (int* a, int i, …

VSCode 多開、環境對比

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 多開&#xff1a; 第一種&#xff1a;win10的開始菜單&#xff0c;在vscode圖標右鍵選擇“新開窗口”&#xff0c;這樣就多了一個vscode…

前言_工作兩年自我感觸

17年大學畢業&#xff0c;到今天整整工作兩年&#xff0c;從前端到數據分析&#xff0c;從上家公司&#xff08;簡稱A&#xff09;到現公司&#xff0c;想趁著今天是參加工作兩年的紀念日&#xff0c;回憶過往&#xff0c;結合現狀有感而發。 剛畢業的時候&#xff0c;啥都學&a…

數據結構排序法之堆排序he歸并排序

堆排序&#xff08;Heapsort&#xff09;是指利用堆這種數據結構所設計的一種排序算法。堆是一個近似完全二叉樹的結構&#xff0c;并同時滿足堆性質&#xff1a;即子結點的鍵值或索引總是小于&#xff08;或者大于&#xff09;它的父節點。 堆排序的時間&#xff0c;主要由建…

超詳細設置 Idea 類注釋模板和方法注釋模板

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 網上找了一下&#xff0c;沒有很詳細且正確介紹Idea配置注釋模板的&#xff0c;于是結合多篇文章自己琢磨整理出如下。 設置類注釋模板…

手動創建兩個文本文件text1.txt和text2.txt,按要求創建text3.txt

實現在text1.txt和text2.txt文件中除去首行和末尾對應的數據&#xff0c;要求三個文本內容如下&#xff1a; text1 text2 text3begin begin begin10 11 12 15 16 17 …

感情

團結 共患難的感情轉載于:https://www.cnblogs.com/yyjh/p/11139749.html

誰搶走了中國男人的老婆?

“老夫少妻”、“包二奶”、“洋媳婦”、“單身貴族”、“丁克家庭”都是當今最時髦的詞匯。這看似“你情我愿”的現象背后竟隱藏著巨大隱患! 目前中國男女比例是119&#xff1a;100&#xff0c;某些地區已達130&#xff1a;100;中國將有5百萬以上光棍&#xff0c;這對中國社會…

latex 幻燈片演示模板

http://zzg34b.w3.c361.com/templet/slide.htm轉載于:https://www.cnblogs.com/binterminator/articles/1621647.html

Linux 文件系統編程之系統調用和標準I/O庫

系統調用 訪問設備驅動程序的底層函數主要有&#xff1a; open:打開文件或者設備。 read:從打開的文件或者設備里面讀取數據。 write:向文件或者設備寫數據。 close:關閉文件或者設備。 open系統調用&#xff1a; #include <fcntl.h> #include <sys/types.h> #in…