关于数据库的简单介绍

1、非关系型数据库:

redis mongodb

2、关系型数据库

1).oracle    付费,银行,大型项目用到
2).mysql     web项目,开源免费
3).ms sql server  微软项目
4).sqlite     轻量级数据库,移动平台

3、实时数据库

firebase

4、数据库组成

客户端----------服务端  -------数据库1----表1
       (SQL语句)              ------表2
                    -------数据库2

5、语句分类

DQL: 数据查询语句,select
DML:数据操作语言,数据的增加,删除,修改 insert delete update
TPL:事务处理 ,rollback
DDL:数据定义语言,create drop
CCL:指针控制语言

6、CRUD
增删改查(DQL DML DDL)

7、安装服务器端

sudo apt-get install mysql-server
sudo apt-get install mysql-client

重启
sudo service mysql restart
端口:3306

链接数据库
mysql -uroot -p 密码
退出:
quit/exit

8、数据库的数据类型

整型:int bit
小数:decimal【浮点数decimal(5,2)111.11】
字符串:varchar char(8) ['ab' 'ab      ' char的例子] (0-255)
时间:   date '2018-09-03'
        time '11:11:11'
        datetime  '2018-09-03 11:11:11'
        timestamp 时间戳:格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数
大文本存储:text (0-65535)字符数大于4000
枚举类型:

9、约束

主键:primary key 物理存储顺序
非空:not null 不允许为空
唯一:unique 不允许重复
默认值:default 默认值,如果填写,以填写的值为准
外键:foreign key 

10、实现一个实例

    10、1设计数据库
    create database wuyanzu charset=utf8;
    10、2使用的数据库
    use wuyanzu;
    10、3创建一张新表
    create table customer(
        id int primary key auto_increment not null,
        name varchar(10) not null,
        password varchar(10) not null,
        gender enum('boy','girl','secret'),
        active int default 0 
    )
    create table address(
        id int primary key auto_increment not null,
        add_name varchar(10) default '杭州',
        cus_id int not null
    );
    10、4查看数据库里的表
     show tables;
    10.5查看表的结构
    desc customer(表名)
    10.6删库操作
    drop database wuyanzu;
    10.7增加字段
    alter table customer add email varchar(20) not null;
     alter table customer add is_delete varchar(20) not null default 0;

    10.8修改字段
    alter table customer change name user_name varchar(20) not null;
    10.9删除字段
    alter table customer drop email;(字段名)
    10.10 删除表
    drop table customer

11、数据CRUD–初级

    11.0 增加数据
    insert into customer values(0,'老王','123456','boy',0);
    insert into customer values(0,'王丑丑','123456','girl',1);
     insert into address values(0,0,1);
     insert into address values(0,'杭州',3);
    11.1 查询语句
    select * from  customer;
    select name as '姓名',gender from  customer;
    11.2 按列插入数据
    insert into customer(user_name,password) values('老张','123456');
    11.3 按列插入多行数据
    insert into customer(user_name,password,email) values('老王','123456','12@qq.com'),('老李','123456','23@qq.com');
    11.4 修改数据
    update customer set email='123@qq.com' where id=3;

    11.5 删除数据
    delete from customer where id=1;
    11.6 逻辑删除
     update customer set is_delete=1 where id=5;
     select * from customer where is_delete=0;

12、数据CRUD–中级(条件)

    12.1 比较运算符的问题
    select * from customer where id > 7;
    select * from customer where id !=7;
    select * from customer where id <>7;(> >= < <= != <>)

    12.2逻辑运算符
    select * from customer where id > 7 and user_name='老王';(and or not)

    12.3 模糊查询
    select * from customer where user_name like '王%';
    %代表匹配任意的多个字符
    select * from customer where user_name like '王%' or user_name like '_王%' or user_name like '%王';

    12.4 范围查询
    不连续范围查询
    select * from customer where id in(3,7,9,100);
    连续范围
    select * from customer where id between 3 and 9;(包括序号3和9)

    12.5 null
    select * from customer where gender is null;

    12.6 not null
    select * from customer where gender is not null and user_name like '%王';

    12.7 顺序的问题
    括号 > not >比较运算符 > 逻辑运算符

    12.8 排序的问题
    desc降序 asc升序
    select * from customer order by user_name desc ,id desc;
    语句顺序的问题,如果第一个条件出现同值的情况,按照第二个语句来进行排序

13、数据CRUD–高级(查询结果)

    13.1 删除重复行
    select distinct user_name from customer;
    13.2聚合函数
    sum() max() min() avg() count()

    select avg(id) from customer where id >7 ;
    select sum(id)/count(id) as '均值' from customer where id >7 ;
    select count(id) from customer where id >7 ;

    13.3 分组的问题
    select gender from customer group by gender;
    select gender,group_concat(user_name) from customer group by gender;
    select gender,group_concat(user_name) from customer group by gender having id>3;(x)
    select gender,group_concat(user_name,id) from customer group by gender having group_concat(id)>3;

    having 和 where 功能是一样的
    select * from customer having id>3;

    select gender,avg(id) from customer group by gender;
     13.4分页的问题(解决数据量大的问题)
    select * from customer limit 0,5;

14、完整版语句

    select distinct * from customer
    where 条件
    group by 字段  
    having 条件
    order by limit start,count


    from customer > where >group by> distinct * >having >order by >limit

15.SQL注入的问题
配合execute,列表化参数,或者正则匹配

16.连接查询

内连接查询
select * from (customer inner join address) on id=cus_id;
select * from customer inner join address on customer.id=address.cus_id;
左连接查询
select * from customer left join address on customer.id=address.cus_id;
右连接查询
select * from customer as a right join address as b on a.id=b.cus_id;

17.子查询

select * from customer where id >(select avg(id) from customer);
select * from customer where id in (select cus_id from address);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值