Linux下的mysql数据库基本操作

1.数据的记载:

        1.1甲骨文
        1.2竹简
        1.3纸质记载
        1.4数字化

2.基本知识:

    数据库由多张表组成,表由字段和数据组成
    数据库--表--字段+数据

3.非关系型数据库:

redis 和 mongodb

4.关系型数据库:

Oracle:            付费,银行,大型项目用得到
MySQL:             开源,web项目用的特别多,免费
ms sql server:     微软项目中用的最多
sqlite:            轻量级数据库,多用于移动平台,智能项目

5.实时数据库:

firebase 

6.语句分类:

DQL:   数据查询语句,select
DML:   数据操作语言,数据增加、删除、修改 insert delete update
TPL:   事务处理,rollback
DDL:   数据定义语言:create drop 
CCL:   指针控制语言
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
时间: data '2018-09-03'
       time '11:11:11'
      datetime  '2018-09-03 11:11:11'
      timestamp 时间戳
大文本存储:text (0-65535)字符数大于4000 
枚举类型:enum

数据库设计—三范式
第一范式:1NF,原子性,保证每一列不能再拆分。地址:浙江省杭州市滨江区华新创业园A1206 11011
第二范式:2NF,保证每一个表里有一个主键,并且其他字段必须依赖于主键
第三范式:3NF,不存在传递依赖的情况

9.约束:

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

10.实现一个实例:

10.1设计数据库
    create database csdn charset=utf8;
    #创建   数据库   数据库名称  存储格式  ;
10.2使用数据库
    use csdn;
    #使用  哪个数据库
10.3创建一张新表:
    create table customer(
    #创建  表  表名
        id int primary key auto_increment not null,
        #字段名 数据类型 设为主键  id自动增加   不能为空  


        user_name varchar(10) not null,
        password varchar(10) not null,
        gender enum('boy','girl','secret'),
        active int default 0
    );

10.4查看数据库里的所有表
    show tables;
10.5查看表的结构
    desc customer(表名);
10.6删库操作
    drop database csdn;
10.7增加字段
    alter table customer add email varchar(20) not null;
    #alter table 表名 add 字段名 约束
10.8修改字段
    alter table customer change email e_mail varchar(20) not null;
    #alter table 表名 change 字段名 约束
10.9删除字段
    alter table customer drop email;
10.10删除表
    drop table customer;

11.数据的CRUD–初级:

11.0增加数据
    insert into customer values(0,'老王','123456','boy',0);
    #这句语句是增加表customer的一列数据,有多少字段,value就应该给多少值

11.1查询语句
    select * from customer;
    select name,gender from customer;
    select name as '姓名',gender from customer;
11.2按列插入数据
    insert into customer(user_name,password) values('老张','123456');
11.3按列插入多行数据
    insert into customer(email) values('[email protected]'),('[email protected]'),('[email protected]');
11.4修改数据
    update customer set email='[email protected]' where id=3;
11.5删除数据
    delete from customer whrer id=1;
11.6逻辑删除
    #一般删数据我们都不是吧数据直接删除,而是给一个字段,字段为0表示已经删除,字段为1表示没有被删除
    update customer set is_delete=1 where id=5;
    select * from customer where is_delete=0;

12.数据的CRUD–中级(条件):

12.2比较运算符的问题
    select * from customer where id>7 ;
    select * from customer where id!=7 ;  不等于
    select * from customer where id<>7 ;  不等于
12.3逻辑运算符
    select * from customer where id>7 and user_name='老王';
12.4模糊查询
    匹配姓王的:
    select * from customer where user_name like '王%';
    %代表匹配任意的多个字符

    匹配姓王的或者以王结尾的:
    select * from customer where user_name like '王%' or user_name like '%王' or user_name like '_王%';
12.5范围查询
    不连续范围查询
        select * from customer where id in(3,7,9,100);    (超出范围也不会报错)
    连续范围查询
        select * from customer where id between 3 and 9;  (包括序号3和9)
12.6 null的使用
    select * from customer where gender is null;
12.7 not null的使用
    select * from customer where gender is not null;
    select * from customer where gender is not null and user_name like '%王';
12.8 顺序优先级问题
    括号 > not > 比较运算符 > 逻辑运算符
12.9 排序的问题
    desc 降序
    asc  升序
    select * from customer order by id desc;
    select * from customer order by id desc,user_name desc;
    select * from customer order by id asc;

13.数据的CRUD–高级(查询结果):

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

    select avg(id) from customer where id>7;
    select sum(id)/count(id) from customer;
    select count(id) from customer where id>7;

13.2 分组的问题
    select gender from customer group by gender;
    select gender,group_concat(user_name,id) from customer group by gender;
    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.3 分页的问题(解决数据量巨大的问题)
    select * from customer limit 0,5;    左边是开始位置,右边是取多少条的数据

猜你喜欢

转载自blog.csdn.net/sui_yi123/article/details/82706118