1. 程式人生 > >sql建表語句

sql建表語句

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