zoukankan      html  css  js  c++  java
  • 数据库基础

    做项目过程中 发现之前数据库的知识 忘记好多 所以想重新整理一遍在这里记录下来

    一、数据库基础

     服务器管理软件
    php Apache
    .net IIS
    java Tomcat

    服务器管理软件起到的作用:管理服务器工作,具体的工作如下:
    1.静态页面
    用户将请求发送到服务器,服务器中的服务器管理软件将请求发给相应的页面,静态页面将内容反馈给服务器管理软件,服务器管理软件再将静态页面呈现给用户。
    2.动态页面
    用户将请求发送到服务器,服务器中的服务器管理软件将请求发给相应的页面,页面再发给php(如果用到数据库,则php和数据库Mysql建立链接)将页面转换成静态的反馈给服务器管理软件,服务器管理软件再将页面呈现给用户。

    WAMP:在Windows下开发
    1.搭建环境:模拟服务器

    修改数据库连接密码方式:

    打开SQL的命令行,默认的按回车就可以,然后输入修改密码的语句:

    Use mysql;

    Updatemysql.user  set  password=PASSWORD(123) where  user=’root’ ;

    其中:

    Mysql用户名:root

    Mysql密码:自己修改

    Mysql服务器地址:localhost

    数据表:
    1.列名 类型 长度
    2.主键 每个表都要有 主键列是不能重复的,是能够唯一标识一条数据的
    3.控制列的类型(索引) 唯一索引 非空 
    4.外键 存在与两个表之间的关系

    Mysql 数据类型

    包括:

    int 整型
    float 小数
    double 小数
    varchar(20) 字符串 ()括号内的数字表示长度
    bit 布尔型数据 true 或false
    datetime 日期时间类型
    text 长文本

    money 存货币
    image 存二进制数据

    关系型数据库

    数据库的设计:
    三大范式:
    1.第一范式:保证列的原子性,相对于功能
    山东省淄博市张店区
    山东省 淄博市 张店区

    2.第二范式:每一列都要和主键有关系
    每一列和该表有关系

    3.第三范式:每一列都要和主键有直接关系

      SQL语句基础

    注释语法:#注释语

    一、T-SQL语句
    注意:
    1.语句写完后用“分号;”代表这一句结束
    2.列结束用逗号,最后一列写完不用写逗号
    3.符号一定是英文的

    关键字:
    主键:primary key
    外键:foreign key
    非空索引:not null
    先建主表,在建从表;
    从表的外键语法:外键+列+引用+主表(列);foreign key (列名) references 主表名(列名)
    自增长列:auto_increment
    1.联合主键 2.再加一列自增长(自动递增,是整型的)

    1.创建数据库
    create database test2;

    2.删除数据库
    drop database test2;

    3.创建表
    create table test
    (
    code varchar(20),
    name varchar(20)
    );


    关键字,非空:
    create table test1
    (
    code varchar(20) primary key,
    name varchar(20)
    );

    create table test2
    (
    code varchar(20) primary key,
    name varchar(20) not null
    );

    主从表:
    create table zhu
    (
    code int primary key,
    name varchar(20)
    );
    create table cong
    (
    code int primary key,
    name varchar(20),
    zhu int,
    foreign key (zhu) references zhu(code)
    );

    自增长:关键字自增长:auto_increment
    create table haoyou
    (
    ids int auto_increment primary key,
    me varchar(20),
    friends varchar(20)

    );


    4.删除表:
    drop table test1;

     

    二、对数据的增删改查
    CRUD操作
    C:create 添加
    R:read 查询
    U:update 修改
    D:delete 删除

    1.C:添加数据
    添加 往 表 值()


    insert into 表名 values('n001','张三'); 必须添加第二列

    insert into 表名(列名) values(''); 不想添加第二列,可以指定列添加

    insert into 表名 values('','zs','sl'); 自增长列添加
    注意:
    1.如果是字符串类型,需要加(单引号''),如果是其他类型是不需要加单引号的;
    2.在添加数据时,值的数量要和列匹配,即使不想添加值,也要写单引号;
    3.如果就是不想添加第二列,可以指定列添加;
    4.如果要添加的列是自增长列,可以给个空字符串

    2.D:删除
    delete from 表名 :直接删除表的全部内容
    delete from 表名 where 条件 :删除表中的符合条件的数据
    例如:delete from test1 where code='n002'


    3.U:修改:不能修改主键
    update 表名 set 列名='值'
    update test1 set name='回族' :将所有的name列都改成回族了

    update 表名 set 列名=值 where 条件
    update test1 set name='回族' where code='n002'
    将test1表中的code是n002的name改成回族

    查询语句

    一、简单查询

    1.最简单查询(查所有数据)
    select * from 表名   注意:* 代表所有列,并不是代表所有行
    例:select * from test

    2.查询指定列
    select 列名,列名 from 表名
    例:select code,name from test

    3.修改结果集的列名 as
    select 列名 as '显示的字' from 表名
    例:select code as '代号',name as '姓名' from test

    4.条件查询
    select * from 表名 where 条件
    例:select * from test where code='n003'

    5.多条件查询
    或者 or:select * from 表名 where 条件 or 条件
    例:select * from test where code='p003' or nation='n001'

    并且 and:select * from 表名 where 条件 and 条件
    例:select * from test where code='p004' and nation='n001'

    6.范围查询 (某一列的内容是谁到谁之间的数据)
    例:两种写法:查找汽车价格在40到60之间

    (1)select * from car where price>=40 and price>=60
    (2)select * from car where price between 40 and 60

    7.离散查询
    查询汽车价格在(10、20、30、40、50、60)中出现的信息 in
    例:两种写法

    (1)select * from car where price=10 or price=20 or price=30 or price=40 or price=50 or price=60

    (2)select * from car where price in(10,20,30,40,50,60)

    不在(10、20、30、40、50、60)中出现的信息 not in
    例:select * from car where price not in(10,20,30,40,50,60)

    8.模糊查询(关键字查询)like
    %:任意n个字符
    _:任意一个字符

    查询汽车表名称中包含奥迪
    例:select * from car where name like '%奥迪%'

    查询汽车表名称第二个字符为“马”的汽车
    例:select * from car where name like '_马%'

    9.排序查询 order by
    升序 asc,可省略
    例:汽车表中价格列升序

    select * from car order by price asc

    降序 desc(从高到低)
    例:汽车表中油耗列降序

    select * from car order by oil desc

    先a列升序后b列降序
    例:汽车表中先将a列升序后将b列降序

    select * from car order by a,b desc

    10.去重查询 distinct
    例:查找汽车表中型号一样的去重

    select distinct brand from car

    11.分页查询

    一页显示m条数据 当前是第n页
    limit (n-1)*m,m

    一页显示10条数据 当前是第二页 跳过多少条,取多少条
    例:select * from chinastates limit 10,10


    12.聚合函数(统计函数)
    (1)总数 count(*):查询数据总条数
    例:select count(*) from chinastates

    count(主键列 areacode)
    例:select count(areacode) from chinastates

    (2)求和 sum(求价格和列)
    例:select sum(price) from car

    (3)求平均 avg(求价格平均列)
    例:select avg(price) from car

    (4)取最大值、最小值(价格列)
    例:

    select max(price) from car
    select min(price) from car

    13.分组查询 group by
    查询汽车表中每个系列下有多少汽车
    例:select brand,count(*) from car group by brand

    查询汽车表中卖的汽车数量大于3的系列    注意:  group by....having(条件)
    例:select brand from car group by brand having count(*)>3

     

    二、高级查询


    1.连接查询,对结果集列的扩展
    select * from info,nation      #形成很大的冗余(笛卡尔积)
    多张表的列有重名的,要写表名,然后写列名,格式如下:表名.列名
    两种方式:
    (1)select * from info,nation where info.nation=nation.code

    select info.code,info.name,sex,nation.name,birthday from info,nation where
    info.nation=nation.code

    (2)select * from info join nation on info.nation=nation.code

    2.联合查询,对结果集行的扩展, 列的数量要相同 union
    select code,name from info
    union
    select code,name from nation

    3.子查询
    父查询:外层查询
    子查询:里查询(查询结果作为父查询的条件)

    (1)无关子查询:子查询在执行时和父查询没有关系(子查询可单独执行)
    a.查询民族为汉族的所有人员信息
    父查询:select * from info where nation=()
    子查询:select code from nation where name='汉族'
    合并后就是结果:
    select * from info where nation=(select code from nation where name='汉族')

    b.查询系列名是“宝马5系”的所有汽车信息
    select * from car where brand=(select brand_code from brand where brand_name='宝马5系')

    (2)相关子查询:子查询在执行时和父查询有关系(子查询不可单独执行)
    a.查询汽车表中油耗小于该系列平均油耗的所有汽车信息
    父查询:select * from car where oil<(该系列平均油耗)
    子查询:select avg(oil) from car where brand=该系列
    合并后就是结果:
    select * from car as a where oil<(select avg(oil) from car as b where b.brand=a.brand)
    注意:用as修改表名时不用加引号''



  • 相关阅读:
    将图片转换为base64 格式
    BFC
    面试
    不足之处
    html 调用摄像头 并抓拍
    css,js零散知识的整理
    语义化标签SEO
    Modernizr
    快速排序(2)
    快速排序(1)
  • 原文地址:https://www.cnblogs.com/bhmmlxieliming/p/6409796.html
Copyright © 2011-2022 走看看