mysql 基本操作

一. 为啥使用数据库?

数据库就是存储数据的仓库

因为之前使用文件(Excel)管理数据, 但是数据量特别大的时候,使用Excel管理 的话, 就比较的麻烦了 , 因此引入一个新的数据管理软件 : 数据库软件

二. 数据库的分类?

关系型数据库
        
        1. 有约束
        2. 基于硬盘的存储 (就是将数据存储到硬盘上, 持久化 === 落地)
        
        典型代表:
            MySQL oracle(国企) sqlserver(微软)  sqllite  db2
        
非关系型数据
    
        1. 没有约束 (key--->value)
        2. 基于内存存储 (将数据放入到内存中)
        
        典型代表:
            MemCache, redis(微博), mongodb

三. mySQL的架构:

    客户端:
           socket客户端, 连接服务端, 发送指令 (SQL语句)
   服务端:
          socket服务端, 接收客户端的指令, 并将结果返回给客户端

四. MySQL 的安装:

版本: 5.5 以上  5.7 以下
        
        1. 可执行文件:
.exe 或者 .msi文件 点击下一步
2. 压缩包 解压, 进入目录 bin mysqld : 启动服务 mysql : 连接服务端 3. 环境变量的配置 找到安装目录,将bin路径复制填入系统设置的path中即可 4,注册服务 将mysql注册到系统服务中 mysqld --install 删除服务 sc delete mysql(服务名) 查看系统服务 运行->services.msc 通常设置为自动启动。 5,修改密码 #方式一: mysqladmin -uroot -p旧密码 password 123 警告忽略即可 #方式2: 跳过授权表 进入系统修改授权表推荐 跳过这个操作是服务器相关的操作所以 咱的先关掉服务器重新开 在重新开的时候来告诉它 1.停止服务 2.启动服务器并添加参数 **mysqld --skip-grant-tables** 3.使用客户端登录服务器 执行修改命令 此时不需要输入密码 update mysql.user set password = password("123123") where user="root" and host="localhost"** 4.刷新权限 flush privileges 5.命令行中重启服务器验证新密码 6,编码设置 使用客户端执行s 可以查看当前服务器的设置信息 latin1 gbk 就是默认的编码。 服务器是拉丁 客户端是 GBK 很显然要乱码,我们必须保证编码方式一致! 如何设置: 在mysql安装目录下有个my_default.ini 他就是配置文件,但是他不是正在使用的 而是一个模板文件,我们如果要自己编写配置文件,需要自己复制一个叫做my.ini的文件来编写 编写格式我们已经学习过了 configpaser模块 [section] option = value 要设置客户端的内容就写在mysql分区 要设置服务器的内容就写在mysqld分区下 mysql在启动的时候会自动到安装目录下找my.ini文件 找到命令对应的标题加载里面的设置项 测试:在配置文件中加入用户名密码配置 [mysql] user = "root" pasword = "123" 需要注意的是:mysql mysqld都会来读取这个文件,对于客户端和服务器我们需要使用section来区分 # 服务器配置部分 [mysqld] character-set-server = utf8 # 客户端mysql配置部分 [mysql] default-character-set = utf8 # 其余客户端配置部分 [client] default-character-set = utf8 7,目录解析 bin 执行文件 mysqld 服务器主程序 mysql 无界面的客户端 data 数据文件 my-default.ini 配置文件模板

五. 初始化:

mysqld --initialise-secure

六. 数据库的操作:

数据库 (文件夹):
        表 (文件)
           数据行 (文件中的一行内容)

1. 数据库:------------------------------------------------------------------
        
                增加:
                    SQL语句:  
                        create database 数据库名称;
                    例子:
                        create database db1;
                    
                删
                    drop database 数据库名称;
                    drop database db1;
                    
                修改
        
                    没有专门的修改指令 update
                    
                    删了重新建
                    
                查询
                    show databases;
                
                使用:
                    use 数据库名;
                    use db1;
        
  2. 数据表:-----------------------------------------------------------------
        
                新建:
                    use db1;
                    
                    版本0:
                        SQL语句:
                            create table 表名 (
                                列名1 列类型
                            );
                        
                        例子:
                            create table t1 (
                                id  int,
                                name char(32) 
                            );
                        
                        增加
                            指令: 
                                insert into  表名 (列1, 列2) values (值1, 值2);
                            
                            例子: 
                                insert into  t1 (id, name) values (1, 'zekai');
                                insert into  t1 (id, name) values (2, '你好');
                    
                    改进1:
                        create table 表名 (
                            列名1 列类型
                        )engine=Innodb charset=utf8;
                    
                    ps: 
                        引擎: Innodb 和 MyIsam 
                        5.5 版本以上  默认是Innodb
                        
                        create table t2 (
                            id  int,
                            name char(32) 
                        )engine=Innodb charset=utf8;
                        
                        insert into  t2 (id, name) values (1, '你好');
                        insert into  t2 (id, name) values (1, 'xxx');
                    
                    改进2:
                    
                        create table 表名 (
                            列名1 列类型 auto_increment primary key
                        )engine=Innodb charset=utf8;
                        
                        create table t4 (
                            id  int auto_increment primary key, 
                            name char(32)  not null  default ''
                        )engine=Innodb charset=utf8;
                        
                        auto_increment : 自增
                        primary key : 主键索引 (作用: 加快查找的速度)
                        not null : 不能为空
                        default : 默认值
                        
                        注意: 后面一列写完之后, 不能加逗号  (*********)
                        
                        一种:
                            insert into  t3 (id, name) values (1, '你好');
                            insert into  t3 (id, name) values (2, 'xxx');
                        二种:
                            insert into  t3 (name) values ('hello');
                            insert into  t3 (name) values ('xxx');
                        
                    
                    -------------------------------------------------------------
                    
                    最终的格式:
                        create table 表名 (
                            列1 列类型 [是否为null 默认值],
                            列2 列类型 [是否为null 默认值],
                            .....
                            列n 列类型 [是否为null 默认值]
                        )engine = 存储引擎  charset = 字符集

                    最终的例子:
                        create table t4 (
                            id  int auto_increment primary key, 
                            name char(32)  not null  default '',
                            pwd  char(32)  not null  default ''
                        )engine=Innodb charset=utf8;
                    
                    查看: 
                        指令:
                            select 列名 from 表名;
                        例子:
                            select * from t1;
                
                    
                    列类型:
                        
                        a. 数值型
                            create table t4 (
                                id  unsigned mediumint auto_increment primary key, 
                                name char(32)  not null  default '',
                                pwd  char(32)  not null  default ''
                            )engine=Innodb charset=utf8;
                            
                            tinyint : 
                                范围:  
                                    有符号: -128到127
                                    无符号: 0 到 255  unsigned
                            smallint
                                范围:  
                                    有符号: -32768到32767
                                    无符号: 0 到 65535  unsigned
                            
                            mediumint
                                范围:  
                                    有符号: -8388608到8388607
                                    无符号: 0 到 16777215  unsigned
                            int
                            bigint
                            
                            区别: 
                                a. 取值范围不一样, 根据自己公司的业务来去选择
                                b. 无符号和有符号的意思
                            
                            float(M,D) 浮点型
                            decimal(M,D) 定点型 比float更加的精准
                                
                                例如: 3.1415151519868789789
                                float: 3.141515000000000000
                                decimal : 3.1415151519868789789
                                
                                126.35
                                
                                M:小数总共多少位 decimal(5, )
                                D:小数点后面几位 decimal(5, 2)
                                
                                使用场景:
                                    比如 说存 salary 工资 : 6000.23 decimal(,2)
                                    
                        b. 字符串类型
                            
                            char : 定长 char(32)  这一列的值就是32  优点: 速度快  缺点: 浪费
                            varchar : 变长 varchar(32)              优点: 不浪费, 节省空间  缺点: 速度慢
                            
                            根据自己公司的业务来去选择:
                                
                                create table userinfo (
                                    id  unsigned mediumint auto_increment primary key, 
                                    name varchar(128)  not null  default '',
                                    pwd  char(32)  not null  default '',
                                    create_time  datetime not null default  '1970-01-01 00:00:00'
                                )engine=Innodb charset=utf8;
                                
                            一般情况下, 如果没有100%的把握, 都是用varchar()
                            
                            
                            text: 文本  范围比较大, 如果存储大量字符的话, 可以使用这个字段
                                            
                        c. 时间类型
                                date 2019-6-12
                                
                                推荐使用datetime
                
                
                删
                    指令:
                        drop table 表名;
                        
                        连带着将数据表中的所有数据都会删掉
                        
                        ps: 工作中, 线上数据库, 这个命令根本不会让你用到
                        
                    实例:
                        drop table t1;        
                
                查询
                    show tables;
                    
                    desc 表名;   :  查看表的结构
                    
                    show create table 表名 :  查看表的创建过程
                        
                关于主键自增: (不是重点)
                    show session variables like 'auto_inc%';
                    set session auto_increment_increment = 2;
                    
                    show global  variables like 'auto_inc%';
                    set global auto_increment_increment = 2;
                    
                
                修改
                    改字符编码
                        alter table 表名 default character set gbk
                        
                    改名称
                        rename table tname1 to tname2
                        
                    改字段
                        alter table 表名 add | modify | drop | change  
                        
                        add  跟 列名和类型
                        
                        modify 跟 列名 和 类型
                        
                        drop 跟列名
                        
                        change 跟 旧列名  新列名 类型
                
                    create table t4 (
                        id  int auto_increment primary key, 
                        name char(32)  not null  default '',
                        pwd  char(32)  not null  default ''
                    )engine=Innodb charset=utf8;
                    
                    修改字段:
                        alter table 表名(t3) change 原列名(name) 新列名(username varchar(32) not null default '');
                    
                    新增字段:
                        alter table 表名(t3)  add  新列(pwd char(32)  not null  default '');
                        
                    删除字段:
                        alter  table 表名(t3) drop 列名(pwd);

        
    3. 数据行:-------------------------------------------------------------------------
                
                增
                    insert into  t3 (id, name) values (1, '你好');
                
                查询
                    
                    select * from t3; : 将表中的 所有的列全部列出
                    select 列名, 列名, 列名 from t3 : 将某一列的值查出
                
                删
                    
                    delete from 表名(t3); 将表中的所有的 数据删除掉, 再次添加的时候, 继续会延续上一个 ID
                    
                    truncate 表名(t3);    将表中的所有的 数据删除掉, 再次添加的时候, ID 会重新开始
                    
                    truncate 速度快
                    
                    ps: 工作中, 线上数据库, 这个命令根本不会让你用到
                    
                    delete from 表名(t3) where name = 'xxxxx';
                    
                    
                修改
                
                    update t3 set username='zekai';
                    
                    update t3 set username='xxxx'  where  id=3;
        
                    update t3 set username='xxxx', pwd='xxxxx'  where  id=3;
        

七. 外键: 

        
        缺点:
            1. 数据重复
            2. 如果 部门过长的话, 太占用空间
            
        
        解决方法:
            
            重新设计一张表, 这张表 中存放部门的相关信息
        
        
        部门表:
        
            create table department (
                id  int auto_increment primary key, 
                depart_name varchar(32)  not null  default ''
            )engine=Innodb charset=utf8;
            
            insert into department (depart_name) values ('公关'), ('关关'),('关公');
            
            create table userinfo (    
                id  int auto_increment primary key, 
                name varchar(32) not null default '',
                depart_id int not null  default 1,
                
                # constraint 外键名(fk_userinfo_depart) foreign key (列名(depart_id)) references 表名(department)(关联的列名(id)),
                constraint fk_userinfo_depart foreign key (depart_id) references department(id)
            
            )engine=Innodb charset=utf8;
            
            
            create table student(
                sid int auto_increment primary key,
                sname varchar(20) not null default '',
                gender int not null default '',
                score_id int not null default 1,
                constriant fk_student_score foreign key (score_id) references score(sid)
            )
            
            
            insert into userinfo (name, depart_id) values ('root1', 1);
            insert into userinfo (name, depart_id) values ('root2', 2);  错误的
            
            注意:
                创建多个外键的时候, 名称不能一样
            
            =====> 一对多
 


原文地址:https://www.cnblogs.com/HZLS/p/11014454.html