MySQL-03:数据表操作基本命令笔记
生活随笔
收集整理的這篇文章主要介紹了
MySQL-03:数据表操作基本命令笔记
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄
?
數(shù)據(jù)表
1、創(chuàng)建表
2、刪除表
3、清空表
4、修改表
5、基本數(shù)據(jù)類型
數(shù)據(jù)表
1、創(chuàng)建表
create table 表名(列名? 類型? 是否可以為空,列名? 類型? 是否可以為空 )ENGINE=InnoDB DEFAULT CHARSET=utf8?
是否可空,null表示空,非字符串not null - 不可空null - 可空 默認(rèn)值,創(chuàng)建列時可以指定默認(rèn)值,當(dāng)插入數(shù)據(jù)時如果未主動設(shè)置,則自動添加默認(rèn)值create table tb1(nid int not null defalut 2,num int not null)?
?
自增,如果為某列設(shè)置自增列,插入數(shù)據(jù)時無需設(shè)置此列,默認(rèn)將自增(表中只能有一個自增列)create table tb1(nid int not null auto_increment primary key,num int null)或create table tb1(nid int not null auto_increment,num int null,index(nid))注意:1、對于自增列,必須是索引(含主鍵)。2、對于自增可以設(shè)置步長和起始值show session variables like 'auto_inc%';set session auto_increment_increment=2;set session auto_increment_offset=10;shwo global variables like 'auto_inc%';set global auto_increment_increment=2;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)) 外鍵,一個特殊的索引,只能是指定內(nèi)容creat table color(nid int not null primary key,name char(16) not null)create table fruit(nid int not null primary key,smt char(32) null ,color_id int not null,constraint fk_cc foreign key (color_id) references color(nid))2、刪除表
drop table 表名3、清空表
delete?from 表名 truncate table 表名4、修改表
添加列:alter table 表名 add 列名 類型 刪除列:alter table 表名 drop column 列名 修改列:alter table 表名 modify column 列名 類型;? -- 類型alter table 表名 change 原列名 新列名 類型; -- 列名,類型添加主鍵:alter table 表名 add primary key(列名); 刪除主鍵:alter table 表名 drop primary key;alter table 表名? modify? 列名 int, drop primary key;添加外鍵:alter table 從表 add constraint 外鍵名稱(形如:FK_從表_主表) foreign key 從表(外鍵字段) references 主表(主鍵字段); 刪除外鍵:alter table 表名 drop foreign key 外鍵名稱修改默認(rèn)值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000; 刪除默認(rèn)值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;5、基本數(shù)據(jù)類型
MySQL的數(shù)據(jù)類型大致分為:數(shù)值、時間和字符串
?
bit[(M)]二進(jìn)制位(101001),m表示二進(jìn)制位的長度(1-64),默認(rèn)m=1tinyint[(m)] [unsigned] [zerofill]小整數(shù),數(shù)據(jù)類型用于保存一些范圍的整數(shù)數(shù)值范圍:有符號:-128 ~ 127.無符號:0 ~ 255特別的: MySQL中無布爾值,使用tinyint(1)構(gòu)造。int[(m)][unsigned][zerofill]整數(shù),數(shù)據(jù)類型用于保存一些范圍的整數(shù)數(shù)值范圍:有符號:-2147483648 ~ 2147483647無符號:0 ~ 4294967295特別的:整數(shù)類型中的m僅用于顯示,對存儲范圍無限制。例如: int(5),當(dāng)插入數(shù)據(jù)2時,select 時數(shù)據(jù)顯示為: 00002bigint[(m)][unsigned][zerofill]大整數(shù),數(shù)據(jù)類型用于保存一些范圍的整數(shù)數(shù)值范圍:有符號:-9223372036854775808 ~ 9223372036854775807無符號:0 ~ 18446744073709551615decimal[(m[,d])] [unsigned] [zerofill]準(zhǔn)確的小數(shù)值,m是數(shù)字總個數(shù)(負(fù)號不算),d是小數(shù)點后個數(shù)。 m最大值為65,d最大值為30。特別的:對于精確數(shù)值計算時需要用此類型decaimal能夠存儲精確值的原因在于其內(nèi)部按照字符串存儲。FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]單精度浮點數(shù)(非準(zhǔn)確小數(shù)值),m是數(shù)字總個數(shù),d是小數(shù)點后個數(shù)。無符號:-3.402823466E+38 to -1.175494351E-38,01.175494351E-38 to 3.402823466E+38有符號:01.175494351E-38 to 3.402823466E+38**** 數(shù)值越大,越不準(zhǔn)確 ****DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]雙精度浮點數(shù)(非準(zhǔn)確小數(shù)值),m是數(shù)字總個數(shù),d是小數(shù)點后個數(shù)。無符號:-1.7976931348623157E+308 to -2.2250738585072014E-30802.2250738585072014E-308 to 1.7976931348623157E+308有符號:02.2250738585072014E-308 to 1.7976931348623157E+308**** 數(shù)值越大,越不準(zhǔn)確 ****char (m)char數(shù)據(jù)類型用于表示固定長度的字符串,可以包含最多達(dá)255個字符。其中m代表字符串的長度。PS: 即使數(shù)據(jù)小于m長度,也會占用m長度varchar(m)varchars數(shù)據(jù)類型用于變長的字符串,可以包含最多達(dá)255個字符。其中m代表該數(shù)據(jù)類型所允許保存的字符串的最大長度,只要長度小于該最大值的字符串都可以被保存在該數(shù)據(jù)類型中。注:雖然varchar使用起來較為靈活,但是從整個系統(tǒng)的性能角度來說,char數(shù)據(jù)類型的處理速度更快,有時甚至可以超出varchar處理速度的50%。因此,用戶在設(shè)計數(shù)據(jù)庫時應(yīng)當(dāng)綜合考慮各方面的因素,以求達(dá)到最佳的平衡texttext數(shù)據(jù)類型用于保存變長的大字符串,可以組多到65535 (2**16 ? 1)個字符。mediumtextA TEXT column with a maximum length of 16,777,215 (2**24 ? 1) characters.longtextA TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 ? 1) characters.enum枚舉類型,An ENUM column can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.)示例:CREATE TABLE shirts (name VARCHAR(40),size ENUM('x-small', 'small', 'medium', 'large', 'x-large'));INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'),('polo shirt','small');set集合類型A SET column can have a maximum of 64 distinct members.示例:CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));INSERT INTO myset (col) VALUES ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d');DATEYYYY-MM-DD(1000-01-01/9999-12-31)TIMEHH:MM:SS('-838:59:59'/'838:59:59')YEARYYYY(1901/2155)DATETIMEYYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59 Y)TIMESTAMPYYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某時)?
摘抄自:https://www.cnblogs.com/wupeiqi/articles/5713315.html
?
?
?
總結(jié)
以上是生活随笔為你收集整理的MySQL-03:数据表操作基本命令笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL-02:“数据库”操作基本命令
- 下一篇: MySQL-04:数据内容操作-增删改查