数据库基础笔记1

来源:互联网 发布:大智慧下载数据在哪里 编辑:程序博客网 时间:2024/05/17 07:39
登录服务器


电脑名\服务实例名




如果是本机服务器:
.\服务实例名


cmd
sqlcmd -S.\sqlexpress






sql语句  structured query language


切换数据库用use+数据库名


use School


go
--go表示将前面的SQL打一个包,一起执行
--go不属于T-SQL,它不是SQL语句的一部分
--go属于sqlcmd控制台命令






creat database +新数据库名


creat database +新数据库名
on(
name='数据库逻辑名'
filename='文件名.mdf'
size=大小,    单位可以使M
filegrowth=增长     M或%为单位
)




log on
(


)




判断数据库是否存在


if(DB_ID('School')) is not null










创建表
create table
(
字段名 类型名,
字段名 类型名
)




删除表
drop table


表是否存在


if(object_id('表名','u')) is null   u表示用户自定义表
creat table 表名
(
id int
)


增、删、改、查


inset into 表名(字段名) values(值)




删除数据
delete from 表名     --删除表中所有数据






修改数据
update 表名 set 字段名=值  --修改表中所有数据




查询数据


select * from 表名



数据库文件


1.数据文件.mdf    master database file
2.日志文件.ldf    log database file
3.非主数据文件.ndf     not master database file


实例就是处理数据等操作的一个执行者


架构相当于命名空间
架构的作用常常用来做一些权限、安全限制


表对应一个类(ORM)
每一行数据就对应一个完整对象。(记录、行、元祖)
一张表的所有数据构成一个集合


如果自定义的名字与关键字冲突,或中间需要有空格等
则使用方括号将其括起来

create table [int]
(
id int
)




为了保证数据的完整性,添加约束




删除数据
drop table 表名
delete from b表名
truncate table 表名(将清除所有的数据,包括所有动作不记入日志)




获取字符串长度函数len()
获取当前时间getdate()




添加约束
主键约束、唯一约束、检查约束、默认约束


1.创建表时创建约束
create table tblCreateConstraint
(
id int primary key,
name nvarchar(10) unique,
age int check(age>0 and age<100),
joindate datetime default(getdate())
)


2.修改表时添加约束
凡是修改表语法为alter able 表名 动作


增加检查约束
alter table XXX add constraint
CK_XXX_age check(age>0 and age<100)


删除
alter table XXX drop constraint CK_XXX_age




添加主键约束
alter table XXX add constraint PK_XXX_Id
primary key(Id)


唯一约束
alter table XXX add constraint UQ_XXX_name
unique(name)


默认约束
alter table XXX add constraint DF_XXX_joindate
default(getdate()) for joindate




查看约束 select * from sys.objects   --(查看所有对象)










外键约束


创建表的时候,创建约束


create table MainTable
(
Fid int primary key,
name nvarchar(10)
)


create table ForTable
(
id int primary key,
Fid int foreign key references MainTable(Fid) 
)


创建表后 修改添加外键约束








alter table ForTable1
add constraint
FK_ForTable1_MainTable1_Fid
foreign key(Fid) references MainTable1(Fid)
  先外键表的Fid              再主键表的Fid
  
  
  添加自动增长标识符
  create table XXX
 (
 id int identity(1,1) primary key,
 name nvarchar(10)
 )






 select * from 表名 where 条件
 
 正常情况:不要用*
 select 
 各个字段
 from 表名 where


 
 添加列的别名
 1.列 as 别名
 select
 id as 编号
 from T_student
 
 2.列 别名     建议不用
 3.别名=列
  select
 编号=id
 from T_student
 


print也可以显示数据


select ‘我是select’  消息显示为表
print ‘我是print’    消息直接显示




添加一个年龄排序
使用order by 字段名
select *from T_student order by age  (默认升序)


再后面加asc和desc,分别代表升序和降序
select *from T_student order by age desc


取前面3个数据
select top(3) from T_student order by age desc


按百分比取
select top(3)percent from T_student order by age desc


select top(3)percent * from T_student order by age desc




去除重复的数据(注意:数据一整行表示完整的数据,只有当这条完整的数据完全相同时,才去除)
select distinct * from T_student




聚合函数
求和sum、求平均avg、求最大小max min、求总数count


求总数:
select count(name) from 表名 where name='张三'


求最大:
select max(age) from 表名


group by 中有的字段,才能出现在select语句中
group by 对数据进行分组,但是应注意分组的数据才允许select检索
group by 应写在from后面,但要写在order by前面


聚合函数的一个注意事项


在处理聚合数据的时候,不计入空值数据




SQL Server 中null表示未知




使用between and 表示条件范围
字段between 数值1 and 数值2
年龄在18到23岁之间的所有员工
select * from 表名 where age between 18 and 23


求出年龄在17岁到18岁  和20岁的人
字段 in(数值)
select * from 表名 where age in(17,18,21)




模糊查询
_  单个字符
%   零个或多个字符
寻找XX虎
select * from 表名 where name like '__虎';


如果要找_log.ldf结尾   可以这样:like '%[_]log.ldf'
在方括号中[^]也表示否定,就是表示不出现这个元素的


name like  '[^赵]%'
name not like '赵%'




在SQL中,使用三值逻辑(true,false,unknow)
>,=,<,>=,<=,<>(!=),(!>,!<)


由于null的存在


函数isnull(字段,替换值) 是null则用替换值替换,否则什么也不做


select * from 表名 where math is null;
判断null 用 is 或 is not




datalength() 计算字节
len() 计算字符




聚合函数不能放在where后
where 是查询以前进行筛选
having 是查询以后进行筛选


where  ……  group by
group by ……having 




having 语句不能包含未分组的列名






原创粉丝点击