sql建表语句

版权声明: https://blog.csdn.net/eds124/article/details/85263110
use master
go
drop database mydata
go
create database mydata2
go
use mydata
go
create table t_user
(
	userguid uniqueidentifier not null default newid(),
	username nvarchar(max) not null default N'',
	userpassword nvarchar(max) not null default N'',
	createdatetime smalldatetime not null default getdate(),
	lastalterdatetime smalldatetime not null default getdate(),
	a1 bit not null default 0,
	a2 bit not null default 0,
	a3 bit not null default 0,
	a4 bit not null default 0,
	b1 bit not null default 0, 
	b2 bit not null default 0,
	b3 bit not null default 0,
	b4 bit not null default 0,
	c1 bit not null default 0,
	c2 bit not null default 0,
	c3 bit not null default 0,
	c4 bit not null default 0,
	d1 bit not null default 0,
	d2 bit not null default 0,
	d3 bit not null default 0,
	d4 bit not null default 0,
	primary key (userguid),
)
insert into t_user (username, userpassword) values (N'张三', N'zs123')
insert into t_user (username, userpassword) values (N'里斯', N'as645df')
insert into t_user (username, userpassword) values (N'aaa', N'asdsdf')
insert into t_user (username, userpassword) values (N'a', N'b')
select * from t_user where username = 'AAA'
select * from t_user

create table t_admin
(
	adminguid uniqueidentifier not null default newid(),
	adminname nvarchar(max) not null default N'',
	adminpassword nvarchar(max) not null default N'',
	createdatetime smalldatetime not null default getdate(),
	lastaltertime smalldatetime not null default getdate(),
	primary key (adminguid)
)
insert into t_admin (adminname, adminpassword) values (N'admin', N'admin')
select * from t_admin

drop table t_goods
create table t_goods(
	goodsguid uniqueidentifier not null default newid(),
	goodsname nvarchar(50) not null default N'',
	goodscode nvarchar(50) not null default N'',
	goodsunit nvarchar(10) not null default N'',
	goodscate nvarchar(10) not null default N'',
	goodsprice money not null default 0,
	goodsinserttime datetime not null default getdate(),
	goodsupdatetime datetime not null default getdate(),
	primary key (goodsguid)
)

insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('猪肉', '001', '斤', '肉类', 8.35);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('羊肉', '002', '斤', '肉类', 28.5);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('牛肉', '003', '斤', '肉类', 22);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('驴肉', '004', '斤', '肉类', 32);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('狗肉', '005', '斤', '肉类', 40);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('猫肉', '006', '斤', '肉类', 10);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('兔肉', '007', '斤', '肉类', 15);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('鸡肉', '008', '斤', '肉类', 5);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('鸭肉', '009', '斤', '肉类', 8);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('鹅肉', '010', '斤', '肉类', 10);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('菠菜', '011', '斤', '蔬菜类', 5);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('白菜', '012', '斤', '蔬菜类', 5.5);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('芹菜', '013', '斤', '蔬菜类', 6.5);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('油菜', '014', '斤', '蔬菜类', 0.5);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('韭菜', '015', '斤', '蔬菜类', 9.5);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('包菜', '016', '斤', '蔬菜类', 3.5);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('生菜', '017', '斤', '蔬菜类', 5);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('萝卜', '018', '斤', '蔬菜类', 0.5);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('茭瓜', '019', '斤', '蔬菜类', 1.5);
insert into t_goods (goodsname,goodscode,goodsunit,goodscate,goodsPrice) values ('冬瓜', '020', '斤', '蔬菜类', 2.5);
select * from t_goods where goodsname like '%肉%'

drop table t_shopping
create table t_shopping
(
	shoppingguid uniqueidentifier not null default newid(),
	goodsguid uniqueidentifier not null default newid(),
	goodsname nvarchar(50) not null default N'',
	goodscode nvarchar(50) not null default N'',
	goodsunit nvarchar(10) not null default N'',
	goodscate nvarchar(10) not null default N'',
	goodsprice money not null default 0,
	shoppingshuliang decimal(12,4) not null default 0, 
	goodsinserttime datetime not null default getdate(),
	goodsupdatetime datetime not null default getdate(),
	primary key(shoppingguid)
)
select * from t_shopping

drop table t_jiaohuo
create table t_jiaohuo
(
	jiaohuoguid uniqueidentifier not null default newid(),
	jiaohuoshuliang decimal(12,2) not null default 0,
	jiaohuoinserttime datetime not null default getdate(),
	jiaohuoupdatetime datetime not null default getdate(),
	goodsguid uniqueidentifier not null default newid(),
	primary key (jiaohuoguid),
	FOREIGN KEY (goodsguid) REFERENCES t_goods(goodsguid)
)
select * from  t_jiaohuo

drop view v_goods_jiaohuo;
create view v_goods_jiaohuo as select goodsname 产品名称, t_goods.goodscode 产品代码, goodsunit 计算单位, goodscate 分类, goodsprice 单价, jiaohuoshuliang 叫货数量, goodsprice * jiaohuoshuliang 小计, jiaohuoupdatetime 最后修改时间, jiaohuoguid from t_goods, t_jiaohuo where t_goods.goodsguid=t_jiaohuo.goodsguid
select * from t_goods order by goodsupdatetime desc
select * from v_goods_jiaohuo

select goodsname, t_goods.goodscode, goodsunit, goodscate, goodsprice, jiaohuoshuliang, goodsprice * jiaohuoshuliang total, jiaohuoupdatetime, jiaohuoguid from t_goods, t_jiaohuo where t_goods.goodsguid=t_jiaohuo.goodsguid

select sum(total) from (select goodsname, t_goods.goodscode, goodsunit, goodscate, goodsprice, jiaohuoshuliang, goodsprice * jiaohuoshuliang total, jiaohuoupdatetime, jiaohuoguid from t_goods, t_jiaohuo where t_goods.goodsguid=t_jiaohuo.goodsguid and goodscate = '肉类')t

猜你喜欢

转载自blog.csdn.net/eds124/article/details/85263110