sql 常用语句

常用sql语句

1、 建表 create


create table table_name (                         
    column_name datatype [null|not null],         
    column_name datatype [null|not null],
    ...
    [constraint]
)

2、删除 drop

drop table table_name;

3、修改(添加列,修改列名,删除列) alter


alter table table_name 
add column_name | modify column_name | drop column column_name;

4、插入数据 insert


insert into xxx_table_name
  (colunm_name1, colunm_name2)
values
  (x1,x2);


5、更新数据 update


update xxx_table_name
   set colunm_name1 = 'a1',
       colunm_name2 = 'a2',
where id = '1';

6、删除数据 delete


delete xxx_table_name
 where id = '1';


7、查询数据 select


select id, name from xxx_table_name where id = '1'


8、清空表数据

// 无条件清空,速度比deletetruncate table table_name;

9、插入数据


alter table table_name 
add column_name | modify column_name | drop column column_name;

10、展示表结构


show create table table_name; 

11、查看锁表情况


select l.session_id sid,
       s.serial#,
       l.locked_mode,
       l.oracle_username,
       l.os_user_name,
       s.machine,
       s.terminal,
       o.object_name,
       s.logon_time
  FROM v$locked_object l, all_objects o, v$session s
 WHERE l.object_id = o.object_id
   AND l.session_id = s.sid
--and o.object_name='table_name'       --object_name 表示表名
 ORDER BY sid, s.serial#;


12、解锁


alter system kill session 'sid,serial#';  --其中sid和serial#由1中查出


13、子查询


select id,name from table_name_a where id in (select d_id from table_name_b ) ;


14、连接

# 笛卡尔积
select * from course a,sc b where a.cno=b.cno;

# 内连接 (公共部分,与笛卡尔积写法等效)
select * from course a inner join sc b
on a.cno=b.cno;

# 左连接 (显示左表所有数据,右表无以null表示)
select * from course a left join sc b
on a.cno=b.cno;

# 右连接 (显示右表所有数据,左表无以null表示)
select * from course a right join sc b
on a.cno=b.cno;

15、between .. and


select * from table1 where time between time1 and time2; --限制查询数据范围时包括了边界值
select a,b,c, from table1 where a not between 数值1 and 数值2;--限制查询数据范围时不包括边界


16、in (等同于or分开写)


select * from table1 where a [not] in ('值1','值2','值4','值6');


17、limit [offset,] rows


# 默认偏移量0(可选)+ 偏移行数 行数选择从n+1开始
select * from table_name limit 1,3;  // 检索记录行 2-4行

# 分页 page页码
select * from table limit (page-1)*pageSize,pageSize; 

18、 union 、union all、 intersect


# 区别:union会过滤掉重复行,union all 返回所有,包含重复行,效率高于union
select aid,title from article union select bid,title from blog;
select aid,title from article union select bid,title from blog;


--只返回两个查询都有的行
select aid,title from article intersect select bid,title from blog;


19、根据部分字段去重

select distinct name,sex from table;

20、模糊查询


select * from table where value like '%abc%'

21、explain关键字

# 获取sql执行信息
explain select * fromselect id from order_info where pid=21231 order by modified desc limit 99000,1000as temp join order_info where temp.id = order_info.id

22、索引操作


# 创建索引
create unique index index_id on test2(id);

# 删除索引
drop index index_id ;

# 查看索引
show index from your_tablename;

23、存储过程(造数)含无参/有参

# 创建
CREATE OR REPLACE procedure procedure_name	(
	    [ ( argment [ { IN | IN OUT }] Type,
		     argment [ { IN | OUT | IN OUT } ] Type ]  --可以有多个输入参数和输出参数
	) IS
	    --定义变量和创建游标
	BEGIN
	    --执行游标、语句或调用其他过程等
	     commit; 
	exception //异常处理
	   when others then
	   	   rollback;
	   	   dbms_output.put_line(sqlcode);
	   	   dbms_output.put_line(substr(sqlerrm, 1, 512));
END procedure_name;


# 执行
execute  procedure_name;

# 删除
drop  procedure procedure_name;

# 检查是否有错误
show errors procedure procedure_name;


24、存储过程和函数的区别
存储过程:

  • 是编译好的PL/SQL块,有自己的名称,保存在数据库中可以被调用执行
  • 参数有in,out,inout三种或者不带参数(无入参无返回值),存储过程声明时不需要返回类型。
  • 可以返回参数,如记录集

函数:

  • 函数是由一个或多个 SQL 语句组成的子程序,函数可以在SQL语句中调用
  • 函数参数只有in,而函数需要描述返回类型,且函数中必须包含一个有效的return语句
posted @   鱼的记忆·  阅读(100)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示