今天寫了幾個SQL語句。覺得應該記下來

2021-09-12 04:37:12 字數 1774 閱讀 7695

環境:sql server2005個人版,在最後多表查詢時搞了好久,想想這個東東以後可能有用。先記下!

sql **

create

database test;  

use test;  

drop

table if exists addresses;  

/*位址表*/  

create

table addresses(  

id int

primary

key identity,  

address varchar(20) not

null,/*位址*/  

zipcode int/*郵編*/  

);  

drop

table if exists  family;  

/*家庭表*/  

create

table family(  

id int

unique identity not

null,  

name

varchar(20),   /*家長名*/  

address_id int

notnull, /*位址id*/  

primary

key(id) ,  

constraint family_address foreign

key(address_id) references addresses(id)  

);  

drop

table if exists person;  

/*個人資訊表*/  

create

table person(  

id int

primary

key identity,  

family_id int

notnull, /*家庭id*/  

com_address_id int

notnull,/*公司位址id*/   

name

varchar(20) not

null,/*姓名*/  

*** varchar(5)not

null,/*性別*/  

constraint person_family_address foreign

key(family_id) references family(id),  

constraint person_complay foreign

key(com_address_id) references  addresses(id)  

)  /*檢視個人家庭資訊*/  

select person.name

as '姓名', person.*** as '性別',family.name

as '家長名',  

addresses.address as '家庭住址' ,b.address as '公司位址'  

from person,addresses,family,   

(select addresses.id,addresses.address from person,addresses 

where addresses.id=person.com_address_id)b  

where

addresses.id=family.address_id  and family.id=person.family_id  

and person.com_address_id=b.id;  

自己覺得重要的sql語句

查詢表資料 select from tablename 修改表字段值 update tablename set column newval where columnid id 刪除比表中某一字段值 delete from tablename where columnid id 刪除 表資料和表結構 ...

幾個SQL語句 備忘

1.三漲停 select biao1.biao1.名稱 from biao1,biao2,biao3 where biao1.漲幅 biao2.漲幅 biao3.漲幅 0.27 and biao1.biao2.and biao2.biao3.and not exists select from bi...

幾個經典的sql語句

1.關於group by的sql語句 表結構 sql的寫法 select year,select amount from test as m where month 1 and test.year m.year as m1,select amount from test as m where mon...