美文网首页
Mysql基本概念和Sql基本语法

Mysql基本概念和Sql基本语法

作者: 吃货大米饭 | 来源:发表于2019-06-27 11:26 被阅读0次

    数据库的基本概念

    DB:表 视图 索引 存储过程 触发器 Events
    DB可以想象成为一个文件夹,表就是文件夹里面的excel文件
    一般作为大数据开发来说只需要掌握前面三个就可以了

    字段类型

    1.数值类型
    int 整数
    long 长整数

    float 单精度
    double 双精度
    decimal 小数值(一般和钱挂钩)

    2.字符串类型
    char 定常字符串(0-255)
    varchar 变长字符串(0-65535)
    text

    3.日期和时间
    date YYYY-MM-DD
    time HH:MM:SS
    datetime YYYY-MM-DD HH:MM:SS
    timestamp YYYY-MM-DD HH:MM:SS

    常规命令:

    使用某个数据库

    use 数据库名称;
    

    查看数据库下面的所有表

    show tables;
    

    查看某个表的结构

    show create table 表名称;
    

    创建数据表

    create table student_info(
    id int auto_increment primary key,
    num int,
    name varchar(200),
    age int,
    create_time timestamp default current_timestamp,
    create_user varchar(100),
    update_time timestamp default current_timestamp on update current_timestamp,
    update_user varchar(100)
    )CHARSET=utf8;
    

    特别注意:
    1.id 一定要自增长 非业务字段
    2.创建时间 创建人 修改时间 修改人
    3.规范 统一风格
    4.千万不要用 汉语拼音或首字母 做 字段名称
    5.千万不要用中文 做字段名称

    删除表

    drop table student_info;
    

    插入数据

    insert into student_info(num,name,age) values(1,'alvin',18);
    

    更新数据

    在生产上,要切记是否要加上where

    update student_info set age = 25 where name = 'alvin';
    

    查询数据

    1.基本语法

    select * from student_info;
    select name from student_info;
    

    2.条件查询
    > < = or and

    select * from student_info  where age <25;
    select * from student_info  age=25 and name='alvin';
    

    3.模糊查询

    以a开头的
    select * from student_info where name like 'a%';
    含有u的
    select * from student_info where name like '%u%';
    以n结尾的
    select * from student_info where name like '%n';
    第三个字母是v的(_代表一个占位符)
    select * from student_info where name like '__v%';
    

    删除数据

    在生产上,要切记是否要加上where

    delete from student_info where age=25;
    

    约束

    主键:一张表 就只能1个主键== 非空约束+唯一约束,主键可以有多个字段组成
    唯一约束:也可以由多个字段组成

    ALTER TABLE   student_info  ADD unique(name,num) 
    insert into student_info(num,name,age) values(1,'alvin',6);
    SQL 错误 [1062] [23000]: Duplicate entry 'alvin-1' for key 'name'
    因为有有条记录的name=‘alvin’和num=1了
    

    常见错误

    1.插入字段数量与表的字段数量不匹配

    SQL 错误 [1136] [21S01]: Column count doesn't match value count at row 1
    

    2.唯一约束

    SQL 错误 [1062] [23000]: Duplicate entry 'alvin-1' for key 'name'
    

    3.非空字段,没有指定值

    SQL 错误 [1364] [HY000]: Field 'num' doesn't have a default value
    

    相关文章

      网友评论

          本文标题:Mysql基本概念和Sql基本语法

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