数据库的增删改查的一个例题
生活随笔
收集整理的這篇文章主要介紹了
数据库的增删改查的一个例题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:(下面的照片)
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總結
以上是生活随笔為你收集整理的数据库的增删改查的一个例题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: R语言Excel的读写
- 下一篇: SQL Server报错:选择列表中的列