当前位置: 首页 > news >正文

条件查询-2

use DBTESTS
if exists(select * from sys.objects where name = 'Department' and type = 'U')
drop table Department
--建表
create table Department
(

DepartmentId int primary key identity(1,1),
DepartmentName nvarchar(50) not null,
DepartmentRemark text

)
--char 无论存储是否10个字节 仍然占用10个字节
--varchar 使用多少就占用多少个字节
--text长文本

create table [Rank]
(

RankId int primary key identity(1,1),
RankName nvarchar(50) not null,
RankRemark text

)

create table People
(

PeopleId int primary key identity(1,1),
DepartmentId int references Department(DepartmentId) not null,--部门 引用外键
RankId int  references [Rank] (RankId) not null,--职级 引用外键
PeopleName nvarchar(50) not null,
PeopleSex nvarchar(1) default('男')check(PeopleSex='男' or PeopleSex='女')not null,
PeopleBirth smalldatetime not null,
PeopleSalary decimal(12,2) check(PeopleSalary>=1000 and PeopleSalary<=1000000) not null,
PeoplePhone varchar(20) unique not null,
PeopleAddress varchar(300),
PeopleAddTime smalldatetime default(getdate()) --添加时间 只需要记录到年月日时分,不需要秒、毫秒

)

--修改表结构
--添加列
--alter table 表名 add 新列名 数据类型
alter table People add PeopleMail varchar(200)

--删除列
--alter table 表名 drop column 列名
alter table People drop column PeopleMail

--修改列
--alter table 表名 alter column 列名 数据类型
--修改地址varchar(300)为varchar(200)
alter table People alter column PeopleAddress varchar(200)

--删除约束 删除 添加
--alter table 表名 drop constraint 约束名
--删除月薪的约束
alter table People drop constraint CK__People__PeopleSa__403A8C7D

--添加约束
--alter table 表名 add constraint 约束名
alter table People add constraint CK__People__PeopleSa1
check(PeoPleSalary>=1000 and PeoPleSalary<=1000000)

--添加约束 主键
alter table 表名 add constraint 约束名 primary key(列名)
--添加约束 唯一
alter table 表名 add constraint 约束名 unique(列名)
--添加约束 默认值
alter table 表名 add constraint 约束名 default 默认值 for 列名
--添加约束(外键)
alter table 表名 add constraint 约束名 foreign key(列名)
references 关联表名(列名(主键))

--查询出80厚的员工信息
select* from People where PeopleBirth>='1980-1-1'
and PeopleBirth<='1989-12-31'
select* from People where PeopleBirth between '1980-1-1'and'1989-12-31'
select* from People where year(PeopleBirth)between 1980 and 1989
--查询30-40岁之间 并且工资15000-30000之间的员工信息
--假设 年龄=当前年份-生日年份
select* from People where
(year(getdate())-year(PeopleBirth)>=30 and year(getdate())-year(PeopleBirth)<=40)
and
(PeopleSalary>=15000 and PeopleSalary<=30000)

select* from People where
(year(getdate())-year(PeopleBirth) between 30 and 40)
and
(PeopleSalary between 15000 and 30000)

--查询出星座是巨蟹座的员工信息(6.22-7.22)
select * from People where
(month(PeopleBirth)=6 and day(PeopleBirth)>=22)
or
(month(PeopleBirth)=7 and day(PeopleBirth)<=22)

--查询出工资比赵云高的人的信息
select * from People where PeopleSalary>
(select PeopleSalary from People where PeopleName = '赵云')

--查询出和赵云在一个城市的人的信息
select * from People where PeopleAddress=
(select PeopleAddress from People where PeopleName = '赵云')

--查询所有员工信息 添加一列 显示生肖
select*,
case
when year(PeopleBirth) % 12 = 4 then '鼠'
when year(PeopleBirth) % 12 = 5 then '牛'
when year(PeopleBirth) % 12 = 6 then '虎'
when year(PeopleBirth) % 12 = 4 then '兔'
when year(PeopleBirth) % 12 = 5 then '龙'
when year(PeopleBirth) % 12 = 6 then '蛇'
when year(PeopleBirth) % 12 = 4 then '马'
when year(PeopleBirth) % 12 = 5 then '羊'
when year(PeopleBirth) % 12 = 6 then '猴'
when year(PeopleBirth) % 12 = 4 then '鸡'
when year(PeopleBirth) % 12 = 5 then '狗'
when year(PeopleBirth) % 12 = 6 then '鼠'
end 生肖
from People

http://www.jsqmd.com/news/824152/

相关文章:

  • 终极二维码修复指南:如何用QrazyBox轻松恢复损坏的QR码数据
  • 联盟营销管理系统有哪些?如何选择?
  • Grid++Report设计器避坑指南:搞不定自动换行和字体缩小?看这篇就够了
  • WPF文本框进阶:打造优雅输入提示的三种实现策略
  • 告别臃肿!Dell G15散热控制开源替代方案全解析
  • 开源BaaS平台Nhost实战:基于PostgreSQL与GraphQL的Firebase替代方案
  • 从0到99.2%准确率:DeepSeek MATH竞赛测试通关路径图(含3个被忽略的归一化预处理陷阱)
  • QKeyMapper:Windows平台全能按键映射神器,游戏办公两不误
  • Qt网络调试助手实战指南:TCP/UDP调试与文件传输解决方案
  • 程序员该不该先去猪场接触业务
  • 基于模板匹配的自动化脚本开发:从原理到实战
  • AI编程技能库:用Scribe构建可复用的智能开发工作流
  • 3PEAK思瑞浦 TPA1811-SO1R SOP8 运算放大器
  • 为内部知识库问答系统集成Taotoken的多模型聚合能力
  • Obsidian Importer终极指南:如何一键迁移你的全部笔记到Obsidian知识库
  • 收藏!小白程序员必备:AI大模型时代,如何实现薪资翻倍?
  • 基于MicroPython的嵌入式射击计时器开发实战:从状态机到人机交互
  • CSS+JS实现鼠标跟随粒子爆炸特效:原理、集成与性能优化
  • AM243x多核MCU启动流程解析与OSPI Flash烧录实战
  • 从单仓到多租户GitOps:DeepSeek支撑200+业务线的分层仓库架构(含Git Submodule+OCI Registry双模设计图)
  • 2026年4月服务好的涂胶机公司推荐,单双向预浸机设备/碳纤维预浸料设备/碳纤维预浸料/涂膜机/涂胶机,涂胶机厂商推荐 - 品牌推荐师
  • PNG转Windows鼠标指针:开源工具png-to-cursor全解析
  • 生态系统碳循环模型CENTURY建模方法应用——以柠条灌木林生产力模拟为例
  • 嵌入式Python库CI/CD实战:Travis CI自动化测试与发布
  • 第12章:C++ 编译链接原理
  • AI时代文科复兴论:社会约束的客观性与认知训练的偏移
  • 2026年塑胶品牌曝光渠道哪些值得推荐怎么判断:江外江适用场景与选型对比清单 - 广州矩阵架构科技公司
  • OpenClaw-NVIDIA-NIM-API:简化大模型推理部署的中间层实践
  • Pro Workflow:基于SQLite持久化记忆的AI编程助手智能协作系统
  • 贵州异形沙发定制技术解析与合格厂家参考 - 奔跑123