建表



建表语句
create table <表名>

字段名1,数据类型[列级别约束条件][默认值],
字段名1,数据类型[列级别约束条件][默认值],
......
[表级别约束条件]
);

例:
create table tb_emp2
(
id int(11),
name varchar(25),
deptld int(11),
salary float,
constraint id_pk primary key(id)
)

create table tb_emp5
(
id int(11) primary key,
name varchar(25) not null,
deptld int(11),
salary float unique,
constraint fk_emp_dept1 foreign key (deptld) references tb_dept1(id)
)


查看建表语句
show create table tb_emp1;
Show index from tb_emp1;


表改名
alter table tb_emp1 rename  to tb_emp7;

修改表结构
1.修改字段数据类型
alter table <表名> modify <字段名> <数据类型>
alter table tb8 modify id float;

2.修改字段名
alter table <表名> change <旧字段名> <新字段名> <新的数据类型>
alter table tb8 change id newid int;

3.添加字段
alter table <表名> add <新字段名> <数据类型> [约束条件] [first|after 已存在的字段名]
alter table tb8 add test01 int;      //默认加在最后面
alter table tb8 add test02 int first ;  //first表示将新字段加在首位
alter table tb8 add test03 int after newid;  //after表示加在哪个字段的后面
alter table tb8 add test04 int(5) not null;

4.删除字段
alter table tb8 drop test04;

5.修改字段的排列位置
alter table tb8 modify <字段1> <数据类型> first|after <字段2>
alter table tb8 modify test02 float after salary;

6.修改表的存储引擎
alter table <表名> engine=<更改后的存储引擎名>
alter table tb8 engine=memory;

7.删除表的外键约束
alter table <表名> drop foreign key <外键约束名>
alter table test03 add constraint ruboob_id_Fk foreign key (runoob_id) references test02 (runoob_id);
alter table test03 drop foreign key ruboob_id_Fk

8.删除数据表
drop table [if exists] t1,t2,t3;  //if exists表示检查表是否存在,如果表不存在,会发出警告,但会继续执行

实验过程
1.创建数据库company
create database company;
2.创建表offices
create table offices
(
officecode int(10),
city int(11) not null,
address varchar(50),
country varchar(50) not null,
postalcode varchar(25),
constraint OC_PK primary key (officecode),
constraint PC_UK unique  (postalcode)
);
3.创建表employees
create table employees
(
employeenumber int(11),
lastname varchar(50) not null,
firstname varchar(50) not null,
mobile varchar(25),
officecode int(10),
jobtitle varchar(50) not null,
birth datetime not null,
note varchar(255),
sex varchar(5),
constraint EN_PK primary key auto_increment(employeenumber),
constraint MB_UK unique  (mobile),
constraint OC_FK foreign key (officecode) references offices(officecode)
);

建表语句
create table <表名>

字段名1,数据类型[列级别约束条件][默认值],
字段名1,数据类型[列级别约束条件][默认值],
......
[表级别约束条件]
);

例:
create table tb_emp2
(
id int(11),
name varchar(25),
deptld int(11),
salary float,
constraint id_pk primary key(id)
)

create table tb_emp5
(
id int(11) primary key,
name varchar(25) not null,
deptld int(11),
salary float unique,
constraint fk_emp_dept1 foreign key (deptld) references tb_dept1(id)
)


查看建表语句
show create table tb_emp1;
Show index from tb_emp1;


表改名
alter table tb_emp1 rename  to tb_emp7;

修改表结构
1.修改字段数据类型
alter table <表名> modify <字段名> <数据类型>
alter table tb8 modify id float;

2.修改字段名
alter table <表名> change <旧字段名> <新字段名> <新的数据类型>
alter table tb8 change id newid int;

3.添加字段
alter table <表名> add <新字段名> <数据类型> [约束条件] [first|after 已存在的字段名]
alter table tb8 add test01 int;      //默认加在最后面
alter table tb8 add test02 int first ;  //first表示将新字段加在首位
alter table tb8 add test03 int after newid;  //after表示加在哪个字段的后面
alter table tb8 add test04 int(5) not null;

4.删除字段
alter table tb8 drop test04;

5.修改字段的排列位置
alter table tb8 modify <字段1> <数据类型> first|after <字段2>
alter table tb8 modify test02 float after salary;

6.修改表的存储引擎
alter table <表名> engine=<更改后的存储引擎名>
alter table tb8 engine=memory;

7.删除表的外键约束
alter table <表名> drop foreign key <外键约束名>
alter table test03 add constraint ruboob_id_Fk foreign key (runoob_id) references test02 (runoob_id);
alter table test03 drop foreign key ruboob_id_Fk

8.删除数据表
drop table [if exists] t1,t2,t3;  //if exists表示检查表是否存在,如果表不存在,会发出警告,但会继续执行

实验过程
1.创建数据库company
create database company;
2.创建表offices
create table offices
(
officecode int(10),
city int(11) not null,
address varchar(50),
country varchar(50) not null,
postalcode varchar(25),
constraint OC_PK primary key (officecode),
constraint PC_UK unique  (postalcode)
);
3.创建表employees
create table employees
(
employeenumber int(11),
lastname varchar(50) not null,
firstname varchar(50) not null,
mobile varchar(25),
officecode int(10),
jobtitle varchar(50) not null,
birth datetime not null,
note varchar(255),
sex varchar(5),
constraint EN_PK primary key auto_increment(employeenumber),
constraint MB_UK unique  (mobile),
constraint OC_FK foreign key (officecode) references offices(officecode)
);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值