数据库的增删改查的一个例题

题目:(下面的照片)在这里插入图片描述

use T_JiNeng2;

--用地基本信息表
create table T_proj_info(
proj_id int identity(1,1)  not null,
proj_name varchar(50) not  null,
proj_no varchar(5) not null,
proj_type varchar(25) not null,
tilth_state float not null,
proj_kind varchar(25) not null,
farm_tot float not null,
approve_unit varchar(50) not null
)

--创建行政区基本信息表
create table T_canton_info(
canton_id int identity(10,1) not null,
proj_id int not null,
canton_tot float not null,
canton_no varchar(5) not null,
canton_name varchar(50) not null,
branch varchar(50) not null,
remark text not null
)

--设置主键 constraint 约束
alter table T_proj_info 
add constraint PK_T_proj_info_proj_id
primary key (proj_id)

alter table T_canton_info
add constraint PK_T_canton_info_canton_id
primary key (canton_id)

--在被引用表 ‘Student’ 中没有与外键 ‘FK_S’ 中的引用列列表匹配的主键或候选键。问题原因:
--可能的原因之一是主键前后定义的不一致。
--或者主表没有主键 需要添加或者修改主键
--设置外键 foreign:外国的--外键  references:参照
--怎么定义外键 一定是主表中的主键作为外键
alter table T_canton_info
add constraint FK_T_canton_info_T_proj_info_proj_id
foreign key (proj_id) references T_proj_info(proj_id)

--插入数据
insert into T_proj_info values('灿氏集团','1','建设用地',999999,'商用',1000,'中共中央人民代表大会')
insert into T_proj_info values('灿氏集团2','1','娱乐用地',999999,'娱乐',1001,'中共中央人民代表大会')
insert into T_proj_info values('灿氏集团3','1','建设用地',999999,'商用',1003,'中共中央人民代表大会')
insert into T_proj_info values('灿氏集团4','1','建设用地',999999,'商用',13001,'中共中央人民代表大会')
insert into T_proj_info values('灿氏集团4','2','建设用地',1200,'商用',13001,'中共中央人民代表大会')

insert into T_canton_info values(16,142,'83100','星沙','长沙市国土资源局','我家的')
insert into T_canton_info values(17,142,'83101','星沙','长沙市国土资源局','我家的')
insert into T_canton_info values(18,143,'83102','星沙2','长沙市国土资源局','我家的')

select * from T_proj_info;
select * from T_canton_info;

--•查询出项目编号为“1”的建设用地基本信息;
select * from T_proj_info where proj_no =1 and proj_type ='建设用地'

--•查询出行政直属部门为“长沙市国土资源局”的建设用地基本信息;
select * from T_canton_info where branch='长沙市国土资源局'

--•查询出所有的建设土地基本信息并按农用地总面积升序排序;asc 升序 desc降序
select * from T_proj_info where proj_type ='建设用地' order by farm_tot asc

--•删除耕地面积大于“1300”的建设用地基本信息;
delete  from T_proj_info where tilth_state<=1300

--•请把直属部门由“长沙市国土资源局”修改为“株洲市国土资源局”;
update T_canton_info set branch='株洲市国土资源局' where branch='长沙市国土资源局'

--单表视图
--•创建名为ProjInfo_view1的视图,视图的数据为编号是“1”的建设用地基本信息;
create view ProjInfo_view1 
as 
select *from T_proj_info 
where proj_no='1'

--删除上题中所创建的ProjInfo_view1视图
drop view ProjInfo_view1

--查询行政区名称为‘星沙’的用地基本信息
select a.* from T_proj_info a
inner join T_canton_info  b on b.proj_id = a.proj_id
where b.canton_name ='星沙'

--使用内连接连接两张表中有关联的数据  (查询所有两个表中有关联的数据)
select * from T_proj_info a
inner join T_canton_info  b on b.proj_id = a.proj_id

--使用内连接查询两张表中行政区为星沙的数据。
select * from T_proj_info a
inner join T_canton_info  b on b.proj_id = a.proj_id
where b.canton_name ='星沙'

--left join:左连接
create view Plot_id_view2
as
select  T_plot_info.purpose,T_appr_area.*
from T_plot_info left join T_appr_area on T_plot_info.proj_id = T_appr_area.proj_id

猜你喜欢

转载自blog.csdn.net/qq_45760909/article/details/108582523