建表和添加数据

--切换到master数据库
use master
go
--删除cpms数据库
drop database cpms
go
--创建cpms数据库
create database cpms
on(name=cpms_data1,filename='c:\cpms\cpms_data1.mdf')
log on(name=cpms_log1,filename='c:\cpms\cpms_log1.ldf')
go
--切换到cpms数据库
use cpms
go
--创建ware表
--drop table ware
--go
--drop table worker
--go
create table ware
(ware_id nvarchar(4) not null primary key,--货号
ware_name nvarchar(16), --货名
spec nvarchar(12),--规格
unit nvarchar(2)--单位
)
go
--创建users表
create table users
(username nvarchar(20)not null primary key,--用户名
pwd varchar (20),--密码
usertype nvarchar (5)--用户类型
)
go
--创建worker表
create table worker 
(work_id nvarchar(6) not null primary key,--职工编号
work_name nvarchar(8) not null ,--姓名
sex bit not null,--性别
birth smalldatetime,--出生日期
telephone nvarchar(15) ,--联系电话
address nvarchar(50),--家庭住址
position nvarchar(10)--职位
)
go
--创建supplier表
create table supplier
(sup_name nvarchar(20)not null primary key,--供货商名称
sup_address nvarchar(24) ,--供货商地址
sup_tel nvarchar(8),--供货商联系电话
supplier nvarchar(8)--供货人
 )
go
create table stock
(stock_id int identity(1,1 ) not null primary key,--库存序列号
ware_id nvarchar(4),--货号
buy_num smallint ,--买入数量
sale_num smallint ,--库存数量
)
go
create table sell
(sell_id int identity(1,1) not null, --销售序列号
ware_id nvarchar(4),--货号
sell_price decimal(18,0),--销售单价
sell_date smalldatetime,--销售日期
sell_num smallint ,--销售数量
work_id nvarchar(6),--销售职工编号
)
go
create table restock
(res_id int identity(1,1)not null primary key,--进货序列号
ware_id nvarchar(4), --货号
res_price decimal(18,0),--进货单价
res_number smallint,--进货数量
res_date smalldatetime,--进货日期
work_id nvarchar(6),--进货人
sup_name nvarchar(20),--供货商名称
)
GO
--为users表添加数据
--方法一一次添加一条记录数据
--格式insert into表名(字段名列表)values(值列表)
insert into users(username,pwd,usertype)values('admin','admin','管理员')
insert into users(username,pwd,usertype)values('guest','12345','普通用户')
insert into users values('赵本山','123','普通用户')
--省略字段列表,必须是值列表的个数、顺序、类型和表结构的字段完全一致
--新建一个以你的学号或姓名拼音命名的账号
insert into users(username,pwd,usertype)values('wangmin','54321','普通用户')
--新建一个新账号,账号名为qq,用户类型是管理员,没有密码
insert into users(username,usertype)values('1234567890','管理员')
--已经插入的记录行不能重复插入,否者违反了主键约束
--值列表的顺序必须和字段名列表的顺序一致
--方法二一次添加多条记录数据
select * from users--查询users表的所有数据

 

posted @ 2024-04-09 22:00  困到很想醒  阅读(11)  评论(0编辑  收藏  举报