MySQL資料的CRUD(增刪改查)

2021-10-09 23:22:26 字數 3761 閱讀 9696

增加(insert):

insert into t_name[(欄位1 …)] values(v1 …);

如:insert into t_hero(id, username, gender, age, tel) values(null, 「張三」, 「男」, 16, default);

insert into t_hero values(null, 「張三」, 16, 「男」, default);

insert into t_hero(username, gender, age) values(「李四」, 「思思」, 40);

更新(update):

將表中原有資料修改為我們需要的資料

update t_name set 欄位1 = 新值, 欄位2 = 新值 … where 條件

update t_hero set age = 18, gender = "女" where id = 3;
刪除(delete):

delete from tablename where 條件

delete from t_hero where id =1;

truncate 語句(慎用)

該語句也是用來刪除表,是無法恢復的

select

select * from 表名;

select id, username, age, gender, tel from t_hero;

select username from t_hero;

表的修改:

表結構的修改:alter table

1、增加新的字段

alter table 表名 add 列名 列型別 約束條件;

alter table t_hero add address varchar(255);

2、修改欄位的型別

alter table 表名 modify 列名 列型別;

alter table t_hero modify id bigint;

3、 修改欄位名稱

alter table 表名 change 舊列名 新列名 列型別;

alter table t_hero change id hero_id int;

4、修改表名稱

alter table 表名 rename 新錶名;

alter table t_hero rename hero;

第二種寫法

rename table hero to t_hero;

查詢:查詢語句的使用,返回結果本身就是一張虛擬表

select * from t_hero;

查詢指定的列【字段】,是指定你要查詢的欄位名稱

select hero_id, username, age, gender

from t_hero;

count這個聚合函式完成表中數量的統計

select count(*) from t_hero;

1、在查詢時,如果需要修改結果的列名稱,可以使用別名(alias)完成修改

select count(*) as "總人數" from t_hero;

//需要多個字段別名, as alias,在sql中,as關鍵字是可以省略的!!

select hero_id as id, username uname, age, gender 性別 from t_hero;

2、 查詢id為3的使用者

select * from t_hero where hero_id = 3;

3、查詢成年人

select username from t_hero where age >= 16;

4、查詢性別不為男的

select * from t_hero where gender = "女";

5、不等於

select * from t_hero where gender != '男';

select * from t_hero where gender <> '男';

6、查詢位址為空的,或者位址不能為空

// 注意:查詢空和非空,不能使用 = 或者 !=去查詢,需要使用is或者 not is

select * from t_hero where gender is null;

select * from t_hero where gender is not null;

多個條件同時存在時需要使用邏輯運算子 and or

7、查詢年齡大於100歲的男生

select * from t_hero where age >= 100 and gender = "男";

8、查詢年齡大於100 或者 id 大於2的

select *

from t_hero

where hero_id > 2 or age >= 100;

為每個人年齡增加10歲

update t_hero set age = age + 10;

9、算術運算子的一些細節問題

select 80 + 90;

10、只要其中乙個為數值,則試圖將字元型轉換成數值

select '80' + 100;

11、轉換不成功,則字元型數值為0

select 'liujianhong' + 20;

12、null加任何值都為null

select null + 30;

13、<=> 這個符合的使用

(1)、可以當成普通的等號使用

select * from t_hero where hero_id = 1;

select * from t_hero where hero_id <=> 1;

(2)、判斷null,is is not,而<=>也可以判斷等於空

select * from t_hero where gender is null;

select * from t_hero where gender <=> null;

統計當前表總共有多少種性別

//可以使用distinct關鍵字進行去重

select distinct gender from t_hero;

// 查詢年齡大於100歲的男生

select * from t_hero where age >= 50 and age < 500;

between ... and 語句用來確定區間範圍

select * from t_hero where age between 50 and 500;

/** 查詢id為1 2 3 4 6 8 10 12 **/

select * from t_hero where hero_id in(1,2,3,4, 6, 8, 10,12);

-- 模糊查詢,一般用來匹配一些文字內容

// 在模糊查詢中,使用like進行模糊查詢,不要使用 =

// % 表示任意匹配的意思 _ 表示乙個位

// 以張字開頭的

select * from t_hero where username like '張%';

// 包含張字的

select * from t_hero where username like '%張%';

// 以張字結尾

select * from t_hero where username like '%張';

// 第二個字是李的使用者,

select * from t_hero where username like '_李%';

MySql的CRUD(增 刪 改 查)操作

mysql的crud 增 刪 改 查 操作和sql server的差不多。需要的注意的是delete的語法 delete tbcategory where categoryname qq 錯誤的寫法,少了乙個from delete from tbcategory where categoryname...

mongoodb(增刪改查)crud

一.查詢 1.查詢使用者集合中所有文件 陣列文件 user.find then result console.log result 2.通過 id欄位查詢文件 user.find then result console.log result 3.findone方法返回一條文件,預設返回集合第一條文件...

mybatis實現(增刪改查)CRUD

以下是一套完整的mybatis的增刪改查,親測可用 1.mybatis工具類 public class mybatisutil catch exception e public static sqlsession getsqlsession public inte ce xml version 1....