Mysql 基礎語法總結

2021-08-15 20:43:49 字數 3379 閱讀 1575

建立表:

create

table

ifnot

exists

`tablename`

(`id`

intauto_increment

,-- 自增

`title`

varchar

(100),

primary

key(

`id`

)--主鍵

);

刪除表:

不需要表:drop table `tablename`;

刪除表中全部資料:truncate table `tablename`;

刪除表中某條資料:delete from `tablename` where 1=1;

插入資料:

insert into `tablename`(`id`) values(1),(2);

查詢語句:

select * from `tablename` where 1=1;更新語句:

update `tablename` set `title` = 'title' where `id` = 1;刪除語句:

delete from tablename where id = 2;like: 用於where子語句,

%xx,以xx結尾;

xx%,以xx開頭

%xx%,包含xx

select * from table where name like %a%;sql union 預設不重複,允許重複使用 union all

select name from table1 union select name from table2排序 order by

asc 公升序, 預設項

desc 倒序,降序

select name from table1 where id = 1 order by id descgroup by

select `name` ,count(1) from employee_tbl group by `name`;with rollup 跟列排序有關,(a,b,c)產生(a,b,c)、(a,b)、(a)三種層次聚合

select coalesce( `name`, '總數') as singin_name, sum(singin) as singin_count from table1 group by `name` with rollup;連線

inner join 可省略為join 交集

left join 讀取左表所有,右側沒有則顯示null

right join 讀取右表所有,左側沒有則顯示null

select * from table1 a join table2b on a.id = b.id

null判斷必須使用is null 或者is not null或 <=> 兩者都為null返回true

還可以利用isnull(exp)

select * from table1 where not isnull(name)

等同於select * from table1 where name is not null正規表示式:跟mysql無關,多用於輸入驗證,或爬蟲匹配

^a 匹配以a開頭的字串

b$ 匹配以b結尾的字串

. 匹配出\r以外的任意字元

[abcd] 匹配abcd中的任意字元

[^abcd] 匹配除此之外的任意字元

a|b|c 匹配aor b or c

* 匹配0次或以上 等價

+ 匹配至少一次 等價

匹配n次,n >=0

匹配至少n次,最多m次

事務:

begin 開始乙個事務

rollback 事務回滾

commit 事務確認

預設自動提交,設定set autocommit=0禁止自動提交

alter

刪除字段alter table table1 drop id;新增字段alter table table1 add id int;修改字段

alter table table1 modify id char(10);alter table table1 change id id char(10);alter table table1 change id ids int;

修改字段預設值

alter table table1 modify name varchar(100) not null default 'a';alter table table1 alter name drop default;

修改表名:alter table table1 rename to table2;

索引 建立索引create index indexname on table1(name(10));新增索引alter table table1 add index indexname(name);刪除索引drop index indexname on table1;建立唯一索引create unique index indexname on table1(name(10));

臨時表 只存在於當前會話中create temporary table tabletemp;複製表

只複製表結構create table newtable like oldtable;複製表結構及資料create table newtable select * from oldtable;

批量更新uuid

update mt_note set uuid =uuid();

如果想去掉-需要先設定為36位,再批量更新替換掉-;直接使用replace,會使所有uuid一樣。

update mt_note set uuid =uuid();

update mt_note set uuid =

replace

(uuid,

'-','')

;

MySQL基礎語法總結

一.資料庫和表 建立資料庫 create database databasename 刪除資料庫 drop database databasename 顯示資料庫 show databases 資料庫切換 use databasename 建立表 create table 刪除表 drop tabl...

Mysql 基礎語法總結

1.使用資料庫 1.刪除資料庫 drop database 1.建立表 新建乙個學生表。create table student sid int primary keyauto increment sname varchar 255 age int birthday datetime 2.crud ...

mysql基礎語法演示 mysql基礎語法

1 ddl 增刪改查 1 select 獲取資料 select from 表名 where 條件 2 update 更新資料 update 表名 set 欄位名 值,欄位名 值 where 條件 3 delete 刪除資料 delete from 表名 where 條件 4 insert into ...