MySQL---數據表基本操作

来源:https://www.cnblogs.com/horror/archive/2018/08/20/9506579.html
-Advertisement-
Play Games

一、表操作 1、創建表 1 是否可空,null表示空,非字元串 2 not null - 不可空 3 null - 可空 1 預設值,創建列時可以指定預設值,當插入數據時如果未主動設置,則自動添加預設值 2 create table tb1( 3 nid int not null defalut 2 ...


一、表操作

1、創建表

1 create table 表名(
2     列名  類型  是否可以為空,
3     列名  類型  是否可以為空
4 )ENGINE=InnoDB DEFAULT CHARSET=utf8
1         是否可空,null表示空,非字元串
2             not null    - 不可空
3             null        - 可空
是否可以為空
1         預設值,創建列時可以指定預設值,當插入數據時如果未主動設置,則自動添加預設值
2             create table tb1(
3                 nid int not null defalut 2,
4                 num int not null
5             )
預設值
 1 自增,如果為某列設置自增列,插入數據時無需設置此列,預設將自增(表中只能有一個自增列)
 2             create table tb1(
 3                 nid int not null auto_increment primary key,
 4                 num int null
 5             )
 6  7             create table tb1(
 8                 nid int not null auto_increment,
 9                 num int null,
10                 index(nid)
11             )
12             註意:1、對於自增列,必須是索引(含主鍵)。
13                  2、對於自增可以設置步長和起始值
14                      show session variables like 'auto_inc%';
15                      set session auto_increment_increment=2;
16                      set session auto_increment_offset=10;
17 
18                      show global  variables like 'auto_inc%';
19                      set global auto_increment_increment=2;
20                      set global auto_increment_offset=10;
自增
主鍵,一種特殊的唯一索引,不允許有空值,如果主鍵使用單個列,則它的值必須唯一,如果是多列,則其組合必須唯一。
            create table tb1(
                nid int not null auto_increment primary key,
                num int null
            )
            或
            create table tb1(
                nid int not null,
                num int not null,
                primary key(nid,num)
            )
主鍵
 1 外鍵,一個特殊的索引,只能是指定內容
 2             creat table color(
 3                 nid int not null primary key,
 4                 name char(16) not null
 5             )
 6 
 7             create table fruit(
 8                 nid int not null primary key,
 9                 smt char(32) null ,
10                 color_id int not null,
11                 constraint fk_cc foreign key (color_id) references color(nid)
12             )
外鍵

2、刪除表

1 drop table 表名

3、清空表

1 delete from 表名
2 truncate table 表名

4、修改表

 1 添加列:alter table 表名 add 列名 類型
 2 刪除列:alter table 表名 drop column 列名
 3 修改列:
 4         alter table 表名 modify column 列名 類型;  -- 類型
 5         alter table 表名 change 原列名 新列名 類型; -- 列名,類型
 6   
 7 添加主鍵:
 8         alter table 表名 add primary key(列名);
 9 刪除主鍵:
10         alter table 表名 drop primary key;
11         alter table 表名  modify  列名 int, drop primary key;
12   
13 添加外鍵:alter table 從表 add constraint 外鍵名稱(形如:FK_從表_主表) foreign key 從表(外鍵欄位) references 主表(主鍵欄位);
14 刪除外鍵:alter table 表名 drop foreign key 外鍵名稱
15   
16 修改預設值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
17 刪除預設值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;

5、基本數據類型

MySQL的數據類型大致分為:數值、時間和字元

  1 bit[(M)]
  2             二進位位(101001),m表示二進位位的長度(1-64),預設m=1
  3 
  4         tinyint[(m)] [unsigned] [zerofill]
  5 
  6             小整數,數據類型用於保存一些範圍的整數數值範圍:
  7             有符號:
  8                 -128 ~ 127.
  9             無符號:
 10 ~ 255
 11 
 12             特別的: MySQL中無布爾值,使用tinyint(1)構造。
 13 
 14         int[(m)][unsigned][zerofill]
 15 
 16             整數,數據類型用於保存一些範圍的整數數值範圍:
 17                 有符號:
 18                     -2147483648 ~ 2147483647
 19                 無符號:
 20 ~ 4294967295
 21 
 22             特別的:整數類型中的m僅用於顯示,對存儲範圍無限制。例如: int(5),當插入數據2時,select 時數據顯示為: 00002
 23 
 24         bigint[(m)][unsigned][zerofill]
 25             大整數,數據類型用於保存一些範圍的整數數值範圍:
 26                 有符號:
 27                     -9223372036854775808 ~ 9223372036854775807
 28                 無符號:
 29  ~  18446744073709551615
 30 
 31         decimal[(m[,d])] [unsigned] [zerofill]
 32             準確的小數值,m是數字總個數(負號不算),d是小數點後個數。 m最大值為65,d最大值為30。
 33 
 34             特別的:對於精確數值計算時需要用此類型
 35                    decaimal能夠存儲精確值的原因在於其內部按照字元串存儲。
 36 
 37         FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]
 38             單精度浮點數(非準確小數值),m是數字總個數,d是小數點後個數。
 39                 無符號:
 40                     -3.402823466E+38 to -1.175494351E-38,
 41                     1.175494351E-38 to 3.402823466E+38
 42                 有符號:
 43                     1.175494351E-38 to 3.402823466E+38
 44 
 45             **** 數值越大,越不准確 ****
 46 
 47         DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]
 48             雙精度浮點數(非準確小數值),m是數字總個數,d是小數點後個數。
 49 
 50                 無符號:
 51                     -1.7976931348623157E+308 to -2.2250738585072014E-308
 52                     2.2250738585072014E-308 to 1.7976931348623157E+308
 53                 有符號:
 54                     2.2250738585072014E-308 to 1.7976931348623157E+308
 55             **** 數值越大,越不准確 ****
 56 
 57 
 58         char (m)
 59             char數據類型用於表示固定長度的字元串,可以包含最多達255個字元。其中m代表字元串的長度。
 60             PS: 即使數據小於m長度,也會占用m長度
 61         varchar(m)
 62             varchars數據類型用於變長的字元串,可以包含最多達255個字元。其中m代表該數據類型所允許保存的字元串的最大長度,只要長度小於該最大值的字元串都可以被保存在該數據類型中。
 63 
 64             註:雖然varchar使用起來較為靈活,但是從整個系統的性能角度來說,char數據類型的處理速度更快,有時甚至可以超出varchar處理速度的50%。因此,用戶在設計資料庫時應當綜合考慮各方面的因素,以求達到最佳的平衡
 65 
 66         text
 67             text數據類型用於保存變長的大字元串,可以組多到65535 (2**16 − 1)個字元。
 68 
 69         mediumtext
 70             A TEXT column with a maximum length of 16,777,215 (2**24 − 1) characters.
 71 
 72         longtext
 73             A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 − 1) characters.
 74 
 75 
 76         enum
 77             枚舉類型,
 78             An ENUM column can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.)
 79             示例:
 80                 CREATE TABLE shirts (
 81                     name VARCHAR(40),
 82                     size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
 83                 );
 84                 INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'),('polo shirt','small');
 85 
 86         set
 87             集合類型
 88             A SET column can have a maximum of 64 distinct members.
 89             示例:
 90                 CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));
 91                 INSERT INTO myset (col) VALUES ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d');
 92 
 93         DATE
 94             YYYY-MM-DD(1000-01-01/9999-12-31 95 
 96         TIME
 97             HH:MM:SS('-838:59:59'/'838:59:59' 98 
 99         YEAR
100             YYYY(1901/2155101 
102         DATETIME
103 
104             YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59    Y)
105 
106         TIMESTAMP
107 
108             YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某時)
View Code

二進位數據:TinyBlob、Blob、MediumBlob、LongBlob

二、表內容操作

1、增

1 insert into 表 (列名,列名...) values (值,值,值...)
2 insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)
3 insert into 表 (列名,列名...) select (列名,列名...) from

2、刪

1 delete from2 delete from 表 where id=1 and name='alex'

3、改

1 update 表 set name = 'alex' where id>1

4、查

1 select * from2 select * from 表 where id > 1
3 select nid,name,gender as gg from 表 where id > 1

5、其他

 1 a、條件
 2     select * from 表 where id > 1 and name != 'alex' and num = 12;
 3  
 4     select * from 表 where id between 5 and 16;
 5  
 6     select * from 表 where id in (11,22,33)
 7     select * from 表 where id not in (11,22,33)
 8     select * from 表 where id in (select nid from 表)
 9  
10 b、通配符
11     select * from 表 where name like 'ale%'  - ale開頭的所有(多個字元串)
12     select * from 表 where name like 'ale_'  - ale開頭的所有(一個字元)
13  
14 c、限制
15     select * from 表 limit 5;            - 前5行
16     select * from 表 limit 4,5;          - 從第4行開始的5行
17     select * from 表 limit 5 offset 4    - 從第4行開始的5行
18  
19 d、排序
20     select * from 表 order by 列 asc              - 根據 “列” 從小到大排列
21     select * from 表 order by 列 desc             - 根據 “列” 從大到小排列
22     select * from 表 order by 列1 desc,列2 asc    - 根據 “列1” 從大到小排列,如果相同則按列2從小到大排序
23  
24 e、分組
25     select num from 表 group by num
26     select num,nid from 表 group by num,nid
27     select num,nid from 表  where nid > 10 group by num,nid order nid desc
28     select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid
29  
30     select num from 表 group by num having max(id) > 10
31  
32     特別的:group by 必須在where之後,order by之前
33  
34 f、連表
35     無對應關係則不顯示
36     select A.num, A.name, B.name
37     from A,B
38     Where A.nid = B.nid
39  
40     無對應關係則不顯示
41     select A.num, A.name, B.name
42     from A inner join B
43     on A.nid = B.nid
44  
45     A表所有顯示,如果B中無對應關係,則值為null
46     select A.num, A.name, B.name
47     from A left join B
48     on A.nid = B.nid
49  
50     B表所有顯示,如果B中無對應關係,則值為null
51     select A.num, A.name, B.name
52     from A right join B
53     on A.nid = B.nid
54  
55 g、組合
56     組合,自動處理重合
57     select nickname
58     from A
59     union
60     select name
61     from B
62  
63     組合,不處理重合
64     select nickname
65     from A
66     union all
67     select name
68     from B
View Code

 


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

-Advertisement-
Play Games
更多相關文章
  • #timedatectl set-timezone Asia/Shanghai 關閉SELinux vi /etc/sysconfig/selinux #SELINUX=enforcing SELINUX=disabled setenforce 0 getenforce #yum install - ...
  • 1、mysql幾個重要的文件 每個資料庫新建後,會產生資料庫文件夾,在該文件夾下每張表均對應以下三個文件: xx.frm 存放表結構 xx.MYD 存放表數據 xx.MYI 存放表索引 mysq非常重要的配置文件 my.ini 全局配置文件 2、mysql的邏輯架構 連接層 服務層 引擎層:存儲引擎 ...
  • 數據列定義 表中數據行的數據插入和數據類型都是基於數據列的,學會添加數據列在開發過程中是必不可少的。 使用SSMS資料庫管理工具添加數據列 在數據表中添加一列或者多列步驟相同 1、連接資料庫,選擇數據表-》右鍵點擊-》選擇設計。 2、在新打開的視窗中輸入中-》輸入列名,數據類型,是否可空-》在下麵輸 ...
  • 1、表欄位的操作 1、語法 :alter table 表名 執行動作; 2、添加欄位(add) alter table 表名 add 欄位名 數據類型; alter table 表名 add 欄位名 數據類型 first; alter table 表名 add 欄位名 數據類型 after 欄位名; ...
  • DECLARE @UserID INT; --推廣員帳號 DECLARE @ProxyID INT; --代理帳號 DECLARE @Score INT=1000; --分數 SELECT @UserID = [SpreaderID] FROM [QPAccountsDB].[dbo].[Accou... ...
  • 使用高版本的出現各種問題: 1. driverClass需要更換為 2. url後面需要加上SSL=false 3. 時間不同步,導致無法鏈接資料庫,提示說是時區設置有問題,可以通過在url後面帶時區設置參數解決 4. 出現在資料庫中找到多張同名的數據表,配置文件中的資料庫名的限制不起作用,而且還生 ...
  • 一.概述 mysql可以在多個平臺上運行,在windows平臺上安裝有noinstall包和圖形化包二種方式。在linux/unix平臺上有RPM包安裝,二進位包(Binary Package)安裝,源碼包(Source package)安裝。 對於RPM包的最大優點是安裝簡單,適合初學者學習使用, ...
  • with cte as ( select belongsAgent from [QPProxyDB].[dbo].[BS_ProxyInfo] where ProxyID = @ProxyID union all select a.ProxyID from [QPProxyDB].[dbo].[BS... ...
一周排行
    -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# ...