第九周周记

数据库的创建

在这里插入图片描述

表结构的创建

--切换数据库
if exists (select *from sys.objects where name='Department'and type='U')--U表示是用户自己创建的
drop table Department
use DB
--创建表的基本语法
create table Department
(
	--部门编号
	DepartmentId int primary key identity(1,1),--自动增长,初始值为一,增长步长为1
	DepartmentName nvarchar(50)not null,
	DepartmentRemark text
)
--char :定长,char(10)里面可以存储10个字节,无论存储的数据是否到了10个字节,都要占用10个字节
--varchar :变长,最多占用10个字节
--text:长文本
--char varchar text 前面加n存储Unicode字符,对中文友好
--varchar(100):存储100个字母或者50个汉字
--nvarchar(100)存储100个字母或者100个汉子
create table [Rank]
(
	--职级编号
	RankId int primary key identity(1,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,--部门 references引用外键,将外界的主键引用到内部的位置,
	RankId int references [Rank](RankId)not null ,--职级
	PeopleName nvarchar(50)not null,
	PeopleSex nvarchar(1)check(PeopleSex='男'or PeopleSex='女')not null,
	--default表示的是默认的类型
	PeopleBirth datetime  not null,--date年月日,datetime 精确到时分秒 smalldatatime比datetime范围要小,smallint也比int的范围要小
	PeopleSalary decimal(12,2)check (PeopleSalary>=1000 and PeopleSalary<100000)not null,--12代表的是12位2
	PeoplePhone varchar(20)unique not null,--unique表示的是唯一的约束
	PeopleAddress varchar(300),
	PeopleAddTime smalldatetime default(getdate()) --添加时间默认设置的是系统当前的时间
)

表结构的修改

--修改表结构
--添加列
--alter table 表名 add 新列名 数据类型
--给员工表添加邮箱
alter table People add PeopleMail varchar(200)--alter 表示的是添加

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

--修改列
select * from People--表示查询这个表
select * from Department
--修改
--语法:
--update 表名 set 字段1=值1,字段2=值2 where 条件
--工资调整
update People set PeopleSalary=PeopleSalary+1000
--将员工编号为5的加薪500
update People set PeopleSalary=PeopleSalary+500
where PeopleId=5
--将软件部加薪
update People set PeopleSalary=15000
where DepartmentId=2 and PeopleSalary<14999

select * from People
select * from Department
--修改
--语法:
--update 表名 set 字段1=值1,字段2=值2 where 条件
--工资调整
update People set PeopleSalary=PeopleSalary+1000
--将员工编号为5的加薪500
update People set PeopleSalary=PeopleSalary+500
where PeopleId=5
--将软件部加薪
update People set PeopleSalary=15000
where DepartmentId=2 and PeopleSalary<14999

--删除
--语法
--delete from 表名 where 条件
--删除员工表的所有记录
delete from People
--删除软件部(部门编号2)
delete from People where DepartmentId =2 and PeopleSalary>100
--关于删除(drop,truncate,delete)
--drop table People--删除表对象
--truncate table People 删除数据(清空数据),表对象即表结构,依然存在
--delete from People --删除所有的数据,表对象即表结构依然存在
--truncate,delete 区别
--truncate清空所有的数据,不能有条件,delete 可以删除所有数据也可以带条件,删除符合条件的数据
--自动编号
--假设表中数据自动编号12345
--truncate 标号还在
--delete 编号将永远不存在 
select *from Department
select *from [Rank]
select * from  People
--查询指定列
--select ......from.......
--select  显示中文列名
select distinct (...)from 表名  不需要重复数据显示
--查询性别为女的员工信息
select * from People where PeopleSalary>=10000
--多条件and 连接
--或者or连接
--查询月薪在10000-20000之间的员工信息(多条件)
select *from People where PeopleSalary >=10000 and PeopleSalary<=20000
select *from People where PeopleSalary between 10000and 20000--两者之间between and
--查询出地址在武汉或在北京的员工信息
select *from People where PeopleAddress in(条件1,条件2)--和or 的意思基本一样
--排序
--查询所有的员工信息,根据工资降序(desc)
--asc 升序(默认值)
select *from People order by PeopleSalary desc
--查询所有员工信息,根据名字长度排序
select *from People order by  len(PeopleName)desc
--查询出工资最高的5人的工资
select top 5* from People order by PeopleSalary desc
--查询出工资最高的10%的员工信息
select top 10 percent *from  People order by PeopleSalary desc
--null:空值
insert into People (DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth,PeopleSalary,PeoplePhone,PeopleAddTime)
values(1,1,'马云','男','1977-7-7',5000,'138585858',GETDATE())
--查询出地址没有填写的员工信息
select *from People where PeopleAddress is null
--查询出地址填写的员工信息
select *from People where PeopleAddress is not null
--80后的员工信息
select *from People where (PeopleBirth)  between '1980-1-1'  and'1989-12-31'
--查询30-40岁并且工资在1500-300000之间的员工信息
--假设年龄=当前年份-生日年份
select *from People where 
(year(GETDATE())-year(PeopleBirth)>=30 and year (getdate())-year (PeopleBirth) <=40)
and (PeopleSalary>=15000 and PeopleSalary<=30000)
--(PeopleSalary between 15000and 30000)
--查询出星座是巨蟹的员工信息month()
select *from People where ()


select *from People where PeopleAddress=(select PeopleAddress from People where PeopleName='赵云')
--查询生肖是鼠的人员信息
==
select*from People where year(PeopleBirth)%12=4
select*
case
when year(PeopleBirth)%12=4 then'鼠'
 else''
end 生肖
from People 
--查询出名字中含尚或
select*from People where PeopleName like '_像'
select *from People where PeoplePhone like'138%'
--查询出电话号码开头为138的,第四=位好像是7或者8,最后一个号码是5
select *from People where PeoplePhone like '138[7,8]%5'
--查询出电话号码开头为138的,第四位好像是2-5之间,最后一个号码不是2和3
select *from People where PeoplePhone like '138[7,8]%[^2,3]'

约束的修改和维护

use abc;
--alter table People drop column PeopleMail;
--alter table People add PeopleMail varchar(200)
--修改列
--alter table 表名 alter column 列名 数据类型
--alter table People alter column PeopleAddress varchar(200)
--维护约束(删除添加)
--删除约束
--alter table 表名 drop constraint 约束名
--删除月薪的约束
--alter table People drop constraint CK_People_PeopleSal
--添加约束 check
--alter  table 表名 add constraint 约束名 check(表达式)
--alter table People add constraint CK_People_PeopleSal check(PeopleSalary>=3000and PeopleSalary<=100000)
--添加约束(主键约束)
--alter table 表名 add constraint 约束名 primary key (列名)
--添加约束唯一
--alter  table 表名 add constraint 约束名 unique(列名)
--添加约束默认值
--alter table 表名 add constraint 约束名 default 默认值 for 列名
-- 添加约束外键
--alter table 表名 add constraint 约束名 foreign key (列名) references 关联表名(列名(主键))
--向部门表种插入数据
insert into Department(DepartmentName,DepartmentRemark)values('市场部','。。。。。')
insert into Department(DepartmentName,DepartmentRemark)values('软件部','。。。。。')
insert into Department(DepartmentName,DepartmentRemark)values('企划部','。。。。。')
--简写
insert into Department values('硬件部','.......')
insert into Department values ('软件部','......')
--一次性插入多行数据
insert into Department(DepartmentName,DepartmentRemark)
select '测试部','。。。。。'union
select '实施部','。。。。。'union
select '产品部','..........'
--向职级表中插入数据
insert into [Rank]
(RankName,RankRemark)
values('初级','.....')
insert into [Rank]
(RankName,RankRemark)
values('中级','。。。')
insert into [Rank]
(RankName,RankRemark)
values('高级',',,,,,,')





--向员工表中插入数据
insert into People (DepartmentId,RankId,PeopleName
,PeopleSex,PeopleBirth,PeopleSalary,PeoplePhone,PeopleAddress)
values(2,1,'刘备','男','1988-8-8',10000,'13888888888','中国')

JDBC连接数据库

常用的接口和类
driver接口它定义的是与数据库建立连接的方法,所有java语言连接的数据库都会实现该接口
DriverManager 类
数据库的管理驱动类,这个类的在于实现注册驱动,创建java代码与数据库的连接
加载数据库驱动时通常使用Class类的静态方法forname()来实现
Connection 类
Connection接口表示的是java程序和数据库的连接对象,连接数据库后才能实现一系列操作
Statement 接口
java执行静态SQL的语句
PreparedStatement 接口是Statement接口的子接口用于执行预编译的SQL语句
resultSet接口
ResultSet接口对象有一个指向结果集数据行的指针
常使用next()方法作为while循环的条件来迭代ResultSet结果集

JDBC编程

使用JDBC操作数据库的步骤:注册加载驱动,获取数据库连接,创建语句对象,执行语句,关闭资源
1.注册加载驱动Class.forName
会抛出异常
2.获取数据库连接
Connection con=Divermanager.getConnection(url,use,password)
3.创建语句对象
PreparedStatement pst=con.prepareStatement(SQL语句)
4.执行语句
PreparedStatement
executeQuery
executeUpdate
5.关闭资源
放在try-catch 的finally

第九周总结

今周的主要学习目标是学习SQL语句,和连接数据库的一系列的相关内容。我的感觉在连接数据库的过程中,重复的代码很多,思路上就是用SQL语句对象实现动态静态的一系列操作,重要的是注册加载驱动时花费了很长时间,但是通过询问和网络查询,终于在今天(周四)完成了对数据库的增删查改,但是SQL语言还没学完,我就利用剩余的时间进行了SQL语言的进一步学习,(结合老师推荐的书和视频)对数据库的了解还应该多一点

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值