利用SQL語句查詢(基於MSSQL)

2021-10-22 23:52:23 字數 2771 閱讀 5495

(1)求**工程j1零件的**商號碼sno

(2)求**工程j1零件p1的**商號碼sno

(3)找出所有**商的姓名和所在城市

(4)找出所有零件的名稱、顏色和重量

(5)找出使用**商s1所**零件的工程號碼

create database spj 

on primary

(name=spj_data,

filename='e:\data2\spj_data.mdf',

size=10mb,

filegrowth=10%,

maxsize=unlimited

)log on

(name=spj_log,

filename='e:\data2\spj_log.ldf',

maxsize=10mb,

filegrowth=1mb

)use spj;

create table s(

sno char(3) primary key,

sname char(10),

status char(2),

city char(10));

create table p(

pno char(3),

pname char(10),

color char(4),

weight int,

primary key(pno));

create table j(

jno char(3),

jname char(10),

city char(10),

primary key(jno));

create table spj(

sno char(3),

pno char(3),

jno char(3),

qty int,

primary key(sno,pno,jno),

foreign key(sno) references s(sno),

foreign key(pno) references p(pno),

foreign key(jno) references j(jno)

);insert into s(sno,sname,status,city) values

('s1','精益','20','天津'),

('s2','盛錫','10','北京'),

('s3','東方紅','30','北京'),

('s4','豐泰盛','20','天津'),

('s5','為民','30','上海');

insert into p(pno,pname,color,weight) values

('p1','螺母','紅',12),

('p2','螺栓','綠',17),

('p3','螺絲刀','藍',14),

('p4','螺絲刀','紅',14),

('p5','凸輪','藍',40),

('p6','齒輪','紅',30);

insert into j values

('j1','三建','北京'),

('j2','一汽','長春'),

('j3','彈簧廠','天津'),

('j4','造船廠','天津'),

('j5','機車廠','唐山'),

('j6','無限電廠','常州'),

('j7','半導體廠','南京');

insert into spj values

('s1','p1','j1',200),

('s1','p1','j4',700),

('s1','p2','j2',100),

('s2','p3','j1',400),

('s2','p3','j2',200),

('s2','p3','j4',500),

('s2','p3','j5',400),

('s2','p5','j1',400),

('s3','p1','j1',200),

('s3','p3','j1',200),

('s4','p5','j1',100),

('s4','p6','j3',300),

('s4','p6','j4',200),

('s5','p2','j4',100),

('s5','p3','j1',200),

('s5','p6','j2',200),

('s5','p6','j4',50);

(1)求**工程j1零件的**商號碼sno

select distinct sno from spj where jno='j1';
(2)求**工程j1零件p1的**商號碼sno

select distinct sno from spj where jno='j1' and pno='p1';
(3)找出所有**商的姓名和所在城市

select sname,city from s ;
(4)找出所有零件的名稱、顏色和重量

select pname,color,weight from p;
(5)找出使用**商s1所**零件的工程號碼

select distinct pno from spj where sno='s1';
額 ,先寫這麼多,後面再總結下常見的在mssql上的sql語句查詢,今天不想寫了?

sql 語句 查詢

例11 1 1 use xk gocreate trigger test1 on student for update as print 記錄已修改!go 2 update student set pwd 11111111 where stuno 00000001 例11 3 1 use xk go...

SQL查詢語句

create or replace procedure imms pk reportsend fromdate varchar2,todate varchar2 as function 系統效能統計 author qja 功能 將imms statusreport 表和imms report sen...

sql查詢語句

條件查詢 select 列名列表 from 表名 where 條件 where 條件 用來篩選滿足條件的記錄 行 條件有6種形式 1.比較大小 列名 比較運算子 值 查詢圖書 超過30元的圖書資訊 select from book where price 30 查詢清華大學出版社出版的圖書資訊 se...