3 mysql資料庫表操作

2021-09-20 01:47:57 字數 2385 閱讀 8564

[toc]

create table 表名(

列名 型別 是否可以為空,

列名 型別 是否可以為空

)engine=innodb default charset=utf8

是否可以為空

預設值預設值,建立列時可以指定預設值,當插入資料時如果未主動設定,則自動新增預設值

create table 表名(

nid int not null de****t 2,

num int not null

)

自增,如果為某列設定自增列,插入資料時無需設定此列,預設將自增(表中只能有乙個自增列)

注意:對於自增列,必須是索引(含主鍵)

對於自增可以設定步長和起始值

create table 表名(

nid int not null auto_increment primary key,

num int null)或

create table 表名(

nid int not null auto_increment,

num int null,

index(nid)

)

auto_increment表示:自增

primary key表示: 主鍵約束(不能重複且不能為空),有加速查詢的效果

主鍵,一種特殊的唯一索引,不允許有空值,如果主鍵使用單個列,則它的值必須唯一,如果是多列,則其組合必須唯一。

create table 表名(

nid int not null auto_increment primary key,

num int null

)

create table 表名(

nid int not null,

num int not null,

primary key(nid,num)

)

唯一索引(此列值不能重複)

==unique 唯一索引名稱 (列名)==

create table t1(

id int ....,

num int,

xx int,

unique uq1 (num),

constraint ....

)

聯合唯一索引(此兩列排列組合值不能重複)

==unique 唯一索引名稱 (列名,列名)==

create table t1(

id int ....,

num int,

xx int,

unique uq1 (num,xx),

constraint ....

)

外來鍵,乙個特殊的索引,只能是指定內容

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)

)

mysql: 設定自增步長

基於會話級別:

基於全域性級別(基本上不用):

基礎表級別:

create table `t5` (

`nid` int(11) not null auto_increment,

`pid` int(11) not null,

`num` int(11) default null,

primary key (`nid`,`pid`)

) engine=innodb auto_increment=4, 步長=2 default charset=utf8

create table `t6` (

`nid` int(11) not null auto_increment,

`pid` int(11) not null,

`num` int(11) default null,

primary key (`nid`,`pid`)

) engine=innodb auto_increment=4, 步長=20 default charset=utf8

資料庫(3) MySQL建庫 建表

show databases use database名稱 例如 use mysql 如果沒有進入某一庫,在對庫中的資料進行訪問時,會提示 no database selected 檢視當前已進入的資料庫 show tables 語法 drop database 庫名 例如 drop databas...

MySql資料庫(3)MySql資料管理

create table grade gradeid int 10 not null auto increment comment 年級id gradename varchar 50 not null comment 年級名稱 primary key gradeid engine innodb de...

3 mysql學習之資料庫定義語句

資料定義語句包括alter database alter table create database create index create table drop database drop index drop table rename table語法 一 alter database語法 常見的...