美文网首页
2.数据库初识

2.数据库初识

作者: 我是WilliamWang | 来源:发表于2022-03-01 20:33 被阅读0次

    当前主要有两种数据库:

    • 关系型数据库(RDBMS主要)
    • 非关系型数据库

    关系型数据库主要产品:

    • oracle:大型项目中使用,银行、电信等项目
    • mysql:web时代使用最广泛的关系型数据库
    • ms sql server:在微软的项目中使用
    • sqlite:轻量级数据库,主要应用在移动平台

    关系型数据库核心元素:

    • 数据行(一条记录)
    • 数据列(字段)
    • 数据表(数据行的集合)
    • 数据库 (数据表的集合,一个数据库中可以有n多个数据表)

    SQL(Structured Query Language)

    在数据库中进行操作的语言,结构化查询语言,当前关系型数据库都支持使用sql语言进行操作。

    sql语言主要分为

    • DQL:数据查询语言,用于对数据进行查询,如 select
    • DML:数据操作语言,用于对数据进行增加、修改、删除,如insert、update、delete
    • DDL:数据定义语言,进行数据库、表的管理,如 create
    • DCL:数据控制语言,进行授权与权限回收,如 grant

    MySQl

    MySQL是一个关系型数据库管理系统

    • 安装 MySQL
    • 安装 navicat(图形化界面)
    • 连接数据库

    数据类型

    • 整数:int
    • 小数:decimal
    • 字符串:varchar

    约束

    • 主键(primary key):物理上的存储数据
    • 非空(not null):此字段不允许填写空值
    • 惟一(unique):此字段的值不允许重复
    • 默认值(default):当不填写此值时会使用默认值,如果填写时以填写时为准
    • 外键(foreign key):维护两个表之间的关联关系

    SQL语言

    SQL语句

    数据表操作

    • 创建表
    create table 表名(
          // 字段名 类型 约束,
          // 字段名 类型 约束,
    )
    例如:
    create table student(
    id int unsigned primary key auto_increment,
    name varchar(10),
    age int unsigned,
    height decimal(5,2)
    )
    
    • 删除表
    drop table 表名
    或者
    drop table if exists 表名
    

    数据操作-增删改查

    • 添加数据

    添加一行数据

    insert into 表名 values(...)
    例如
    insert into 表名 values(0,'小明',18,60) // 字段的值必须与数据的个数和顺序一一对应
    

    添加多行数据

    1. 写多条insert语句,语句之间用分号隔开
    insert into student values(0,'小明',18,60);
    insert into student values(0,'小红',19,70);
    insert into student values(0,'小绿',20,80);
    

    2.写一条insert语句,设置多条数据,数据之间用逗号隔开

    // 插入多个信息,设置所有字段信息
    insert into student values(0,'小明',18,60),(0,'小红',19,70),(0,'小绿',20,80);
    // 插入多个信息,只设置姓名
    insert into student(name) values('小明'),('小红'),('小绿');
    
    • 修改
    update 表名 set 列1=值1,列2=值2... where 条件
    例如:
    update student set name = '小黑',age='28' where id=5
    
    • 删除
    delete from 表名 where 条件
    例如:
    delete from student where id=6
    
    
    • 查询(简单查询)

    1.查询所有字段

    select  *  from 表名
    

    2.查询指定字段

    select 列1,列2,... from 表名
    例如:
    select name, age  from student
    
    也可以用表名.字段名
    例如:
    select student.name,student.age from student
    
    还可以通过 as 给表起别名
    例如:
    select s.name,s.age from student as s
    
    还可以使用as给字段起别名
    例如:
    select name as 名字,age as 年龄 from student
    

    3.查询时消除重复行

    select distinct 列1,... from 表名
    例如:
    select distinct name from student
    
    • 条件查询
      使用 where 对表中的数据进行筛选,查询符合条件的结果
      语法:
    select 字段1,字段2... from 表名 where 条件
    例如:
    select * from student where id=1
    

    where 后面支持多种运算符,进行条件的处理
    1)比较运算

    - 等于:=
    - 大于:>
    - 大于等于:>=
    - 小于:<
    - 小于等于:<=
    - 不等于:!= 或 <>
    例如:
    select * from student where name !='小明' 
    select * from student where age < 20
    

    2)逻辑运算

    - and
    - or 
    - not
    例如:
    select * from student where name = '小明' and age = 18
    select * from student where  age = 28 or name = '小明' 
    select * from student where not name = '小明' 
    

    3)模糊查询

    - like
    - %表示任意多个任意字符
    - _表示一个任意字符
    例如:
    select * from student where name like '%明%'
    
    1. 范围查询
    - in: 表示在一个非连续的范围内
    例如:
    select * from student where hometown in (‘北京’,'南京','东京')
    - between ... and ...表示在一个连续的范围内
    select * from student where age between 18 and 30
    

    5)空判断

    is null  // 注意与null是不用的
    例如:
    select * from student where card is null // 查询没有填写身份证的学生
    select * from student where card is not null // 查询填写了身份证的学生
    
    • 排序(为了方便查看数据)
      语法:
    select * from 表名
    order by 列1 asc|desc,列2 asc|desc,...
    // 默认从小到大排列
    // asc 升序
    // desc 降序
    例如:
    select * from student order by age // 升序
    select * from student order by age desc // 降序
    select * from student order by age desc,No // 查询所有学生信息,按年龄从大到小排,年龄相同时,再按学号从小到大排
    
    • 聚合函数

    为了快速得到统计数据,经常会用到5个聚合函数

    1.查询学生总数

    select count(*) from student
    
    
    1. 查询最大的年龄
    select max(age) from student
    

    3.查询女生的最小年龄

    select min(age)  from student where sex = '女'
    

    4.查询学生的年龄总和

    select sum(age) from student 
    

    5.查询学生的平均年龄

    select avg(age) from student 
    
    • 分组(略)

    where 和 having

    where是对from后面指定的表进行数据筛选,属于对原始数据的筛选
    having是对group by的结果进行筛选

    • 分页(略)
    • 连接查询(当查询结果的列来源于多张表,需要将多张表连接成一个数据集)

    准备数据

    drop table if exists student;
    create table student (
     studentNo varchar(10) primary key,
     name varchar(10),
     sex varchar(1),
     hometown varchar(20),
     age tinyint(4),
     class varchar(10),
     card varchar(20)
    )
    
    insert into student values
    ('001', '王昭君', '女', '北京', '20', '1班', '340322199001247654'),
    ('002', '诸葛亮', '男', '上海', '18', '2班', '340322199002242354'),
    ('003', '张飞', '男', '南京', '24', '3班', '340322199003247654'),
    ('004', '白起', '男', '安徽', '22', '4班', '340322199005247654'),
    ('005', '大乔', '女', '天津', '19', '3班', '340322199004247654'),
    ('006', '孙尚香', '女', '河北', '18', '1班', '340322199006247654'),
    ('007', '百里玄策', '男', '山西', '20', '2班', '340322199007247654'),
    ('008', '小乔', '女', '河南', '15', '3班', null),
    ('009', '百里守约', '男', '湖南', '21', '1班', ''),
    ('010', '妲己', '女', '广东', '26', '2班', '340322199607247654'),
    ('011', '李白', '男', '北京', '30', '4班', '340322199005267754'),
    ('012', '孙膑', '男', '新疆', '26', '3班', '340322199000297655')
    
    ---------------------------------------------------------------------------
    
    drop table if exists courses;
    create table courses (
     courseNo int(10) unsigned primary key auto_increment,
     name varchar(10)
    );
    insert into courses values 
    ('1', '数据库'),
    ('2', 'qtp'),
    ('3', 'linux'),
    ('4', '系统测试'),
    ('5', '单元测试'),
    ('6', '测试过程');
    
    ---------------------------------------------------------------------------
    
    drop table if exists scores;
    create table scores (
     id int(10) unsigned primary key auto_increment,
     courseNo int(10),
     studentno varchar(10),
     score tinyint(4)
    );
    insert into scores values 
    ('1', '1', '001', '90'),
    ('2', '1', '002', '75'),
    ('3', '2', '002', '98'),
    ('4', '3', '001', '86'),
    ('5', '3', '003', '80'),
    ('6', '4', '004', '79'),
    ('7', '5', '005', '96'),
    ('8', '6', '006', '80');
    
    
    1. 等值连接查询(两个表的公共数据)
    select * from 表1,表2 where 表1.列=表2.列
    或者
    select * from 表1
    inner join 表2 on 表1.列=表2.列 // 又叫内连接
    例如:查询学生信息和学生成绩
    
    select
     *
    from
     student stu,
     scores sc
    where
     stu.studentNo = sc.studentNo
    ---------------------------------------
    select
     *
    from
     student stu
    inner join scores sc on stu.studentNo = sc.studentNo
    
    
    1. 左连接查询(两个表的公共数据加左表特有数据)
    select * from 表1
    left join 表2 on 表1.列=表2.列
    
    1. 右连接查询(两个表的公共数据加右表特有数据)
    select * from 表1
    right join 表2 on 表1.列=表2.列
    
    • 自关联查询
    • 子查询
      在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语

    命令行

    连接仓库

    mysql -uroot -p 回车 输入密码
    

    查看所有仓库

    show databases;
    

    使用某个仓库

    use 仓库名;
    

    查看所有表

    show tables;
    

    查看表结构

    desc 表名;
    或者
    show create table 表名;
    

    中文乱码

    set charset gbk;
    

    函数

    视图

    对查询的封装

    create view 视图名称 as select语句;
    

    事务

    begin
    commit
    rollback 回滚

    索引

    外键

    相关文章

      网友评论

          本文标题:2.数据库初识

          本文链接:https://www.haomeiwen.com/subject/phpgxltx.html