MySQL 建表语法

1、最简单的:

CREATE   TABLE  t1(
   id 
int  not  null ,
   name 
char ( 20 )
);

2、带主键的:

a:
CREATE   TABLE  t1(
   id 
int  not  null primary key ,
   name 
char ( 20 )
);

b:复合主键
CREATE   TABLE  t1(
   id 
int  not  null ,
   name char
( 20 ),
   primary key (id,name)
);

3、带默认值的:

CREATE   TABLE  t1(
   id 
int  not  null default 0 primary key ,
   name char
( 20 ) default '1'
);

转贴的:
CREATE   TABLE  PLAYERS
       (PLAYERNO      
INTEGER   NOT   NULL   PRIMARY   KEY ,
        NAME          
CHAR ( 15 NOT   NULL ,
        INITIALS      
CHAR ( 3 NOT   NULL ,
        BIRTH_DATE    DATE,
        SEX           
CHAR ( 1 NOT   NULL
                     
CHECK (SEX  IN  ( ' M ' , ' F ' )),
        JOINED        
SMALLINT   NOT   NULL
                     
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   NOT   NULL   PRIMARY   KEY ,
        PLAYERNO      
INTEGER   NOT   NULL ,
        DIVISION      
CHAR ( 6 )    NOT   NULL
                     
CHECK (DIVISION  IN  ( ' first ' , ' second ' )),
        
FOREIGN   KEY    (PLAYERNO)  REFERENCES  PLAYERS (PLAYERNO))
;
CREATE   TABLE  MATCHES
       (MATCHNO       
INTEGER   NOT   NULL   PRIMARY   KEY ,
        TEAMNO        
INTEGER   NOT   NULL ,
        PLAYERNO      
INTEGER   NOT   NULL ,
        WON           
SMALLINT   NOT   NULL
                     
CHECK (WON  BETWEEN   0   AND   3 ),
        LOST          
SMALLINT   NOT   NULL
                     
CHECK (LOST  BETWEEN   0   AND   3 ),
        
FOREIGN   KEY  (TEAMNO)  REFERENCES  TEAMS (TEAMNO),
        
FOREIGN   KEY  (PLAYERNO)  REFERENCES  PLAYERS (PLAYERNO))
;
CREATE   TABLE  PENALTIES
       (PAYMENTNO     
INTEGER   NOT   NULL   PRIMARY   KEY ,
        PLAYERNO      
INTEGER   NOT   NULL ,
        PAYMENT_DATE DATE 
NOT   NULL
                     
CHECK (PAYMENT_DATE  >=  DATE( ' 1969-12-31 ' )),
        AMOUNT        
DECIMAL ( 7 , 2 )    NOT   NULL
                     
CHECK  (AMOUNT  >   0 ),
        
FOREIGN   KEY  (PLAYERNO)  REFERENCES  PLAYERS (PLAYERNO))
;
CREATE   TABLE  COMMITTEE_MEMBERS
       (PLAYERNO      
INTEGER   NOT   NULL ,
        BEGIN_DATE    DATE 
NOT   NULL ,
        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 ' )))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值