言)+DBA-数据库管理员;
2. SQL(关系型结构化查询语言)=DDL+DML+DCL(数据控制语言)+DQ(数据查询);SQL
Server数据库管理系统;
3. 逻辑主键:
4. nvarchar(含有非ASIC码信息,比如中文,日文等等)和varchar定义数据不确定的,数
据确定的用char,不足长度n的部分会用空格填充;
5. SQL语句字符串用单引号,大小写不敏感;
6. 主键选择:Guid(产生使用newid()函数)和id(int);
7. bit为布尔类型 在SQL中只有(0和1);
8. null为不知道;
9. 聚合函数不能出现在where 子句中,having 对分组后的数据进行过滤,能用的列和
selecte 能用的列一样,不能代替where;或者说where 字句作用于基本表或者视图,从中选择满足条件的元组,Having短语作用于组,从中选择满足条件的组!
10. 限制结果集用于分页;
11. 不是确定要重复行,就用union all;
12. SQL重要语句:
定义数据库:
1) 创建数据库: create database mydatabase on(name=mydatabase_data;
filename=’d:\\sqlserverdata\\mydatabase_data.mdf; size=6; maxsize=20; filegrowth=10%;)-数据文件) log on(name=mydatabase_data; filename=’d:\\sqlserverdata\\mydatabase_data.ldf; size=2;maxsize=10; filegrowth=1;)-日记文件)两个文件加起来的大小就是整个数据库的8MB)
2) 修改数据库:alter database mydatabase modify name=mydatabase1
3) 扩大数据库:其中一种方式通过增加或扩大数据文件来扩大数据库;如:为原本的
数据库增加一个文件扩大到10MB:alter database mydatabase add file(name=mydatabase_01_data; filename=’…………ldf’; size=3; …………..)或者……modifyfile(…….size=8……..);
4) 收缩数据库:与扩大一样也有三种方式; 5) 分离数据库(sp_detach_db),附加数据库(…..for attach),删除数据库(drop); 6) 优化数据库:放置数据文件和日志文件;使用文件组;使用raid技术; 数据定义:
1)创建和删除模式:create schema student authorization fu/drop…
2)创建,修改和删除表:create table students(stuNumber int primary key not null;) /alter/drop...;
用SQL语句添加删除修改字段 1.增加字段
alter table docdsp add dspcode char(200) 2.删除字段
ALTER TABLE table_NAME DROP COLUMN column_NAME 3.修改字段类型
ALTER TABLE table_name ALTER COLUMN column_name new_data_type
3) 创建和删除 索引:create unique index stuNumber on student(Sno)/drop….; 数据查询: 1) 单表查询:
Select *** from Student;
Select distinct ***from Student;(消除重复行);
Select *** from Student where ***(查询满足条件的元组);
Select *** from Student where Sname like ’刘%’;select *** from Student
where like ‘刘_ _’;(通配符)
Select *** from Student where *** order by *** desc(默认ASC升序); Select *** from Student where *** group by ***(分组);
2)连接查询:
Select *** from Student , SC ;(一般连接)
Select *** from Student , SC where Student.Sno= SC.Sno;(等值连接)
Select Student.Sno *** from Student, SC where Student.Sno= SC.Sno(等值连接中把重复行的属性列去掉=自然连接)
Select First.Sno,Second.Sno from Student First,Student Second where First.Sno=Second.Sno;(自身连接);
Select Student.Sno ****from Student join SC on(Student.Sno=SC.Sno)(外连接)
Select Student.Sno *** from Student left join SC on(Student.Sno=SC.Sno)(左外连接)
Select Student.Sno *** from Student right join SC on(Student.Sno=SC.Sno)(右外连接)
3)嵌套查询:
Select Sname from Student where Sno in(Select Sno from SC where Cno=’2’;)(带in 子查询)
Select Sname from Student where Sno =/>/>=/!=/<>(Select Sno from SC where Cno=’2’;)(带运算符子查询)
Select Sname from Student where Sno = any/all(Select Sno from SC where Cno=’2’;)(带any 或all 子查询)
Select Sname from Student where Sno = any/all(Select Sno from SC where Cno=’2’;)(带any 或all 子查询)
Select Sname from Student where Sno exists(Select Sno from SC where Cno=’2’;)(带exists子查询)
Select Sno from SC where Cno=’1 ’ union(并)/intersect(交)/except(差)where Select Sno from SC where Cno=’2’;
4)聚集函数:
Count/sum/avg/max/min/(distinc|all);
数据更新: 1) 插入数据:
Insert into Student(Sno,Sname…..) values(‘3434’,’战三’…..); 2) 修改数据:
Update Student Set Sno=’5345’ where Sname=’战三; ’
3) 删除数据:
Delete from Student where Sno=’3453’;
13. 数据库函数
ABS():求绝对值
CELILING():舍入到最大整数; FLOOR() :舍入到最小整数; ROUND():四舍五入;
LEN():计算字符串长度;
LOWER()、UPPER():转小写,大写; LIRIM():字符串左侧的空格去掉; RIRIM():字符串右侧的空格去掉;
SUBSTRING(string,start_positon,length);
GETdate():获得当前日期时间;DATEADD();DATEIFF();DATEPARTE(); CAST():类型转换CONVERT():
14. 流控函数:
1)Isnull();
2)Case I
when 1 then ‘fdf’
when 2 then ‘fcdf’……; else ‘htgh’ end;
15. 索引:(全表扫描效率低)
1) 创建目录(主键默认都是有索引的), 2) 为每个字段建索引键虽然方便查找,但是占用空间;而且会降低更新数据操作,
所以只在经常检索的字段上创建索引;
3) 即使创建了索引有时候也需要全表扫描,如LIKE,函数,类型转换;
因篇幅问题不能全部显示,请点此查看更多更全内容