数据表的基本操作

0x01  创建数据表

1 创建员工表tb_emp1

首先创建数据库,sql语句:create  database test_db;
选择创建表的数据库,sql语句:use test_db;
创建tb_emp1表,sql语句为:
create table tb_emp1
(
id      int(11),
name   varchar(25),
deptid  int(11),
salary  float
);
语句执行后,便创建了一个名称为tb_emp1的数据表,使用show tables;语句查看数据表是否创建成功,sql语句如下:
 show tables;

可以看到,test_db数据库中已经有了数据表tb_tmp1,数据表创建成功。

0x02 主键的创建

1 定义数据表tb_emp 2,其主键为id,sql语句如下:

create table tb_emp2
(
id     int(11) primary key,
name   varchar(25),
deptid int(11),
salary float
);
2 定义数据表tb_emp 3,其主键为id,sql语句如下:
create table tb_emp3 
(
id int(11),
name varchar(25),
deptid int(11),
salary float,
primary key(id)
);
上述两个例子执行后的结果是一样的,都会在id字段上设置主键约束。
3 定义数据表tb_emp4,假设表中间没有主键id,为了唯一确定一个员工,可以把name、deptid联合起来做为主键,sql语句如下:
create table tb_emp4
 (
name varchar(25),
deptid int(11),
salary float,
primary key(name,deptid)

);

0x03 外键约束

1 定义数据表tb_emp5,并在tb_emp5表上创建外键约束。
创建一个部门表tb_dept1,sql语句如下:
create table tb_dept1
(
id       int(11) primary key,
name    varchar(22)  not null,
location  varchar(50)
);

2 定义数据表tb_emp5,让它的键deptid作为外键关联到tb_dept1的主键id,sql语句为:
create table tb_emp5 
(
id      int(11) primary key,
name   varchar(25),
deptid  int(11), 
salary   float,
constraint fk_emp_dept1 foreign key(deptid) references tb_dept1(id)
);
以上语句执行成功之后,在表tb_emp5上添加了名称为fk_emp_dept1的外键约束,外键名称为deptid,其依赖于表tb_dept1的主键id。

0x04 非空约束

定义数据表tb_emp6,指定员工的名称不能为空,sql语句如下:

create table tb_emp6 
(
id     int(11) primary key,
name   varchar(25) not null,
deptid  int(11), 
salary  float
);

0x05 唯一性约束

1 定义数据表tb_dept2,指定部门的名称唯一,sql语句如下:

create table tb_dept2 
(
id      int(11) primary key,
name    varchar(22) unique,
location  varchar(50)
);
2 定义数据表tb_dept3,指定部门的名称唯一,sql语句如下:
create table tb_dept3 
(
id      int(11) primary key,
name    varchar(22),
location  varchar(50),
constraint sth unique(name)

);

0x06 使用默认约束

1 定义数据表tb_emp7,指定员工的部门编号默认为1111,sql语句如下:
create table tb_emp7 
(
id      int(11) primary key,
name   varchar(25) not null,
deptid  int(11) default 1111, 
salary  float

);

0x07 表的属性值自动增加

1 定义数据表tb_emp8,指定员工的编号自动递增,sql语句如下:
create table tb_emp8 
(
id      int(11) primary key auto_increment,
name   varchar(25) not null,
deptid  int(11), 
salary  float
);

2  执行如下插入语句:
insert into tb_emp8 (name,salary) values('lucy',1000), ('lura',1200),('kevin',1500);

语句执行完后,tb_emp8表中增加3条记录,在这里并没有输入id的值,但系统已经自动添加该值。

使用select命令查看记录:select * from tb_emp8;

0x08 desc 查看表结构

1 使用describe和desc查看表结构
查看tb_dept1表结构,sql语句:describe tb_dept1;
查看tb_emp1表结构,sql语句:desc tb_emp1;

2 show 查看表结构

使用show create table查看表tb_emp1的详细信息,sql语句:show create table tb_emp1;

使用参数’\g’ 来查看表结构: show create table tb_emp1 \g;

0x09 重命名表的名称

使用alter table将表tb_dept3改名为tb_deptment3,sql语句:alter table tb_dept3 rename tb_deptment3;

0x10 修改字段类型

将数据表tb_dept1中name字段的数据类型由varchar(22)修改成varchar(30)。
1 执行修改表名操作之前,使用desc查看tb_dept表结构。

2 输入如下sql语句并执行:alter table tb_dept1 modify name varchar(30);

0x10 修改字段名称

1 将数据表tb_dept1中的location字段名称改为loc,数据类型保持不变,sql语句如下:
alter table tb_dept1 change location loc varchar(50);
2  将数据表tb_dept1中的loc字段名称改为location,同时将数据类型变为varchar(60),sql语句如下:
alter table tb_dept1 change loc location varchar(60);

0x11 添加删除修改字段

1 在数据表tb_dept1中添加一个没有完整性约束的int类型的字段managerid(部门经理编号),sql语句如下:
alter table tb_dept1 add managerid int(10);
2 在数据表tb_dept1中添加一个不能为空的varchar(12)类型的字段column1,sql语句如下:
alter table tb_dept1 add column1 varchar(12) not null;# 插入在该表中的最后一列
3  在数据表tb_dept1中添加一个int类型的字段column2,sql语句如下:
alter table tb_dept 1 add column2 int(11) first; # 插入在该表中的首列
3  在数据表tb_dept1中name列后添加一个int类型的字段column3,sql语句如下:
alter table tb_dept1 add column3 int(11) after name;# 插入在该表中的字段name 后门

4 删除数据表tb_dept1表中的column2字段。

删除column2字段,sql语句如下:
alter table tb_dept1 drop column2;
5 将数据表tb_dept中的column1字段修改为表的第一个字段,sql语句如下:
alter table tb_dept1 modify column1 varchar(12) first;
6 将数据表tb_dept1中的column1字段插入到location字段后面,sql语句如下:
alter table tb_dept1 modify column1 varchar(12) after location;

0x12 修改表的存储引擎

1 将数据表tb_deptment3的存储引擎修改为myisam。
先使用show create table查看表tb_deptment3当前的存储引擎,输入如下sql语句并执行:show create table tb_deptment3 \g
接下来修改存储引擎类型,输入如下sql语句并执行:alter table tb_deptment3 engine=myisam;

最后使用show create table,输入如下sql语句并执行:show create table tb_deptment3 \g;

我们可以查看表tb_deptment3的存储引擎,发现表tb_dept的存储引擎变成了“myisam”

0x013 删除外键约束

1 删除数据表tb_emp9中的外键约束
首先创建表tb_emp9,创建外键deptid关联tb_dept1表的主键id,sql语句如下:
create table tb_emp9 
(
id      int(11) primary key,
name   varchar(25),
deptid  int(11),
salary   float,
constraint fk_emp_dept  foreign key (deptid) references tb_dept1(id)
);
使用show create table查看表tb_emp9的结构:show create table tb_emp9 \g
删除表tb_emp的外键约束,输入并执行:alter table tb_emp9 drop foreign key fk emp_dept;
使用show create table再次查看表tb_emp9结构:show create table tb_emp9 \g

0x14 删除已存在的表

删除数据表tb_dept2,sql语句如下:drop table if exists tb_dept2;

0x15 删除关联表

1 在数据库中创建两个关联表,首先,创建表tb_dept2,sql语句如下:
create table tb_dept2 
(
id       int(11) primary key,
name    varchar(22),
location  varchar(50)
);
2 接下来创建表tb_emp,sql语句如下:
create table tb_emp 
(
id       int(11) primary key,
name    varchar(25),
deptid   int(11), 
salary   float,
constraint fk_emp_dept  foreign key (deptid) references tb_dept2(id)
);
3 使用show create table命令查看表tb_emp的外键约束,结果如下:show create table tb_emp\g

可以看到,以上执行结果创建了两个关联表tb_dept2和表tb_emp,其中tb_emp表为子表,具有名称为fk_emp_dept的外键约束,tb_dept2为父表,其主键id被子表tb_emp所关联。
3 如前所述,在存在外键约束时,主表不能被直接删除。
4 接下来,解除关联子表tb_emp的外键约束,sql语句:alter table tb_emp drop foreign key fk_emp_dept;
5 将取消表tb_emp和表tb_dept2之间的关联关系,此时,可以输入删除语句,将原来的父表tb_dept2删除,sql语句如下:
drop table tb_dept2;

最后通过show tables;查看数据表列表,如下所示:show tables;


欢迎大家分享更好的思路,热切期待^^_^^ !

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值