常用的SQL

本文深入探讨了SQL数据库的基本操作方法,包括增加列、修改表名、调整列顺序、建立主键等常见操作,同时提供了数据库表的管理、查询和优化策略。还详细介绍了如何在Oracle和标准SQL环境中进行相应的数据库操作,以及使用SQL获取系统日期、执行模糊查询、查询重复记录等高级功能。此外,文章还涵盖了SQL中的索引管理、外键约束、唯一性校验等关键概念,旨在帮助开发者高效管理和优化数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/***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;

启动外键约束:
SET FOREIGN_KEY_CHECKS=1;
查看 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


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值