系统掌握SQL Server增删改查

-- 创建数据库
create database test;

-- 创建表
create table 职工表  
(  
职工编号 int identity (1,1) primary key,  
职工号 varchar(50) unique,  
仓库号 varchar(50),  
工资 int check('基本工资'>=800 and '基本工资'<=2100),  
)  
create table 订单表  
(  
订单编号 int identity(1,1) primary key,  
订单号 varchar(50) unique,  
职工号 varchar(50) references 职工表(职工号),--references两张表通过“职工号”关联--  
订购日期 datetime,  
销售金额 int  
)  
create table 阳光工资表  
(  
职工编号 int identity (1,1) primary key,  
职工号 varchar(50) unique,  
仓库号 varchar(50),  
基本工资 int check(基本工资>=800 and 基本工资<=2100),  
加班工资 int,  
奖金 int,  
扣率 int,  
应发工资 as (基本工资+加班工资+奖金-扣率) --as为自动计算字段,不能输入值--  
)

-- 插入数据
insert into [test].[dbo].[阳光工资表](职工号,仓库号,基本工资,加班工资,奖金,扣率) 
values
(101, 6001, 2000,100,200,500),
(102, 6002, 2000,100,200,100),
(103, 6003, 2100,100,200,100),
(104, 6004, 1000,300,200,200),
(105, 6005, 1200,200,200,100),
(106, 6006, 1500,300,300,200),
(107, 6007, 1800,400,100,100);

-- 使用UPDATE语句修改数据
USE [test]
update [test].[dbo].[阳光工资表] set [扣率]=0,[奖金]=500 where [职工编号]=1;

-- 使用ALTER TABLE语句修改表结构
--向数据库中的某一表中添加Sex字段
USE [test]
ALTER TABLE [test].[dbo].[阳光工资表]
ADD  Sex char(2)
--删除数据库中的某一表中的Sex字段
USE [test]
ALTER TABLE [test].[dbo].[阳光工资表]
DROP COLUMN Sex

-- 删除数据
delete from [test].[dbo].[阳光工资表] where [职工编号]=3;

-- 删除全部数据
delete from [test].[dbo].[阳光工资表] 
truncate table [test].[dbo].[阳光工资表] 

-- 删除表
drop table [test].[dbo].[阳光工资表] 
drop table [test].[dbo].[订单表]
drop table [test].[dbo].[职工表]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值