SqlServer数据库知识点笔记

1.主键约束:
要对一个列加主键约束的话,这列就必须要满足的条件就是非空
因为主键约束:就是对一个列进行了约束,约束为(非空、不重复)
以下是代码   要对一个列加主键,列名为id,表名为emp

格式为:
alter table 表格名称 add constraint 约束名称 增加的约束类型 (列名)

例子:
alter table emp add constraint ppp primary key (id)

2.check约束:
就是给一列的数据进行了限制
比方说,年龄列的数据都要大于20的
表名(emp) 列名(age)

格式:
alter table 表名称 add constraint 约束名称 增加的约束类型 (列名)

例子:
alter table emp add constraint xxx check(age>20)

3.unique约束:
这样的约束就是给列的数据追加的不重复的约束类型

格式:
alter table 表名 add constraint 约束名称 约束类型(列名)
比方说可以给ename列加个unique,让ename列的数据不重复
例子:
alter table emp add constraint qwe unique(ename)

4.默认约束:
意思很简单就是让此列的数据默认为一定的数据

格式:
alter table 表名称 add constraint 约束名称 约束类型 默认值) for 列名

比方说:emp表中的gongzi列默认为10000

alter table emp add constraint jfsd default 10000 for gongzi

5.外键约束:
这个有点难理解了,外键其实就是引用
因为主键实现了实体的完整性,
外键实现了引用的完整性,
应用完整性规定,所引用的数据必须存在!

其实就是个引用,
比方说一个表名称叫dept 里面有2列数据 一列是ID一列是ENAME
id:表示产品的编号
ename:表示产品的名称

另外一个表格名称是emp 里面有2列数据,一列是ID 一列是DID
id:表示用户号
did:表示购买的产品号

要让emp表中的did列去引用dept表中的id

可以用下面的方法

格式:
alter table 表名 add constraint 约束名称 约束类型 (列名) references 被引用的表名称(列名)

例子:
alter table emp add constraint jfkdsj foreign key (did) references dept (id)

   

SQL常用命令使用方法:

数据记录筛选:

select*from Products:查询出Products表里面的所有信息

select ProductID,ProductName from Products:查询出Products表里面所有的ProductID,ProductName

select ProductID,ProductName from Products where ProductID=1:查询出Products表里面ProductID=1的所有ProductID和ProductName

select* from employee where fname='Paul' and job_id=5 :查询出employee表中fname=Paul,并且job_id=5的所有记录

select*from Products where ProductID in(4,5,6):查询出Products表中ProductID为4,5,6的所有信息

select*from Products where UnitPrice>10 and UnitPrice<30 order by UnitPrice:查询出Products表中10<UnitPrice<30的所有信息,并按照UnitPrice的大小由小到大排序

select*from Products where UnitPrice between 10 and 30 order by UnitPrice:上面的另外一种写法

select * from Employees where FirstName like 'A%':查询出Employees中FirstName里面第一个字母是A的所有人信息

select*from Employees where FirstName like '%A%':查询出Employees中FirstName里面中间有A的所有人信息

select*from Employees where FirstName like '%A':查询出Employees中FirstName里面最后一个字母是A的所有人信息

select count(*) from Employees:查询出Employees表中的所有记录数

select min(Unitprice)from Products:查询出Products表中Unitprice的最小值

select max(Unitprice)from Products:查询出Products表中Unitprice的最大值

select avg(Unitprice)from Products:查询出Products表中Unitprice的平均值

select sum(Unitprice)from Products:查询出Products表中Unitprice的总和

select * from Products where Unitprice> (select avg(Unitprice) from Products):有子查询,查找出比平均值高的商品信息

select top 5* from Products:查询出前五条的记录信息

select distinct [name] from Category :查出Category 中不重复的name

select count(distinct name) from Category :查出Category 中不重复的name的数量

(2) 更新数据记录:
sql="update 数据表 set 字段名=字段值 where 条件表达式"
sql="update 数据表 set 字段1=值1,字段2=值2 …… 字段n=值n where 条件表达式"

(3) 删除数据记录:
sql="delete from 数据表 where 条件表达式"
sql="delete from 数据表" (将数据表所有记录删除)

(4) 添加数据记录:
sql="insert into 数据表 (字段1,字段2,字段3 …) values (值1,值2,值3 …)"
sql="insert into 目标数据表 select 字段名 from 源数据表" (把源数据表的记录添加到目标数据表)

(5) 数据记录统计函数:
AVG(字段名) 得出一个表格栏平均值
COUNT(*&brvbar;字段名) 对数据行数的统计或对某一栏有值的数据行数统计
MAX(字段名) 取得一个表格栏最大的值
MIN(字段名) 取得一个表格栏最小的值
SUM(字段名) 把数据栏的值相加
引用以上函数的方法:
sql="select sum(字段名) as 别名 from 数据表 where 条件表达式"
set rs=conn.excute(sql)
用 rs("别名") 获取统的计值,其它函数运用同上。

(5) 数据表的建立和删除:
CREATE TABLE 数据表名称(字段1 类型1(长度),字段2 类型2(长度) …… )
例:CREATE TABLE tab01(name varchar(50),datetime default now())

DROP TABLE 数据表名称 (永久性删除一个数据表)


2. 记录集对象的方法:
rs.movenext 将记录指针从当前的位置向下移一行
rs.moveprevious 将记录指针从当前的位置向上移一行
rs.movefirst 将记录指针移到数据表第一行
rs.movelast 将记录指针移到数据表最后一行
rs.absoluteposition=N 将记录指针移到数据表第N行
rs.absolutepage=N 将记录指针移到第N页的第一行
rs.pagesize=N 设置每页为N条记录
rs.pagecount 根据 pagesize 的设置返回总页数
rs.recordcount 返回记录总数
rs.bof 返回记录指针是否超出数据表首端,true表示是,false为否
rs.eof 返回记录指针是否超出数据表末端,true表示是,false为否
rs.delete 删除当前记录,但记录指针不会向下移动
rs.addnew 添加记录到数据表末端
rs.update 更新数据表记录

排序

desc就是用于查询出结果时候对结果进行排序,是降序排序,而asc就是升序。。要用与order by一起用。
例如select * from student order by id desc; 就是把选出的结果经过“按id从大到小排序”后,把资源返回。
还可以select * from student order  by age desc,id desc;用“,”号隔开多个排序条件,这样,先按age 再按 id,就是说,先按age从大到小排序,如果有相同年龄的,那么相同年龄的学生再按他们的id从大到小排序。

DESC指排序时按降序排序ASC是升序。

原文地址:https://www.cnblogs.com/Mzg121584668/p/8496520.html