mysql(數據表基本操作)
一、數據表基本操作
1、創建表
create table 表名(
列名 類型 是否可以為空,
列名 類型 是否可以為空
)ENGINE=InnoDB DEFAULT CHARSET=utf8
查看表結構
desc 表名;
1)是否可空,null表示空,非字符串
not null - 不可空
null - 可空
2)默認值
默認值,創建列時可以指定默認值,當插入數據時如果未主動設置,則自動添加默認值
create table tb1(
nid int not null defalut 2,
num int not null
)
3)自增
自增,如果為某列設置自增列,插入數據時無需設置此列,默認將自增(表中只能有一個自增列)
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、一張表只能有一個自增列。
2、對於自增可以設置步長和起始值。
4)主鍵
一張表只能有一個主鍵,唯一不能重復,不能為null,一般情況下將自增列設置為主鍵,一張表中的主鍵可以多列組合
主鍵,一種特殊的唯一索引,不允許有空值,如果主鍵使用單個列,則它的值必須唯一,如果是多列,則其組合必須唯一。
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) #聯合主鍵
)
5)外鍵
外鍵,一個特殊的索引,只能是指定內容
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、修改表
1)添加列:alter table 表名 add 列名 類型;
2)刪除列:alter table 表名 drop column 列名;
3)修改列:
alter table 表名 modify column 列名 類型; -- 類型
alter table 表名 change 原列名 新列名 類型; -- 列名,類型
4)添加主鍵:
alter table 表名 add primary key(列名);
5)刪除主鍵:
alter table 表名 drop primary key;
alter table 表名 modify 列名 int, drop primary key;
6)添加外鍵
alter table 從表 add constraint 外鍵名稱(形如:FK_從表_主表) foreign key 從表(外鍵字段) references 主表(主鍵字段);
7)刪除外鍵
alter table 表名 drop foreign key 外鍵名稱
8)修改默認值
ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
9)刪除默認值
ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
二 表內容操作
1、增
insert into 表 (列名,列名...) values (值,值,值...)
insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)
insert into 表 (列名,列名...) select (列名,列名...) from 表
2、刪
delete from 表
delete from 表 where id=1 and name=‘alex‘
3、查
select * from 表
select * from 表 where id > 1
select nid,name,gender as gg from 表 where id > 1
4、改
update
表
set
name
=
‘alex‘
where
id>1
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
mysql(數據表基本操作)