zoukankan      html  css  js  c++  java
  • 黑马程序员+SQL基础(上)

    黑马程序员+SQL基础

    1  DBMS:数据库管理系统(oracle,mssqlserver,db2,access,mysql,sybase等)

    2  分类:不同类的数据放到不同的数据库中,便于对各个分类进行个性化管理,避免命名冲突,安全性更高

    3  table: 表,不同类型的资料放到不同的格子中

    4  Column:列 ;Field 字段

    5  主键:pk 唯一标识行  ;业务主键:用业务意义的字段做主键,如身份证号;逻辑主键:没有任何业务意义字段做主键,推荐用逻辑主键

    6  外键 fk ;创建数据库,创建表,设置主键

    7 数据类型:int ,bit,char(10),datetime,float,image,decimal(18,0),money,bigint,smallint,nvarchar(50)…

    8  varchar,nvarchar,char的区别:char不满足长度,会自动用空格填充,varchar则不会,Var:variable可变

    9  sql语句中字符串用单引号‘’,大小写不敏感,但字符串值还是大小写敏感的

    10  新建查询:

    create table T_Person(id int not null,name nvarchar(10),age int not null)   --创建表

    drop table T_person   --删除表

    select NEWID()--guid

    11   SQL主要分DDL数据定义语言(create table, drop table ,alter table)和DML数据操作语言(select insert)

    12  主键类型:int, uniqueidentifier (guid)   (标识规范)自动增长,一个表只能有一个标识列

    13  .net生成guid方法:Guid.NewGuid(),数据库中用newid()方法;int自增字段优点:占用空间小,易读,效率低;guid:效率高,不易读, 占用空间大;业界更倾向于Guid

    14 数据插入:insert into 表名(列名)values(值);可以给字段默认值,如果guid类型主键的默认值设定为newid()会自动生成 

        insert into T_person(name,age) values ('kk',20) --插入数据

    insert into T_person values ('jj',20) --插入数据

    15 Guid主键:
    create table T_Person2(id uniqueidentifier  not null,name nvarchar(10),age int not null)   --创建表

    insert into T_Person2(id,name,age)values(NEWID(),'ll',20)  --插入数据NEWID()

    select * from T_Person2 --查询表

    16 更新数据:
        update T_Person2 set name='qq',age=30 --更新多条数据

    update T_Person2 set name='qq',age+=1 --更新自己的数据

    select * from T_Person2 --查询表

    17  条件更新数据:

    update T_Person2 set name=N'青年人' where age>35 ---更新年龄大于的人员名字为青年人

    update T_Person2 set name='38' where age=38    --更新年龄等于的名字为

    update T_Person2 set name='gg' where (age>35 and age<40) or age=31  --更新年龄在到岁或者是岁的名字为gg

    18  删除数据  
    delete from T_Person2 where age<21 --删除年龄小于

    select * from T_Person2 --查询表  

    19  检索数据
    select * from T_Person2 --查询表

     select name as 姓名,age as 年龄 from T_Person2 --as取别名

    select 1+2 as 列,GETDATE() as 日期,NEWID() as 编号 --可不于数据库相关

    20 聚合函数

       select COUNT(*)from T_Person2 --统计多少条数据

    select MIN(age)from T_Person2--最小年龄

    select MAX(age)from T_Person2--最大年龄

    select AVG(age)from T_Person2--平均年龄

    select SUM(age) from T_Person2 --求和年龄

    21  排序
      select * from T_Person2 order by age desc--按年龄排序(降序)

    select * from T_Person2 order by age asc,name desc --按年龄排序(升序),后年龄降序

       select * from T_Employee where FAge>23 order by FAge desc,FSlary asc –年龄大于23,按年龄降序,工资升序进行排序

    22  通配符(模糊查询)
        select * from T_Employee where FName like ‘_erry’    -- _表示一个字符(like)

    Select * from T_Employee where FName like ‘%n%’   --%表示多个字符,含有n字符的

    Select * from T_Empployee where FName like ‘n%’    --以n开头姓名
    23 空值处理(null :不知道)

    Select null+1   --null不知道

    Select ‘abc’+’123’  --abc123

    Select * from T_Employee where FName is(not) null –查询姓名为null的数据
    24 多值匹配
        select * from T_Employee where FAge in(23,45,28)   –in用法

    Select * from T_Employee where FAge>20 and FAge<30  —-年龄20~30

    Select * from T_Employee where FAge  between 20 and 30  --年龄20~30

    25 数据分组

    Select FAge,count(*) from T_Employee group by FAge –根据年龄分组后计数

    Select FAge,count(*) from T_Employee group by FAge having count(*)>1 --聚合函数不能出现在where语句中,应放在having语句中(放在group by后)

    Select FAge,count(*) from T_Employee group by FAge having FSalary>2000

    --此句错误,having是对分组后信息的过滤,能用的列和Select中能用的列一样;而where对原数据进行过滤

    26 限制结果集函数

      select top 3 * from T_Employee where FSalary desc –工资前三数据

      select top 3 * from T_Employee where id not in(select top 5 id from T_Employee order by FSalary desc)   --id(6~9)工资降序数据    分页效果

    select distinct FDepartment from T_Employee   -- distinct消除完全重复的数据

    27联合结果集(union)合并

      Select FName,FAge from T_TempEmployee union select FName,FAge from T_Employee  --两表的列名个数一样和数据类型相容(union会消去相同的数据)

      Select FName,FAge from T_TempEmployee union all select FName,FAge from T_Employee  --两表的列名个数一样和数据类型相容(union all不会消去相同的数据)

    28习题:
         报表:select N‘正式员工最高年龄’,max(FAge) from T_Employee
      union all  select N‘正式员工最低年龄’,min(FAge) from T_Employee

    Union all select N‘临时员工最高年龄’,max(FAge) from T_TempEmployee

    Union all select N‘临时员工最低年龄’,min(FAge) from T_TempEmployee
         合计:select FNumber,FSalary from T_Employee union all select N‘工资合计’,Sum(FSalary) from T_Employee

  • 相关阅读:
    CSS3 动画--- CSS3 animation
    CSS3 3D变形 transform---rotateX(), rotateY(), rotateZ(), 透视(perspective)
    CSS3 2D变形 transform---移动 translate(x, y), 缩放 scale(x, y), 旋转 rotate(deg), transform-origin, 倾斜 skew(deg, deg)
    CSS3 过渡---transition
    情人节用Python智能聊天机器人的实现|制作一个虚拟恋人
    Python基础知识详解 从入门到精通(七)类与对象
    Python核心编程:8个实践性建议
    给Python初学者的一些编程技巧
    30分钟编写一个抓取 Unsplash 图片的 Python爬虫
    Python反编译调用有道翻译(附完整代码)
  • 原文地址:https://www.cnblogs.com/zhangliang1990/p/3405316.html
Copyright © 2011-2022 走看看