主要通过6张表的建立来实现数据库操作,数据库设计是不合理的,主要是探索基本语法使用和实现。
t_manager :经理表,主要负责一个商店,销售人员
t_shop : 商店
t_cash_housewoker : 销售人员表
t_director :仓库主任,主要负责一个仓库,和业务人员
t_warehouse :仓库信息表
t_account :业务人员信息表
数据库创建:
create database test
商店信息表:
create database test use test; create table t_shop ( s_id int identity(1,1) primary key, --商店编号 s_name varchar(20), --商店名称 s_tel varchar(11), --商店电话 s_address text --地址 );
经理信息表:
create table manage ( m_id int primary key identity(1,1), --identity(1,1) 低版本sql server 的自动增长 m_name varchar(20), m_password varchar(20), m_sex varchar(2) check((m_sex='男') or (m_sex='女')), m_bir datetime, m_tel varchar(11), s_id int , foreign key(s_id) references t_shop(s_id) );
销售人员表:
create table t_cash_housewoker ( c_id int primary key identity(1,1), c_name varchar(20) , c_password varchar(20), c_sex varchar(2)check((c_sex='男') or (c_sex='女')), c_bir datetime, c_tel varchar(11), m_id int, foreign key(m_id) references manage(m_id) )
仓库表:
create table t_warehouse ( w_id int identity(1,1), w_name varchar(20), w_address text, primary key(w_id) )
定义仓库主任表
create table t_director ( d_id int identity(1,1) primary key, d_name varchar(10), d_password varchar(20), d_sex varchar(2) check((d_sex='男') or (d_sex='女')), d_bir datetime , d_tel varchar(11), w_id int, foreign key(w_id) references t_warehouse(w_id) on delete cascade on update cascade --定义级联删除和级联修改 )
业务人员信息表:
create table t_account ( a_id int identity(1,1) primary key, a_name varchar(10), a_password varchar(20), a_sex varchar(2) check((a_sex='男') or (a_sex='女')), a_bir datetime, a_tel varchar(11), d_id int , foreign key(d_id) references t_director(d_id) )
插入语句的实现:
商店
insert into t_shop values('烩面馆','11111111112','新乡') insert into t_shop values('酒店','11111111112','河南科技学院')
经理:
insert into manage values('buyingfei','11111111112','男','2010-10-10','11111111112',1) insert into manage values('buyafei','11111111112','男','2010-10-10','11111111112',2)
销售人员:
insert into t_cash_housewoker values('销售1','11111111112','男','2010-10-10','11111111112',1) insert into t_cash_housewoker values('销售2','11111111112','男','2010-10-10','11111111112',1)
仓库:
insert into t_warehouse values('仓库1','河南科技学院') insert into t_warehouse values('仓库2','河南')
仓库主任:
insert into t_director values('仓库主任1','11111111112','男','2010-10-10','11111111112',1) insert into t_director values('仓库主任2','11111111112','男','2010-10-10','11111111112',2)
业务员:
insert into t_account values('业务员1','11111111112','男','2010-10-10','11111111112',1) insert into t_account values('业务员2','11111111112','男','2010-10-10','11111111112',1)
查询:
查询m_id负责的商店和销售人员
select s_name 商店名字,s_address 商店地址,c_name 销售人员 from t_shop,t_cash_housewoker where s_id in(select s_id from manage where m_id=1) and t_cash_housewoker.m_id=1
修改:
修改m_id=1经理的名字
update manage set m_name='步迎飞' where m_id='1'
删除:
delete from t_director where d_id in(3) --因为定义了级联删除,会直接删除仓库表中相关数据