/***MySQL***/
增加列:
alter table table_name add column_name column_type;修改表名:
alter table table_name1 rename table_name2;
修改列属性:
alter table table_name modify column_name column_type;
修改列名:
alter table table_name change column_name1 column_name2 column_type;
删除列:
alter table table_name drop column_name ;
调整列顺序:
alter table table_name change column_name1 column_name1 colume_type1 after column_name2;
如果需要调整的列是主键并是自增的,指定column_type时要加上auto_increment,否则自增策略会丢失。
并且在指定column_type时,不要指定primary key,否则会出现"Field 'id' doesn't have a default value"的错误。
建立表之后设置主键自增:
alter table table_name change id_column id_column int not null auto_increment primary key;
如果出现 "Field 'id' doesn't have a default value"错误。
可以先把当前的主键删除,重新添加主键列,并设置为自动增长。
查看建表语句:
show create table table_user;
查看表数量:
select count(table_name) from information_schema.tables where table_schema='dbname'
查看表中的约束
select * from information_schema.table_constraints where table_name ='tableName'
查看表中的索引
show index from tablename 或show index from tablename from dbname;
查看建表语句
show create table tablename
添加主键约束:
alter table 表名 add constraint 主键(形如:PK_表名) primary key 表名(主键字段);
添加外键约束:
alter table 从表 add constraint 外键(形如:FK_从表_主表) foreign key 从表(外键字段)
删除主键约束:
alter table table_name drop primary key;
删除外键约束:
alter table 表名 drop foreign key 外键(区分大小写)
禁用外键约束:
SET FOREIGN_KEY_CHECKS=0;
查看 FOREIGN_KEY_CHECKS当前值:SELECT @@FOREIGN_KEY_CHECKS;
关闭唯一性校验 :
set unique_checks=0;
开启唯一性校验:
set unique_checks=1;
添加索引:alter table tableName add index indexName(columnName)
删除索引:alter table tableName drop index indexName
"/*=====================================================================*/"
/***Oracle***/
修改列名:alter table table_name rename column oldName to newName;
修改列类型(长度):alter table table_name modify(columnName type);
添加字段的语法:alter table table_name add (column datatype [default value][null/not null],….);
修改字段的语法:alter table table_name modify (column datatype [default value][null/not null],….);
删除字段的语法:alter table table_name drop (column);
重命名表名:alter table old_table_name rename to new_table_name;
创建备份表并插入数据:create table tab_bak as select * from tab;
创建相同的表结构:create table tab_bak as (select * from tab where 1=2);
向已有的表中插入数据:insert into tab_bak select * from tab;
获取系统当前日期:select sysdate from table_name;(table可为自定义表,或dual表)
获取系统当前指定格式时间:select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss' ) from table name;
查询重复记录:
select * from table_name where name in (select name from table group by name having
count(name) >=2)
模糊查询表名:
select table_name, tablespace_name,temporary from user_tables where table_name like '%keyword%'
查询出的列含义依次为:表名,所在表空间名,是否为临时表。
% 表示零个或多个字符
_ 表示单个字符
[] 表示范围[a-f]或集合[abcdef]的任何单个字符
[^] 表示不属于指定范围的[a-f] 或集合[abcdef]的单个字符 通常表示[^a-f] or [^abcdef]
查询所有表名 或当前用户所有表:
(1)select table_name from all_tables where owner='用户名'; (要注意用户名的大小写)
(2)select * from tab;
显示当前登陆用户:
select sys.login_user from dual
/***标准SQL***/
修改列类型(长度):
alter table table_name modify column columnName columnType