mysql建表語句

2021-08-30 14:13:38 字數 3729 閱讀 3417

mysql裝好以後,進入命令列,開始建表需要先建立乙個database.

開始使用:

create database mybase;

use mybase;

create table user(id int(10) auto_increment not null primary key,username varchar(25),password varchar(10));

mysql中如何找到系統表?

show tables from your_db;

use mysql;//mysql是mysql的系統表的database

show tables;

1、最簡單的:

create

table

t1(id 

intnot 

null

,name 

char(20

));

2、帶主鍵的: a:

create

table

t1(id 

intnot 

null primary key

,name 

char(20

));b:復合主鍵

create

table

t1(id 

intnot 

null

,name char(20

),primary key (id,name));

3、帶預設值的:

create

table

t1(id 

intnot 

null default 0 primary key

,name char(20

) default '1');

轉貼的:

create

table

players

(playerno      

integer

notnull

primary

key,

name          

char(15

) not

null

,initials      

char(3

) not

null

,birth_date    date,

***           

char(1

) not

null

check

(*** in(

'm',

'f')),

joined        

smallint

notnull

check

(joined 

>

1969

) ,street        

char(30

) not

null

,houseno       

char(4

),postcode      

char(6

) check

(postcode 

like

'______'),

town          

char(10

) not

null

,phoneno       

char(13

),leagueno      

char(4

));create

table

teams

(teamno        

integer

notnull

primary

key,

playerno      

integer

notnull

,division      

char(6

)   

notnull

check

(division in(

'first',

'second

')),

foreign

key(playerno) 

references

players (playerno))

;create

table

matches

(matchno       

integer

notnull

primary

key,

teamno        

integer

notnull

,playerno      

integer

notnull

,won           

smallint

notnull

check

(won 

between

0and3),

lost          

smallint

notnull

check

(lost 

between

0and3),

foreign

key(teamno) 

references

teams (teamno),

foreign

key(playerno) 

references

players (playerno))

;create

table

penalties

(paymentno     

integer

notnull

primary

key,

playerno      

integer

notnull

,payment_date date 

notnull

check

(payment_date 

>=

date(

'1969-12-31

')),

amount        

decimal(7

,2)   

notnull

check

(amount 

>0),

foreign

key(playerno) 

references

players (playerno))

;create

table

committee_members

(playerno      

integer

notnull

,begin_date    date 

notnull

,end_date      date,

position      

char(20

),primary

key(playerno, begin_date),

foreign

key(playerno) 

references

players (playerno),

check

(begin_date 

<

end_date),

check

(begin_date 

>=

date(

'1990-01-01

')));

mysql建表語句

在sql語句中注意 約束的概念 1.實體完整性約束 主鍵 唯一且非空 primary key 違約處理 no action 拒絕執行 2.參照完整性約束 外來鍵約束 foregin key references tablename filedname on delete update casecad...

mysql建表語句

工作的時候總會寫一些建表語句提交給db,有的時候就會忘記主鍵自增寫法,以及一些型別的標註,下面是乙個比較全的建表語句,包括各種型別。create table minisite lock site id int not null auto increment primary key,admin id ...

mysql建表語句

create table who csjcm transfer jollycall data rec id int 11 unsigned not null auto increment comment 唯一自增主鍵 user id int 11 not null default 0 comment...