mysql 语法

来源:互联网 发布:淘宝可靠的正品美瞳店 编辑:程序博客网 时间:2024/05/16 03:11


//主外键关系

create database racetrack_dev;


create table race(
id bigint(20) not NULL AUTO_INCREMENT,
version bigint(20) not NULL,
distance float not NULL,
max_runners int(11) not NULL,
start_date_time datetime not Null,
state float not Null,
cost varchar(255) not Null,
primary key(id),
city varchar(255) not Null
)


DESCRIBE race;


create table registration(
id bigint(20) not NULL AUTO_INCREMENT,
version bigint(20) not NULL,
gender varchar(255) not Null,
date_of_birth datetime not NULL,
postal_address varchar(255),
created_at datetime not NULL,
race_id bigint(20) NULL,
name varchar(255) not NUll,
primary key(id),
foreign key(race_id)REFERENCES race(id)
)


DESCRIBE registration;


//已经有的表上 添加一列


如果想在一个已经建好的表中添加一列,可以用诸如:

alter table t1 add column addr varchar(20) not null;

这条语句会向已有的表t1中加入一列addr,这一列在表的最后一列位置。如果我们希望添加在指定的一列,可以用:

alter table t1 add column addr varchar(20) not null after user1;

注意,上面这个命令的意思是说添加addr列到user1这一列后面。如果想添加到第一列的话,可以用:

alter table t1 add column addr varchar(20) not null first;