• mysql 基础语法


    连接数据库操作

    # 连接 mysql
    mysql -h 地址 -P 端口 -u 用户名 -p 密码
    例如: mysql -h 127.0.0.1 -P 3306 -u root -p ****
    
    # 退出mysql
    exit;
    

    数据库操作

    # 查看数据库
    show databases
    
    # 创建数据库
     create database `create`
    
    # 显示数据库创建信息
    show database 数据库名
    
    # 删除数据库
    drop database 数据库名
    
    # 进入数据库
    use 数据库名
    
    # 显示当前的数据库
    select database();
    

    表操作

    # 创建表
    create table 表名(
    字段1  字段1类型 [字段选项],
    字段2  字段2类型 [字段选项],
    字段n  字段n类型 [字段选项]
    )表选项信息;
    
    例如: create table test(
      id int(10) unsigned not null auto_increment comment 'id',
      content varchar(100) not null default '' comment '内容',
      time int(10) not null default 0 comment '时间',
      primary key (id)
    )engine=InnoDB default charset=utf8 comment='测试表';
    
    语法解析(下文MySQL列属性单独解析):
    如果不想字段为NULL可以设置字段的属性为NOT NUL,在操作数据库时如果输入该字段的数据为NULL,就会报错.
    AUTO_INCREMENT定义列为自增的属性,一般用于主键,数值会自动加1.
    PRIMARY KEY关键字用于定义列为主键.可以使用多列来定义主键,列间以逗号分隔.
    ENGINE 设置存储引擎,CHARSET 设置编码, comment 备注信息.
    
    # 查看当前有哪些表
    show tables;
    
    # 查看表结构
    desc 表名
    
    # 更新表名
     alter table emp rename users;
    
    # 关键字查询/模糊查询
    show tables like '模糊查询表名%';
    
    #  查看表的创建语句
    show create table 表名;
    
    # 删除表
    drop table [if exists] 表名
    例如: drop table if exists test;
    
    # 修改表名
    alter table 旧表名 rename to 新表名;
    
    # 新增字段
    alter table 表名 add 新列名 字段类型 [字段选项];
    例如: alter table test add name char(10) not null default '' comment '名字';
    
    # 删除字段
    alter table 表名 drop 字段名;
    例如: alter table test drop content;
    
    # 修改字段类型
    alter table 表名 modify 字段名 新的字段类型 [新的字段选项];
    例如: alter table test modify name varchar(100) not null default 'admin' comment '修改后名字';
    
    # 重命名字段
    alter table 表名 change 原字段名 新字段名 新的字段类型 [新的字段选项];
    例如: alter table test change name username varchar(50) not null default '' comment '用户名字';
    
    # 插入 数据
    insert into 表名(字段列表) values(值列表);
    
    # 查询数据
    select *[字段列表] from 表名[查询条件];
    例如: select * from user;--查全部字段用*代替
    
    select name from user where age>0;--查name字段,age大于0
    
    # 删除数据
    delete from 表名[删除条件];
    例如: delete from user where age<1;--删除age小于1数据
    
    # 修改数据 
    update 表名 set 字段1=新值1,字段n=新值n [修改条件];
    例如: update user set age=100 where name='admin_a';
    
    # 数据排序
    SELECT 字段 FROM 表名 ORDER BY COUNT DESC
    
    # 数据去重
    SELECT DISTINCT 字段 FROM 表名
    
    
  • 相关阅读:
    Cordova 混合开发
    可能比文档还详细--VueRouter完全指北
    VUE项目实现页面跳转
    Vuex结合 async/await 优雅的管理接口请求
    vuex深入理解 modules
    vuex学习与实践——mapState、getter、mapGetters
    vuex里mapState,mapGetters使用详解
    vue.js相关UI组件收集
    vuex最简单、最直白、最全的入门文档
    深入理解表单脚本系列第三篇——选择文本
  • 原文地址:https://www.cnblogs.com/yimeng123/p/15339037.html
Copyright © 2020-2023  润新知