1、我的数据库原理及应用学习笔记 数据库原理及开发应用(总结的知识储备)1. 数据库就是储存数据的仓库,专业的解释就是对数据存储和管理的软件和数据本身统称为数据库。2. 大型数据库有Oracle,Sql sever属于中型数据库,小的太多忽略不计,要知道就上网查。3. 数据库管理系统的主要功能:数据库定义功能;数据存取功能;数据库运行管理;数据库的建立和维护功能。4. 数据库的三级模式构:外模式、模式和内模式。(外模式即是用户模式可以多个,模式只能一个,内模式就是储存模式)。5. DDL: 数据库模式定义语言DDL(Data Definition Language)。6. DML:数据操作语言(d
2、atamanipulationlanguage)。7. DBMS: 数据库管理系统(Database Management System)。8. DBS:数据库系统(Database System)。9. DBA:数据库管理员(Database Administrator)。从三方面学习数据库1. 数据库是如何储存数据的?(主键,外键,唯一键)2. 数据库是如何操作数据的?(insert update delete)3. 数据库是如何显示数据的?(select重点)操作及代码1. Crate table 语句:create table student(stu_name nvarchar(30)
3、 not null,stu_numb int not null,stu_sex nchar(1) not null);2. 主键外键:stu_numb int primary key;(可以在int 和primary中间加上constraint 约束名称)。 sch_add nvarchar(30) foreign key references student (stu_numb); 顺带回忆一下主键和外键的定义:主键:能够唯一标示一个事物的的字段或者多个字段的组合,叫主键。(含有主键的表叫做主键表,主键一般是整数不建议用字符串,而且一般用代理主键不要用业务主键,也就是说不要用有业务的字段作为
4、主键,一般就用没有含义的编号当做主键。) 外键:如果一个表中的若干个字段来自若干个表的主键或者唯一键,就称这若干个字段为外键。(含有外键的表叫做外键表,通常都来自主键,因为唯一键可能为null,而且外键不一定来自别的表可能就来自本表)。自认为总结的超好。 哦哦,还有删除表的时候先删外键表,不然先删主键会报错,原因很简单,外键引用错误。 没完,主键约束:不允许重复元素,避免了数据的冗余。外键约束:从语法上保证所关联的事物一定存在,而且事物和事物之间的关系是通过外键来体现的。 3. check语句: stu_mark int check(stu_mark=60 and stu_mark=1500
5、and salary =1500 and salary =5000是不可取的,这两个命令是一起的,中间不能打分号。查找另外一个范围的数据时可以用以下语句:select * from empwhere salary not between 1500 and 5000(另外一种是:select * from empwhere salary =5000 )4. in【若干个孤立的值】select * from emp where salary in (1500,3000)等价于select * from emp where salary =1500 or salary =3000select * f
6、rom emp where salary not in (1500,3000)等价于select * from emp where salary 1500 and salary 3000 也作select * from emp where salary !=1500 and salary != 3000-数据库中不等于有两种写法: != 和 推荐使用第二种。-or取反时记得改为and,呵呵。5. top【最前面的若干个记录,专属于sql server不可移植到其他数据库】select top 5 * from emp;select top 15 percent * from emp;-此语句输
7、出的是百分之15的行数的所有字段,如果经计算百分之15是2.1,则输出的是三行。而且 * 不能省略,否则报错。6. null【没有值 空值】零和null不一样,null表示没有值,而零表示一个确定的值。null不能参与这些运算: != =可以参与如下运算:is is not (下面的例子中emp 表示表名,而 comm表示字段名)select * from emp where comm is null;-输出的是奖金为空的员工的信息。select * from emp where comm is not null;-输出的是奖金不为空的员工信息。select * from emp where
8、comm null;-输出为空errorselect * from emp where comm != null;-输出为空errorselect * from emp where comm = null;-输出为空error任何类型的数据都允许为nullcreate table t1 (stu_name nvarchar(30),stu_id int ,stu_sex nchar(1);insert into t1 values(null,null,null);-正确任何数字与null参与数学运算的结果都是null(后面例子中empna表示员工名字,empsal表示月薪,empid表示员工号
9、码,comm与上面一样表示年终奖金)select empid,empna,empsal * 12 + comm as ”年薪” from emp;运行结果是最后一个字段都为null,证明了上面的说法。另外一种正确的写法:select empid,empna,empsal * 12 + isnull(comm.,0) as “年薪”from emp;关于函数 isnull(comm.,0)的解释:如果comm的值为null则返回0,如果不是null则返回原本的comm值。7. order by【以某个字段排序】order by a,b按a,b都按升序排列。order by a,b desca升序
10、,b,降序order by a desc, ba 降序,b升序order by a desc,b desca,b 都按降序排列。-解释:默认是按照升序排列的,升序是asc 可以不写。(ascending,descending)8. 模糊查询【搜索时经常使用】格式:select 字段的集合 from 表名 where 某个字段的名字 like 匹配条件(匹配条件通常含有通配符 % )通配符表示任意零个或者多个字符select * from emp where ename like %A%-只要含A的ename 就输出。select * from emp where ename like A%-e
11、name只要首字母为A就输出。select * from emp where ename like %A-ename 只要以A结尾就输出。_【下划线】表示任意单个字符select * from emp where ename like _A%-ename只要第二个字母为A就输出。a-f a到f 中的任意单个字符select * from emp where ename like _a-f%-ename中只要第二个字母是在a到f中的就输出。a,f a或fa-c不是a到c中的任意一个字母,看清楚是 不是 自己瞎了别怪我没提醒。select * from emp where 部门ip like _发,联%(这个语句是从sql sever里面复制的,感觉颜色还蛮好看的)注意:匹配的条件必须用单引号括起来,不能傻不拉几的用双引号,后面来解释一下要搜索带 % _ 的字段时的方法。直接把在sql sever里的复制看:create table people (name nvarchar(30) not null, age int)