Home Database Mysql Tutorial oracle database basic statements

oracle database basic statements

Oct 23, 2019 pm 03:16 PM
oracle database

oracle database basic statements

oracle数据库基本语句

一、Oracle数据库操作

1、创建数据库

create database databasename
Copy after login

2、删除数据库

drop database dbname
Copy after login

3、备份数据库

完全备份

exp demo/demo@orcl buffer=1024 file=d:\back.dmp full=y
Copy after login

demo:用户名、密码

buffer: 缓存大小

file: 具体的备份文件地址

full: 是否导出全部文件

ignore: 忽略错误,如果表已经存在,则也是覆盖

将数据库中system用户与sys用户的表导出

exp demo/demo@orcl file=d:\backup\1.dmp owner=(system,sys)
Copy after login

导出指定的表

exp demo/demo@orcl file=d:\backup2.dmp tables=(teachers,students)
Copy after login

按过滤条件,导出

exp demo/demo@orcl file=d:\back.dmp tables=(table1) query=\" where filed1 like 'fg%'\"
Copy after login

导出时可以进行压缩;命令后面 加上 compress=y ;如果需要日志,后面: log=d:\log.txt

备份远程服务器的数据库

exp 用户名/密码@远程的IP:端口/实例 file=存放的位置:\文件名称.dmp full=y
Copy after login

4、数据库还原

打开cmd直接执行如下命令,不用再登陆sqlplus。

完整还原

imp demo/demo@orcl file=d:\back.dmp full=y ignore=y log=D:\implog.txt
Copy after login

指定log很重要,便于分析错误进行补救。

导入指定表

imp demo/demo@orcl file=d:\backup2.dmp tables=(teachers,students)

还原到远程服务器

imp 用户名/密码@远程的IP:端口/实例 file=存放的位置:\文件名称.dmp full=y
Copy after login

二、Oracle表操作

1、创建表

create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
Copy after login

根据已有的表创建新表:

A:select * into table_new from table_old (使用旧表创建新表)
B:create table tab_new as select col1,col2… from tab_old definition only<仅适用于Oracle>
Copy after login

2、删除表

drop table tabname
Copy after login

3、重命名表

alter table 表名 rename to 新表名
Copy after login

4、增加字段

alter table 表名 add (字段名 字段类型 默认值 是否为空);
例:alter table tablename add (ID int);
Copy after login

5、修改字段

alter table 表名 modify (字段名 字段类型 默认值 是否为空);
Copy after login

6、重名字段

alter table 表名 rename column 列名 to 新列名 (其中:column是关键字)
Copy after login

7、删除字段

说明:

alter table 表名 drop column 字段名;
Copy after login

8、添加主键

alter table tabname add primary key(col)
Copy after login

9、删除主键

alter table tabname drop primary key(col)
Copy after login

10、创建索引

create [unique] index idxname on tabname(col….)
Copy after login

11、删除索引

drop index idxname
Copy after login

注:索引是不可更改的,想更改必须删除重新建。

12、创建视图

create view viewname as select statement
Copy after login

13、删除视图

drop view viewname
Copy after login

三、Oracle操作数据

1、数据查询

select <列名> from <表名> [where <查询条件表达试>] [order by <排序的列名>[asc或desc]]
Copy after login

2、插入数据

insert into 表名 values(所有列的值);
insert into test values(1,&#39;zhangsan&#39;,20);
insert into 表名(列) values(对应的值);
insert into test(id,name) values(2,&#39;lisi&#39;);
Copy after login

3、更新数据

update 表 set 列=新的值 [where 条件] -->更新满足条件的记录
update test set name=&#39;zhangsan2&#39; where name=&#39;zhangsan&#39;
update 表 set 列=新的值 -->更新所有的数据
update test set age =20;
Copy after login

4、删除数据

delete from 表名 where 条件 -->删除满足条件的记录
delete from test where id = 1;
delete from test -->删除所有
commit; -->提交数据
rollback; -->回滚数据
delete方式可以恢复删除的数据,但是提交了,就没办法了 delete删除的时候,会记录日志 -->删除会很慢很慢
truncate table 表名
删除所有数据,不会影响表结构,不会记录日志,数据不能恢复 -->删除很快
drop table 表名
删除所有数据,包括表结构一并删除,不会记录日志,数据不能恢复-->删除很快
Copy after login

5、数据复制

表数据复制

insert into table1 (select * from table2);
Copy after login

复制表结构

create table table1 select * from table2 where 1>1;
Copy after login

复制表结构和数据

create table table1 select * from table2;
Copy after login

复制指定字段

create table table1 as select id, name from table2 where 1>1;
Copy after login

四、数据库复制命令

不同的数据库语法不同(SQL Server和Oracle为例),且复制包括目标表已存在和目标表不存在的情况,分别回答:

SQL Server中,如果目标表存在:

insert into 目标表select*from原表;
Copy after login

SQL Server中,,如果目标表不存在:

select*into 目标表from原表;(复制表结构和数据)
select*from 目标表from原表where1=0(只复制表结构)
Copy after login

Oracle中,如果目标表存在:

insert into 目标表select*from原表;
commit;
Copy after login

Oracle中,如果目标表不存在:

create table 目标表 as select * from 原表 where 1=0(只复制表结构)
create table 目标表 as select * from 原表;复制表结构和数据
Copy after login

推荐教程:Oracle教程

The above is the detailed content of oracle database basic statements. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create oracle dynamic sql How to create oracle dynamic sql Apr 12, 2025 am 06:06 AM

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values ​​to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to read the oracle awr report How to read the oracle awr report Apr 11, 2025 pm 09:45 PM

An AWR report is a report that displays database performance and activity snapshots. The interpretation steps include: identifying the date and time of the activity snapshot. View an overview of activities and resource consumption. Analyze session activities to find session types, resource consumption, and waiting events. Find potential performance bottlenecks such as slow SQL statements, resource contention, and I/O issues. View waiting events, identify and resolve them for performance. Analyze latch and memory usage patterns to identify memory issues that are causing performance issues.

How to use triggers for oracle How to use triggers for oracle Apr 11, 2025 pm 11:57 PM

Triggers in Oracle are stored procedures used to automatically perform operations after a specific event (insert, update, or delete). They are used in a variety of scenarios, including data verification, auditing, and data maintenance. When creating a trigger, you need to specify the trigger name, association table, trigger event, and trigger time. There are two types of triggers: the BEFORE trigger is fired before the operation, and the AFTER trigger is fired after the operation. For example, the BEFORE INSERT trigger ensures that the age column of the inserted row is not negative.

See all articles