oracle常用语句大全
以下是一些Oracle数据库中常用的SQL语句,涵盖了查询、插入、更新、删除、创建表等基本数据库操作。请注意,具体的语句可能因数据库结构和需求而有所不同。
查询数据:
查询所有数据:SELECT * FROM table_name;
条件查询:SELECT column1, column2 FROM table_name WHERE condition;
排序查询:SELECT * FROM table_name ORDER BY column_name;
聚合函数(求和、平均值等):SELECT AVG(column_name) FROM table_name;
分组查询:SELECT column1, COUNT(column2) FROM table_name GROUP BY column1;
插入数据:
插入一行数据:INSERT INTO table_name (column1, column2) VALUES (value1, value2);
更新数据:
更新数据:UPDATE table_name SET column1 = value1 WHERE condition;
删除数据:
删除数据:DELETE FROM table_name WHERE condition;
创建表:
创建表:CREATE TABLE table_name (column1 datatype, column2 datatype);
主键约束:CREATE TABLE table_name (column1 datatype PRIMARY KEY, column2 datatype);
修改表结构:
添加列:ALTER TABLE table_name ADD column_name datatype;
修改列:ALTER TABLE table_name MODIFY column_name datatype;
删除表:
删除表:DROP TABLE table_name;
索引:
创建索引:CREATE INDEX index_name ON table_name (column_name);
删除索引:DROP INDEX index_name;
连接表:
内连接:SELECT * FROM table1 INNER JOIN table2 lumn_name = lumn_name;
外连接:SELECT * FROM table1 LEFT JOIN table2 lumn_name = lumn_name;
子查询:
单行子查询:SELECT column_name FROM table_name WHERE column_name = (SELECT column_name FROM another_table);
多行子查询:SELECT column_name FROM table_name WHERE column_name IN (SELECT column_name FROM another_table);regexp like
视图:
创建视图:CREATE VIEW view_name AS SELECT column1, column2 FROM table_name WHERE condition;
序列:
创建序列:CREATE SEQUENCE sequence_name START WITH 1 INCREMENT BY 1;
事务控制:
开始事务:START TRANSACTION;
提交事务:COMMIT;
回滚事务:ROLLBACK;
权限管理:
授权:GRANT SELECT ON table_name TO user_name;
撤销权限:REVOKE SELECT ON table_name FROM user_name;
函数和存储过程:
创建函数:CREATE FUNCTION function_name (parameters) RETURNS datatype AS BEGIN ... END;
创建存储过程:CREATE PROCEDURE procedure_name AS BEGIN ... END;
触发器:
创建触发器:CREATE TRIGGER trigger_name BEFORE INSERT ON table_name FOR EACH ROW BEGIN ... END;
同义词:
创建同义词:CREATE SYNONYM synonym_name FOR table_name;
数据类型转换:
TO_CHAR:SELECT TO_CHAR(date_column, 'MM/DD/YYYY') FROM table_name;
TO_NUMBER:SELECT TO_NUMBER(char_column) FROM table_name;
TO_DATE:SELECT TO_DATE(char_column, 'MM/DD/YYYY') FROM table_name;
时间和日期函数:
CURRENT_DATE:SELECT CURRENT_DATE FROM dual;
SYSDATE:SELECT SYSDATE FROM dual;
MONTHS_BETWEEN:SELECT MONTHS_BETWEEN(date1, date2) FROM table_name;
存储空间管理:
表空间大小查询:SELECT tablespace_name, SUM(bytes) FROM dba_free_space GROUP BY tablespace_name;
分页查询:
分页查询:SELECT * FROM (SELECT column1, column2, ROWNUM FROM table_name WHERE ROWNUM <= 10) WHERE ROWNUM >= 1;
复杂条件查询:
CASE语句:SELECT column1, column2, CASE WHEN condition THEN 'Result1' ELSE 'Result2' END FROM table_name;
外部表导入导出:
外部表导入:COPY FROM username/password@database_name - TO username/password@database_name CREATE table_name USING SELECT * FROM external_table_name;
字符串函数:
字符串拼接:SELECT CONCAT(string1, string2) FROM table_name;
字符串长度:SELECT LENGTH(string_column) FROM table_name;
字符串转小写:SELECT LOWER(string_column) FROM table_name;
正则表达式:
正则表达式匹配:SELECT * FROM table_name WHERE REGEXP_LIKE(column_name, 'pattern');
字符串截取:
SUBSTR:SELECT SUBSTR(string_column, start_position, length) FROM table_name;
数据导入导出:
数据导入:LOAD DATA INFILE 'file_path' INTO TABLE table_name;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论