数据库学习遇到的问题

来源:互联网 发布:java base32编码方法 编辑:程序博客网 时间:2024/05/19 02:21

1.完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例。 
1) 数据库是一系列物理文件的集合(数据文件,控制文件,联机日志,参数文件等); 
2) Oracle数据库实例则是一组Oracle后台进程/线程以及在服务器分配的共享内存区。

 

 

2.创建表:

语法:

Create table 表名(

字段名1 字段类型  是否为空

字段名2 字段类型  是否为空

例子:

Create tabletamp(

Id number(2)  not null primarykey,

Tname varchar2 not null

)

3.增加主键

Alter table 表名 add constraint 主键名 primary key (字段1)

4.增加外键

Alter table 表名

add constraint 外键名 foreign key (字段1)

Reference 关联表 (字段2)

 

5.建立表格时就指定主外键:

create tabletemp(

id number(9) not null

constraint temp_id_pk primary key;

name varchar2(9) not null ;

)

6.查询表:

Select *from 表名

删除表:

Drop table 表名

修改表名:

Rename 表名1  to  表名2;

显示表结构:

Describe 表名

插入:

Insert into 表名 value ()

Commit;

更新;

Update 表名 set 列名=新值 where 列名 = 某值;

 

 

7.Sql 中最重要的语句

Create database 创建数据库

Alter database 修改数据库

Create table    创建表

Alter table

Drop table

Create index 创建索引

Drop index

 

 

8.Select

select distinct 关键词 DISTINCT用于返回唯一不同的值。

语法:select distinct列名 from表名;

 

9.Where

select * from 表名 where 列名 运算符 某值

OR 和   AND运算符

Select * from persons where firstname = ‘carter’ andlastname = ‘tomaos’;

Selsct * from persons where firstname = ‘carter’ orlastname = ‘william’;

OR和 AND合并使用

Select * from persons where (firstname = ‘carter’or lastname = ‘william’)

      And lastname = ‘tomaos’;

 

10.ORDER BY 语句用于根据指定的列对结果集进行排序。

ORDER BY 语句默认按照升序对记录进行排序。

如果您希望按照降序对记录进行排序,可以使用 DESC 关键字。

11.Insert into

语法:insert into 表名 values (值1,值2…)

      Insert into (列1,列2…) values (值1,值2…)

12.Update

Update 表名 set 列名 = 新值 where 列名= 某值

Commit;

Update persons set firstname = ‘a’ where lastname = ‘b’;

13.Delete

Delete from 表名 where 列名 = 某值;

14.Like

 语法:select * from 表名 where 列名 like ‘%’

%可以夹在前后 如: %lon%

Not like 不相似的

通配符:%   _  ^

 

 

 

15.Top:语法:SELECT *
FROM Persons
WHERE ROWNUM <= 5

Select *from 表名 where rownum < 5

16.Between………and…………

Select *from  table  where 列名 between value1 and value2

17.Alias(别名)

表的别名没整明白

 

18.将一个表中的数据复制到另外一张表中:

Create table table_new asselect * from table_old

Insert into table_newselect * from table_old

 

19.数据导出:
1
将数据库TEST完全导出,用户名system密码manager 导出到D:/daochu.dmp
exp system/manager@TEST file=d:/daochu.dmp full=y

 

 

 

 

 

 

 

 

 

 

 

 

20.创建表空间,表用户,分配角色,导入数据。

(1)用system登陆,密码一般在数据库创建时设置了oracle,创建表空间,创建语句如下:

create tablespace DSCK

datafile 'E:\oradata\dsck.dbf'

size 10240M;

--追加表空间

ALTER TABLESPACE "DSCK"   ADD DATAFILE 'E:\oradata\dsck1.dbf ' SIZE 2048M;

说明:红字部分为“DSCK”可以为“A6”、“DSCKBM”、“ZCZK”

--追加临时表空间大小

 ALTER DATABASE TEMPFILE
 
'/u01/app/oracle/oradata/GSP/temp02.dbf' RESIZE 2G;

 

 

(2)创建表用户及分配角色,脚本如下:

--建用户:

create user dsck identified by  "oracle"

default tablespace dsck

temporary tablespace temp; 

--赋权限:

grant aq_administrator_role to dsck;

grant connect to dsck;

grant dba to dsck;

grant resource to dsck;

-- Grant/Revoke system privileges

grant alter any materialized view to dsck;

grant create any materialized view to dsck;

grant create any table to dsck;

grant create any view to dsck;

grant create operator to dsck;

grant create procedure to dsck;

grant create profile to dsck;

grant create view to dsck;

grant delete any table to dsck;

grant drop any materialized view to dsck;

grant drop any table to dsck;

grant drop any view to dsck;

grant execute any operator to dsck;

grant execute any procedure to dsck;

grant global query rewrite to dsck;

grant insert any table to dsck;

grant manage tablespace to dsck;

grant on commit refresh to dsck;

grant query rewrite to dsck;

grant select any dictionary to dsck;

grant select any sequence to dsck;

grant select any table to dsck;

grant under any table to dsck;

grant under any view to dsck;

grant unlimited tablespace to dsck;

grant update any table to dsck;

(3)导入数据,脚本如下:

导入:

imp fxgl/oracle@srdsdc file=c:\data\fxgl.dmp full=y

数据泵导入:

impdp fxgl/oracle@srdsdc dumpfile=fxgl.dmp   full=y

数据泵导入:

impdp system/oracle11G dumpfile=fxgl.dmp   full=y

impdp 用户名/密码 dumpfile=fxgl.dmp   full=y

impdp fxgl/oracle dumpfile=fxgl_dp.dmp full=y

 

 21.“minus”直接翻译为中文是“减”的意思,在Oracle中也是用来做减法操作的,只不过它不是传统意义上对数字的减法,而是对查询结果集的减法。A minus B就意味着将结果集A去除结果集B中所包含的所有记录后的结果,即在A中存在,而在B中不存在的记录。

select no,name,age,level_no from t_user minus select no,name,age,level_no from t_user2;

拥有另外一个表t_user2,其拥有和t_user表一样的表结构,那么如下语句可以找出除id外,在t_user表中存在,而在t_user2表中不存在的记录

配合rownum可以实现查询数据

select * from area where rownum <= 8 minusselect * from area where rownum < 2;

使用集合减运算符minus,该操作返回在第一个select中出现而不在第二个select中出现的记录。

 

 

22.添加主键:

altertableperson addConstraintpk_person Primarykey(id_1)

 

创建表空间

create tablespace 地名_FXGL

datafile 'F:\oradata\地名缩写\地名缩写_fxgl.dbf'

size 25G;

autoextend on

next 500m maxsize unlimited

extent management local;

--追加表空间

ALTER TABLESPACE "DSCK"   ADD DATAFILE 'F:\oradata\地名缩写\地名缩写_fxgl.dbf' SIZE 2048M;

数据本导入(导入到指定表空间)

impdp fxgl_jz/oracle@orcl DIRECTORY=DATA_PUMP_DIR dumpfile=FXGL_2015-08-26.DMPREMAP_SCHEMA=fxgl:fxgl_jz << REMAP_TABLESPACE=USERS:FXGL >>

误删表空间物理文件导致数据库无法登陆的解决方法

http://www.docin.com/p-714510263.html

 

对象类型type创建失败的解决办法:

原因:oracle中每一个type都会分配出唯一哦oid编号,

解决方法:在导入时加入:transform=oid:n

 

 

服务器老报这个获取数据库连接出错的错误,重启服务就又能用了,但是过段时间又会报这个同样的错误!有遇到过这样问题的同志吗?借鉴下经验!错误如图下图

//必须在command window中打开执行

show parameter sessions;

show parameter processes;

show parameter open_cursors;

alter system set open_cursors=5000 scope=both;

alter system set processes=1000 scope=spfile;

alter system set sessions=1800 scope=spfile;

 

查询用户所在的表空间

select username,default_tablespacefrom dba_users orderby username;

 

0 0
原创粉丝点击