1. 程式人生 > 其它 >表和欄位管理操作

表和欄位管理操作

目錄

1.增

結構
create table 表名(欄位名 型別);

建立表s1,id欄位為int型別,設定為自增主鍵
create table s1(
id int AUTO_INCREMENT PRIMARY KEY,
name char(20),
age int
);

插入一條資料
insert into s1(name,age) values('xx',11),('dd',35);

將s1複製為s2表,from後面可以接查詢語句去查詢。
create table s2 select * from s1;

為s2表新增一個age欄位,放在name欄位後面
alter table s2 add age int(4) after name;

複製s1的表結構成s2,不包含資料。
create table IF NOT EXISTS s2 (LIKE s1);

2.刪

刪除表
drop table 表名;

刪除表所有記錄
DELETE FROM 表名;

刪除某個表id為3百萬的記錄,只要匹配就刪除。
delete from s1 where id=3000000;

清空表資料-1
truncate table table_name;

清空表資料-2
delete * from table_name;
注 : truncate操作中的table可以省略,delete操作中的*可以省略

truncate、delete 清空表資料的區別 :

  1. truncate 是整體刪除 (速度較快),delete是逐條刪除 (速度較慢)
  2. truncate 不寫伺服器 log,delete 寫伺服器 log,也就是 truncate 效率比 delete高的原因
  3. truncate 不啟用trigger (觸發器),但是會重置Identity (標識列、自增欄位),相當於自增列會被置為初始值,又重新從1開始記錄,而不是接著原來的 ID數。而 delete 刪除以後,identity 依舊是接著被刪除的最近的那一條記錄ID加1後進行記錄。如果只需刪除表中的部分記錄,只能使用 DELETE語句配合 where條件
  4. truncate操作中的table可以省略,delete操作中的*可以省略

3.改

將表s1的名字改為s2
alter table s1 rename to s2;

將s1表所有id設定為10
update s1 set id=10;

將名字為張三的行,id號設定為20
update s1 set id=20 where name='zhangsan';

更改預設儲存引擎
alter table 表名 ENGINE=InnoDB;

4.查

語法,格式固定
select xx from xx where xx;

以列表形式顯示
select xx from xx where xx\G;

將name顯示為姓名,age顯示為年齡
select name AS '姓名',age AS '年齡' from xx where xx;

可以多條件查詢
select * from test where id>2 and id<5;

模糊匹配
select * from s3 where name LIKE 'A%' or email like '3%' ;

正則匹配,以A開頭或者A結尾的
select * from s3 where name regexp '^A|A$';

將age的平均數作為條件,和age比對,找出大於平均數的
select name,age from s1 where age > (select avg(age) from a1);

將所有id號求和
select sum(id) from s1;

查看錶記錄數量
SELECT count(*) FROM 表名;

統計一個欄位不重複的值的個數
select count(distinct user) from mysql.user;

將s1,s2表的xx欄位合併一起顯示,但會重啟,去重用union
select xx from s1 union all select xx from s2;

排列顯示
按id的排列順序顯示查詢到的東西
select * from s2 order by id asc;

倒敘排列
select * from s2 order by id desc;

行數顯示
檢視前2行資料
select * from s2 limit 2;

檢視第一行的後2行資料
select * from s2 limit 1,2;

聯合表查詢
會將2個表拼一起
select * from s1,s2;

where將2部分相同的地方找出來,保證不重複。再將a1全部輸出,a2只輸出成績部分。
select a1.*,a2.grade from a1,a2 where a1.name=a2.name;

查詢表分割槽
show create table tlog_rsp\G;

二.欄位

1.增

新增欄位
alter table 表名 add 欄位 型別(寬度) 約束條件;

新增固定為值,最後新增first,將會放到第一個欄位
`alter table 表名 add 欄位 型別(寬度) 約束條件 after 上一個欄位名;

2.刪

刪除欄位
alter table 表名 drop 欄位名;

刪除表分割槽
alter table tlog_rsp drop PARTITION TLOG_RSP_20190901;

3.改

將s2表的age欄位修改為char型別,最大字元數為4。
alter table s2 modify age char(4);

將欄位放到最前面
alter table 表名 modify 欄位 int(3) first;

修改欄位名,若型別修改則修改,原先則預設
alter table 表名 change 原名 新名 型別(寬度);

修改欄位型別
alter table 表名 modify 欄位 char(20);

三.特殊查詢

檢視現在時間,now是函式
select now()

檢視警告部分
show warnings;

輸出a1表查詢過程中的操作資訊,
explain select * from a1;

*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: a1
         type: ALL
possible_keys: NULL #沒有用索引
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 6 #總共查詢6行
        Extra:

顯示了有哪些執行緒在執行
show full processlist;

檢視mysql有哪些使用者
select user,host from mysql.user;

檢視當前庫有哪些表
show tables;

查詢表建立時的語句
show create table 表名;

表格形式查看錶結構
describe host;

檢視s1表的結構,顯示name欄位
SHOW COLUMNS FROM s21 LIKE '%name';

檢視當前支援儲存引擎
show engines;

查看錶資訊,ENGINE表示當前表儲存引擎,CHARSET預設語言
show create table 表名;

檢視當前進入資料庫的使用者
select user();

檢視當前時間區域,預設system東八區
show variables like 'time_zone';

本文版權歸作者所有,歡迎轉載,請務必新增原文連結。