mysql(三) 資料表的基本操作操作
阿新 • • 發佈:2019-01-13
mysql(三) 資料表的基本操作操作
建立表,曾刪改查,主鍵,外來鍵,基本資料型別。1. 建立表
create table 表名(
列名 型別 是否可以為空,
列名 型別 是否可以為空
)ENGINE=InnoDB DEFAULT CHARSET=utf8
例如:
型別解釋:
- 是否可以為空:
是否可空,null表示空,非字串
not null - 不可空
null - 可空
- 預設值設定
預設值,建立列時可以指定預設值,當插入資料時如果未主動設定,則自動新增預設值
create table tb1(
nid int not null defalut 2,
num int not null
)
- 自增
自增,如果為某列設定自增列,插入資料時無需設定此列,預設將自增(表中只能有一個自增列)
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、對於自增可以設定步長和起始值
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)
)
- 外來鍵
外來鍵,一個特殊的索引,只能是指定內容
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 表名
enter description here
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 外來鍵名稱
修改預設值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
刪除預設值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
5. 基本資料型別
MySQL的資料型別大致分為:數值、時間和字串
bit[(M)]
二進位制位(101001),m表示二進位制位的長度(1-64),預設m=1
tinyint[(m)] [unsigned] [zerofill]
小整數,資料型別用於儲存一些範圍的整數數值範圍:
有符號:
-128 ~ 127.
無符號:
0 ~ 255
特別的: MySQL中無布林值,使用tinyint(1)構造。
int[(m)][unsigned][zerofill]
整數,資料型別用於儲存一些範圍的整數數值範圍:
有符號:
-2147483648 ~ 2147483647
無符號:
0 ~ 4294967295
特別的:整數型別中的m僅用於顯示,對儲存範圍無限制。例如: int(5),當插入資料2時,select 時資料顯示為: 00002
bigint[(m)][unsigned][zerofill]
大整數,資料型別用於儲存一些範圍的整數數值範圍:
有符號:
-9223372036854775808 ~ 9223372036854775807
無符號:
0 ~ 18446744073709551615
decimal[(m[,d])] [unsigned] [zerofill]
準確的小數值,m是數字總個數(負號不算),d是小數點後個數。 m最大值為65,d最大值為30。
特別的:對於精確數值計算時需要用此型別
decaimal能夠儲存精確值的原因在於其內部按照字串儲存。
FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]
單精度浮點數(非準確小數值),m是數字總個數,d是小數點後個數。
無符號:
-3.402823466E+38 to -1.175494351E-38,
0
1.175494351E-38 to 3.402823466E+38
有符號:
0
1.175494351E-38 to 3.402823466E+38
**** 數值越大,越不準確 ****
DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]
雙精度浮點數(非準確小數值),m是數字總個數,d是小數點後個數。
無符號:
-1.7976931348623157E+308 to -2.2250738585072014E-308
0
2.2250738585072014E-308 to 1.7976931348623157E+308
有符號:
0
2.2250738585072014E-308 to 1.7976931348623157E+308
**** 數值越大,越不準確 ****
char (m)
char資料型別用於表示固定長度的字串,可以包含最多達255個字元。其中m代表字串的長度。
PS: 即使資料小於m長度,也會佔用m長度
varchar(m)
varchars資料型別用於變長的字串,可以包含最多達255個字元。其中m代表該資料型別所允許儲存的字串的最大長度,只要長度小於該最大值的字串都可以被儲存在該資料型別中。
注:雖然varchar使用起來較為靈活,但是從整個系統的效能角度來說,char資料型別的處理速度更快,有時甚至可以超出varchar處理速度的50%。因此,使用者在設計資料庫時應當綜合考慮各方面的因素,以求達到最佳的平衡
text
text資料型別用於儲存變長的大字串,可以組多到65535 (2**16 − 1)個字元。
mediumtext
A TEXT column with a maximum length of 16,777,215 (2**24 − 1) characters.
longtext
A 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');
DATE
YYYY-MM-DD(1000-01-01/9999-12-31)
TIME
HH:MM:SS('-838:59:59'/'838:59:59')
YEAR
YYYY(1901/2155)
DATETIME
YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59 Y)
TIMESTAMP
YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某時)
二 表內容的操作
1、增
insert into 表 (列名,列名...) values (值,值,值...)
insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)
insert into 表 (列名,列名...) select (列名,列名...) from 表
2、刪
delete from 表
delete from 表 where id=1 and name='alex'
3、改
update 表 set name = 'alex' where id>1
4、查
select * from 表
select * from 表 where id > 1
select nid,name,gender as gg from 表 where id > 1
5、擴充套件
a、條件
select * from 表 where id > 1 and name != 'alex' and num = 12;
select * from 表 where id between 5 and 16;
select * from 表 where id in (11,22,33)
select * from 表 where id not in (11,22,33)
select * from 表 where id in (select nid from 表)
b、萬用字元
select * from 表 where name like 'ale%' - ale開頭的所有(多個字串)
select * from 表 where name like 'ale_' - ale開頭的所有(一個字元)
c、限制
select * from 表 limit 5; - 前5行
select * from 表 limit 4,5; - 從第4行開始的5行
select * from 表 limit 5 offset 4 - 從第4行開始的5行
d、排序
select * from 表 order by 列 asc - 根據 “列” 從小到大排列
select * from 表 order by 列 desc - 根據 “列” 從大到小排列
select * from 表 order by 列1 desc,列2 asc - 根據 “列1” 從大到小排列,如果相同則按列2從小到大排序
e、分組
select num from 表 group by num
select num,nid from 表 group by num,nid
select num,nid from 表 where nid > 10 group by num,nid order nid desc
select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid
select num from 表 group by num having max(id) > 10
特別的:group by 必須在where之後,order by之前
f、連表
無對應關係則不顯示
select A.num, A.name, B.name
from A,B
Where A.nid = B.nid
無對應關係則不顯示
select A.num, A.name, B.name
from A inner join B
on A.nid = B.nid
A表所有顯示,如果B中無對應關係,則值為null
select A.num, A.name, B.name
from A left join B
on A.nid = B.nid
B表所有顯示,如果B中無對應關係,則值為null
select A.num, A.name, B.name
from A right join B
on A.nid = B.nid
g、組合
組合,自動處理重合
select nickname
from A
union
select name
from B
組合,不處理重合
select nickname
from A
union all
select name
from B