zoukankan      html  css  js  c++  java
  • MySQL问题

    1、重置id问题

    由于在使用mysql,设计表的时候,设置了id自增,然后删除了数据后,再次新增数据时,就会出现id累计的情况,重置清空id,可以使用truncate

    #重置清空id,让id从1开始自增
    truncate table t_student

    2、Insert ignore的使用

    表要求有:primary key,或者有unique索引

     Insert ignore会忽略已存在的数据

    insert ignore into t_student(name,age,class) values("test",19,"计算机");
    replace into配合唯一索引使用,如果数据存在则更新,不存在则插入
    replace INTO `pgc_dashboard`.`t_ads_offline_add_vip_belongs`(`tab_type`, `project_name`, `user_cnt`, `log_date`) 
    VALUES (1, '天官赐福', 1000, '2020-11-22') 

    3、MySQL的表数据的导入导出

    #先把测试环境的数据导出到sql文件
    mysqldump -h192.168.1.1 -uadmin -padmin demo user_account > /opt/demo/dmp/user_account.sql
    #然后导入到生产环境
    mysql -h192.161.1.215 -uadmin -padmin demo < /opt/dmp/user_account.sql

    4、服务器上访问mysql

    mysql -h127.0.0.1 -uroot -proot -A

    5、mysql添加字段

    alter table t_operation_item add parent_item_id int(10) not null Default 0 comment'沉浸式banner的父id';
    ALTER TABLE t_contract_permission
     ADD end_type int(10) not null default 0 comment'结束时间选择类型,1-日期,2-永久,3-授权时限',
     ADD end_num int(10) not null default 0 comment'授权期(年)',
     ADD policy_group_id int(10) not null default 0 comment'策略组id'
     ;

    6、修改mysql表字段

    alter table t_pay_info change presell presell_json VARCHAR(255) not null DEFAULT "" COMMENT "预售相关信息json";  

    7、更新数据

    update t_contract_company set name='test' where id=1;

    8、增加删除索引

    --删除索引
    ALTER TABLE t_ads_offline_lose_vip_belongs
    DROP INDEX unx;
    --创建普通索引
    ALTER  TABLE  t_ads_offline_add_vip_belongs  ADD  INDEX mtime (  `mtime`  );
    --创建唯一索引
    ALTER  TABLE  `t_ads_offline_lose_vip_belongs`  
    ADD  UNIQUE KEY `idx_uk` (`tab_type`, `project_name`,`log_date`);

    9、建立表的sql

    CREATE TABLE `t_dim_season_title` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `season_id` int(11) NOT NULL DEFAULT 0 COMMENT 'season_id',
    `title` varchar(255) unsigned NOT NULL DEFAULT 0 COMMENT '名称',
    `ctime` timestamp NOT NULL DEFAULT current_timestamp() COMMENT '创建时间',
    `mtime` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() COMMENT '更新时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `idx_uk`(`season_id`) USING BTREE,
    KEY `ix_mtime` (`mtime`) USING BTREE
    ) ENGINE=InnoDB AUTO_INCREMENT DEFAULT CHARSET=utf8 COMMENT='season维度表'

    10、修改mysql字段的默认值

    ALTER TABLE r_season_pay_right_config ALTER time_limit SET DEFAULT 172800;

    11、Mysql的row_number函数来查topK

    题目描述

    SELECT
        t.dept_no,
        t.emp_no,
        t.salary 
    FROM
        (
        SELECT
            de.dept_no,
            de.emp_no,
            de.salary,
            row_number ( ) over ( PARTITION BY de.dept_no ORDER BY de.salary DESC ) AS rk 
        FROM
            ( SELECT a.emp_no, a.dept_no, b.salary FROM dept_emp a INNER JOIN salaries b ON a.emp_no = b.emp_no ) de 
        ) t 
    WHERE
        t.rk = 1;
  • 相关阅读:
    使用tcmalloc编译启动时宕机
    使用tcmalloc编译出现undefined reference to `sem_init'
    使用AddressSanitizer做内存分析(一)——入门篇
    VIM-美化你的标签栏
    Entity Framework Code First (六)存储过程
    Entity Framework Code First (五)Fluent API
    Entity Framework Code First (四)Fluent API
    Entity Framework Code First (三)Data Annotations
    Entity Framework Code First (二)Custom Conventions
    Entity Framework Code First (一)Conventions
  • 原文地址:https://www.cnblogs.com/drq1/p/14545171.html
Copyright © 2011-2022 走看看