• mysql基本知识总结


            第一天
    
    create database act_web character set utf8; : 创建数据库并设立编码(命令中是不允许使用“-”的)        
    create user 'ning' identified by '32'; :创建用户并设立密码
    grant all on act_web.* to ning; :为用户授权,act_web.*代表数据库中的对象(如表、视图、包、函数、存储过程、触发器、索引等),all代表的增删改查
    
    执行外部SQL文件:
        set names utf8; //设置当前控制台编码
        source 文件全名;
        如:
        source /home/soft01/note_resource/cloud_note.sql
        source d:/robin/note_resource/cloud_note.sql
            
    1.退出: exit;
    2.MySql的常用命令
      select now();--当前的日期和时间
      select version();--查看当前数据库的版本
      select user();--查看当前用户
    
    3.数据库命令
      查看小数据库--show databases;
      使用数据库--use 库名;
      创建数据库--create database 库名;
      删除数据库--drop database 库名;
    
    4.数据库中的内容
    (1)查看当前库中存在什么表
        show tables;
    (2)创建一张表
        create table 表名(字段的名字1 字段的属性 [not null|null] 
    [ default value][auto_increment自增一][index索引])
    (3)字段的属性
         a.整型:
        tinyint:有负数时:-128--127  
                 没有负数:0-255
         通常使用在年龄,使用在IP
        int: 有负数时:-20亿--20亿    
              表示整数的时候:0--40亿;
        bigint:范围确实很大
          b.浮点型:
        floatdouble
        decimal(m,d):m--指代共有几位数
                 n--小数点后共有几位
        
          c.字符串
        char--定长字符串
        varchar--不定长字符集
        text--文本类型,大致可以存65500个字
        longtext--    
        enum(值1,值2,...):枚举类型
        sex enum("男","女")
           d.日期类型
        date--"2008-03-08"
        datetime--"2008-03-08 12:12:12"
        
    
        create table user32(
            userIP int(4) auto_increment primary key,
            username varchar(20),
            userpwd varchar(20)
            age tinyint,  
            sex enum("男","女"),
            sal decimal(5,1)
            
    );
    
        如何解决乱码?
        1.指定数据库中所有的字符都为UTF-8
        show variables like "%character%";   //查看数据库各个变量是否都为UTF-8
        character_set_filesystem=utf8;
        
            第二天
        
    1.修改表的结构(只能发生在学习数据库的初级阶段)
        添加字段: alter table 表名 add 字段名 字段类型
        修改字段: alter table 表名 change 旧的字段名称 新字段 新的字段属性
        删除字段: alter table 表名 drop 字段名称
        对表名的修改: alter table 表名 rename 新的表名            
        删除表: drop table 表名
    
    2.MySql中的数据操作
        (1)修改记录:
            update 表名 set 字段=where 限制条件
        (2)增加记录:
            insert into 表名 (指定字段) values (字段的值)
        (3)删除记录:
            delete from 表名 where 限制条件
        (4)查询语句:
            ---自学项目:MySql的分页查询该怎么写?
            子查询在真实项目中一般是禁止使用。
    
    3.连接查询
        内连接:返回两个表中所有满足条件的记录
        查询员工的姓名和所在部门的名称?
            select e.ename,d.dname from emp_ning e JOIN dept_ning d on(e.deptno=d.deptno);
        
        外连接:不仅返回两个表中满足条件的记录,还要返回不满足条件的记录
            左连接:查询没有部门的职员
            select e.ename,d.dname from emp_ning e left outer join dept_ning d on(e.deptno=d.deptno);
            右连接:查询没有职员的部门
            select e.ename,d.dname from emp_ning e right outer join dept_ning d on(e.deptno=d.deptno);
            
            
            第三天
            
    MySql的索引
        -----“目录” 快速查询记录
        存在形式:索引在数据库中是以文件的形式存在。
        特性:索引文件与数据库表中的记录是同步更新的。
        1.普通索引(MUL):最基本的索引,一张表可以添加多个普通索引(快速查询记录)。
        (1)在创建表的时候添加索引:
            create table test004(id int(4) auto_increment primary key,
            name varchar(20),pwd varchar(20),index(name));
            
            select .......from .........where 在后面必须添加上索引(字段=..)
        
        (2)对已经存在的表添加索引:
            alter table 表名 add index(字段的名称)
            
            alter table test004 add index(pwd);
        
        2.唯一索引(UNI):可以添加在一张表的多个字段上,如果一个字段添加了唯一索引,那么该字段的值不能重复。
            
        (1)在创建表的时候添加索引:
            create table test005(id int(4) auto_increment primary key ,phone varchar(20),name varchar(20),unique key(phone));
            
            insert into test005(phone,name) values("13678780832","baobao"); 
            insert into test005(phone,name) values("13678780843","ning"); 
            
        (2)修改表的时候添加唯一索引:
            alter table 表名 add unique key(字段名)
            
        3.主索引(主键):确定唯一记录的字段。
        
        4.全文索引:不支持中文,用不到。
        
        外键:外来的主键
        外键约束:保证了数据的完整性,实现多张表的统一操作。
        两件方式:
        1.级联删除:
            on delete cascade 
        2.级联修改:
            on update cascade
            
        创建用户表:主表
            create table user03(uid int(4) auto_increment primary key,name varchar(20),age varchar(20))
        engine=innodb;  联级发动机
        
            insert into user03(name,age) values("ning",23);
            insert into user03(name,age) values("long",24);
            
        创建一个订单表:从表
            create table order03(oid int(4) auto_increment primary key,oname varchar(20),money int(4),id int(4),
        foreign key(id) references user03(uid) on delete cascade) engine=innodb;
        
            insert into order03(oname,money,id) values("i love",5000,1);
            insert into order03(oname,money,id) values("you love",5000,2);
        
        
        
        
        
        
        
            
  • 相关阅读:
    企业架构-发布在线文档【企业架构框架-TOGAF v0.2.pdf】
    以后我的blog部分主题在其他地方写,留意者请继续关注!
    2010年3月blog汇总:企业架构、团队管理
    参加了两天QCon会议,你有什么感觉?
    DDD - 使用聚合(Aggregate)来设计类库
    发布【报表引擎设计.pdf】
    关心你的blog统计数据 给博客增加免费统计服务
    推荐:C2C文档销售与分享社区豆丁
    个人管理 - 后续的个人管理系列文章列表,大家一起来提提建议
    BABOK 需求获取(Elicitation)
  • 原文地址:https://www.cnblogs.com/shiyun32/p/9394215.html
Copyright © 2020-2023  润新知