首页 > 数据库 >sql 常用语句

sql 常用语句

时间:2022-09-23 17:02:47浏览次数:40  
标签:语句 常用 name column sql table where id select

常用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、清空表数据

// 无条件清空,速度比delete快
truncate 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 * from (select id from order_info where pid=21231 order by modified desc limit 99000,1000)
as 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语句

标签:语句,常用,name,column,sql,table,where,id,select
From: https://www.cnblogs.com/hikk/p/16723308.html

相关文章

  • windows常用命令
    ifconfig/all获取获取域名、IP地址、DHCP服务器、网关、MAC地址、主机名nettime/domain查看域名、时间netview/domain查看域内所有共享netviewip查看对方局......
  • MySQL面试题
    目录Count(*),Count(1),Count(字段)哪个性能更高?Count(*),Count(1),Count(字段)哪个性能更高?count的作用统计符合查询条件的记录中,函数指定的参数不为NULL的记录有多......
  • GPU常用命令
    在终端执行程序时指定GPU$CUDA_VISIBLE_DEVICES=1pythonrun_file.py查看一台服务器上所有显卡上的使用者和GPU占用情况首先安装gpustatpipinstallgpustat安装......
  • Centos7设置postgresql数据库开机自启动
    前言PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下如果不知道具体的路径,可以用find命令进行查找。命令如下:[root@admin~]#find/-......
  • JavaWeb--MySQL约束、数据库设计、多表查询、事务--2022年9月22日
    第一节  约束1、概念A、约束是什么约束是作用于表中列上的规则,用于限制加入表的数据约束的存在保证了数据库中数据的正确性、......
  • 常用基本命令
    man获取帮助信息1)基本语法man[命令或配置文件]     (功能描述:获取帮助信息)2)显示说明信息功能NAME命令的名称和单行描述SYNOPSIS怎样......
  • 常用git命令
    1.命令行删除 node_modules 文件1)安装删除工具npm install rimraf -g2)执行删除命令rimraf node_modules2.更新远程分支git remote update origin --prune3.合......
  • Kettle 添加Mysql驱动后仍提示连接错误
    错误连接数据库[kettle_mysql]:org.pentaho.di.core.exception.KettleDatabaseException:ErroroccurredwhiletryingtoconnecttothedatabaseDriverclass'o......
  • SQL 中count(*)与count(列名)的区别,count(1) 与 count(*)
    https://blog.csdn.net/weixin_42248302/article/details/100895938https://www.jb51.net/article/198907.htm count(*)与count(列名)的区别首先count(*)与count(列名)......
  • for循环语句
    基本语法和定义:  例:inti;for(i=1;i<=5;i++){printf("\nhello!");}实现打印五行hello以下是流程图:  注意事项  2)的例子如下(上面的变种):int......