1.创建数据库
一、Oracle数据库操作
create database 数据库名称
1、创建数据库
2.删除数据库
create database databasename
drop database 数据库名称
2、删除数据库
3.备份sql server
drop database dbname
创建备份数据的device
3、备份数据库
use master
- 完全备份
exec sp_addumpdevice '名称','新的名称','路径'
exp demo/demo@orcl buffer=1024 file=d:back.dmp full=y
开始备份
demo:用户名、密码
backup database pubs to 新的名称
buffer: 缓存大小
4.创建表
file: 具体的备份文件地址
create table 表名(列名1 类型,列名2 类型)
** full:** 是否导出全部文件
5.根据已有表创建新表
** ignore:** 忽略错误,如果表已经存在,则也是覆盖
create table 新表名称 like 旧表名称
- 将数据库中system用户与sys用户的表导出
create table 新表名称 as select 列名 from 旧表名称 defintion only
exp demo/demo@orcl file=d:backup1.dmp owner=(system,sys)
6. 增加一个列
- 导出指定的表
Alter table 表名称 add 列名称 数据类型
exp demo/demo@orcl file=d:backup2.dmp tables=(teachers,students)
7.添加主键
- 按过滤条件,导出
alter table 表名称 add primary key(列名称)
exp demo/demo@orcl file=d:back.dmp tables=(table1) query=" where filed1 like 'fg%'"
8.自增id属性从1开始每次加1
导出时可以进行压缩;命令后面 加上 compress=y ;如果需要日志,后面: log=d:log.txt
identity (1,1)
- 备份远程服务器的数据库
9.创建索引
exp 用户名/密码@远程的IP:端口/实例 file=存放的位置:文件名称.dmp full=y
create index 索引名 on 表名(列名)
4、数据库还原
10.删除索引
打开cmd直接执行如下命令,不用再登陆sqlplus。
drop index idx_name
- 完整还原
vnsc5858威尼斯城官网,11.CTE查询
imp demo/demo@orcl file=d:back.dmp full=y ignore=y log=D:implog.txt
;with t as(select openid,ROW_NUMBER()over(partition by openid order byopenid)as rowfrom #temp)delete t where row1
指定log很重要,便于分析错误进行补救。
12.case when的用法
- 导入指定表
update pnset pn.id=case when t.id'' then t.id else pn.id endoutput deleted.id into 临时表from 表名称 pn with(nolock)join #temp t
imp demo/demo@orcl file=d:backup2.dmp tables=(teachers,students)
13.查询、插入、删除、求和、平均、最大值
- 还原到远程服务器
select * from tableinsert into new_table(id,name)values(1,'张三')delete from table where 范围select sum(field1) as sumvalue from table1select avg (field1) as avgvalue from table1select max(field1) as maxvalue from table1
imp 用户名/密码@远程的IP:端口/实例 file=存放的位置:文件名称.dmp full=y
通过以上内容给大家详解Sql基础语法,希望本文介绍能够给大家带来帮助。
二、Oracle表操作
1、创建表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
根据已有的表创建新表:
A:select * into table_new from table_old (使用旧表创建新表)
B:create table tab_new as select col1,col2… from tab_old definition only<仅适用于Oracle>
2、删除表
drop table tabname
3、重命名表
说明:alter table 表名 rename to 新表名
eg:alter table tablename rename to newtablename
4、增加字段
说明:alter table 表名 add (字段名 字段类型 默认值 是否为空);
例:alter table tablename add (ID int);
eg:alter table tablename add (ID varchar2(30) default '空' not null);
5、修改字段
说明:alter table 表名 modify (字段名 字段类型 默认值 是否为空);
eg:alter table tablename modify (ID number(4));
6、重名字段
说明:alter table 表名 rename column 列名 to 新列名 (其中:column是关键字)
eg:alter table tablename rename column ID to newID;
7、删除字段
编辑:计算机教程 本文来源:详解Sql基础语法
关键词: