关于在调试数据库时遇到的一些问题

首先说明一下:

建表时的default 与default null区别
default '' 指的是 默认空字符     default null 指的是 默认值为null      null和 '' 是有区别的

 mysql设计表时 建议不要用default NULL

 string类型的default ''      int类型的default 0

问题一:invalid default value for 'id'

出现这样的问题是在 数据库的建表语句里面id的属性给赋予了null

解决办法,把id的默认值改为一个空格

问题二:出现 tochar 类型的错误,

问题三:关于在字段名出现关键字如Key的时候 应该换成别的

问题四:利用SQL语句创建用户和密码

利用SQL语句创建用户
grant all privileges on HHH.* to 'HHH'@'%' identified by 'asdIAS@123' with grant option;
flush privileges;

出现错误Your password does not satisfy the current policy requirements
解决方法,把密码给设置成复杂一点的


grant all privileges on 数据库名.* to '用户名'@'%' identified by '密码' with grant option;
flush privileges;

问题五:关于oracle里面有索引,mysql里面没有索引的解决方案

关于oracle的索引信息

1.检查当前标的索引信息

select * from user_indexes where table_name='tablename' ; -- 将tablename替换为你的表名就即可 ,也可以不指定表名,直接查询出所有表的索引信息

2.根据索引名称,查询到标的索引列信息

select * from user_ind_columns where index_name=upper('I1PBASEPAPER');  -- I1PBASEPAPER是上面那条语句查询出来的 INDEX_NAME 值

3.一条SQL语句查询出索引相关信息,将tablename替换为需要的表名即可

select
  a.index_name
 , a.table_name
 , a.column_name
 ,b.index_type
 ,b.uniqueness
FROM all_ind_columns a, all_indexes b
WHERE a.index_name=b.index_name
AND a.table_name = upper('tablename')
ORDER BY a.table_name, a.index_name, a.column_position
;

4.存储过程,去除oracle表中的所有索引并且拼凑成mysql索引创建脚本输出

需要注意的点:

  唯一索引需要单独判断

  组合索引写法有点不一样,需要单独拼接

先来一段简单的,看看索引信息(第一段for里面的in (T1APL,tablename2)指定表名信息即可,多张表用逗号分割)

declare
begin
       for curidx in (select index_name,TABLE_NAME from user_indexes where table_name in('T1APL','tablename2')) loop
           dbms_output.put('索引:【表:'||curidx.TABLE_NAME||',     索引: '||curidx.index_name||',    索引列:');
           for curidxcol in (select * from user_ind_columns where index_name=upper(curidx.index_name)) loop
               dbms_output.put_line(curidxcol.column_name||'】');
           end loop;
       end loop;  
end;

5.万能脚本

declare
 idx_cnt int(10) ; -- 用于保存索引列的数量
begin
       for curidx in (select index_name,table_name,uniqueness from user_indexes where table_name in('tablename','T1APL')) loop
           select count(*) into idx_cnt from user_ind_columns where index_name=upper(curidx.index_name);
           if idx_cnt >1  -- 组合索引
           then
                if curidx.uniqueness = 'UNIQUE' then -- 判断是否为 唯一索引如果是就在创建索引的时候添加 unique 关键字
                     dbms_output.put('ALTER  TABLE  or_lifepro.'||curidx.table_name||' add '||curidx.uniqueness||' index '||curidx.index_name||' (');
                else 
                     dbms_output.put('ALTER  TABLE  or_lifepro.'||curidx.table_name||' add  index '||curidx.index_name||'(');
                end if;    
                for curidxcol in (select * from user_ind_columns where index_name=upper(curidx.index_name)) loop
                     idx_cnt := idx_cnt -1; -- 当前循环每执行一次,变量次数减1
                     if idx_cnt=0 -- 当前循环为最后一次循环时 使用括号结束索引创建语句
                     then
                         dbms_output.put_line(curidxcol.column_name||');');
                     else
                         dbms_output.put(curidxcol.column_name||',');
                     end if;
                end loop;
           else   -- 单列索引
                for curidxcol in (select * from user_ind_columns where index_name=upper(curidx.index_name)) loop
                    if curidx.uniqueness = 'UNIQUE' then -- 判断是否为 唯一索引如果是就在创建索引的时候添加 unique 关键字
                       dbms_output.put_line('ALTER  TABLE  or_lifepro.'||curidx.table_name||' add '||curidx.uniqueness||' index '||curidx.index_name||'('||curidxcol.column_name||');');
                    else 
                       dbms_output.put_line('ALTER  TABLE  or_lifepro.'||curidx.table_name||' add  index '||curidx.index_name||'('||curidxcol.column_name||');');
                    end if;    
                 
                end loop;
           end if;
       end loop;
         
end;

关于 MySQL的索引信息

1.mysql查看表索引语句

show index from tablename;

2.添加索引

ALTER table table_name ADD INDEX index_name (column1,column2,column3); -- 添加组合索引
ALTER table table_name ADD INDEX index_name (columnname); -- 添加普通索引
ALTER table table_name ADD UNIQUE INDEX index_name (columnname); -- 添加唯一索引

3.分析表,使得索引生效

analyze table tablename;

4.批量删除mysql表索引

先将表索引信息删除语句拼凑好,执行如下sql,更改你的tablename即可

SELECT i.TABLE_NAME, i.COLUMN_NAME, i.INDEX_NAME,
CONCAT('ALTER TABLE ',i.TABLE_NAME,' DROP INDEX ',i.INDEX_NAME,' ;')
FROM INFORMATION_SCHEMA.STATISTICS i where i.TABLE_NAME='tablename'
;

猜你喜欢

转载自blog.csdn.net/tony_yang6/article/details/108058755