系统掌握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].[职工表]

猜你喜欢

转载自blog.csdn.net/craftsman2020/article/details/107289222