赞
踩
数据库————>数据库实例ORCL——>表空间(用户里面订单创建表)——>数据文件
地球 —————>中国————————————> 省份(人民)———————————————>土地山川河流
create tablespace 表空间的名称
datafile '文件路径(服务器上)',
size 大小,
autoextend on 自动扩展
next 每次扩展的大小
create tablespace itzheng -- 创建表空间名称
datafile 'c:\itzheng.dbf' -- 创建dbf文件
size 100m -- 设置文件大小100m
autoextend on -- 设置自动扩展
next 10m; -- 设置每次自动扩展10m
这条语句删除是逻辑关系并不会把原有的文件删除
drop tablespace itzheng;
成功删除
create tablespace itzheng
datafile 'c:\itzheng.dbf'
size 100m
autoextend on
next 10m;
如果直接删除该文件是无法删除的
如果要删除需要先删除表关系
create user 用户名
identified by 密码
default tablespace 表空间的名称
在itzheng表空间当中创建用户itzhenguser密码为root
create user itzhenguser
identified by root
default tablespace itzheng
登录用户itzhenguser
无法登录:缺少创建会话的权限,登录被拒绝
新创建的用户没有任何权限
授权 grant 角色 | 权限 to 用户
grant connect to itzhenguser;
登录itzhenguser
登录成功
grant dba to itzhenguser;
select * from scott.emp;
create table 表名(
列名 列的类型(列的约束),
列名 列的类型(列的约束)
);
varchar
,在Oracle当中,是目前是支持的,但是不保证以后还支持
varchar2(长度)
:可变字符长度 varchar2(10) hello
占5个字符
char(长度)
:固定长度字符 char(10) hello
占10个字符,用空格填充
number(总长度:小数长度) 数字类型
date 年月日时分秒
timestamp 时间戳,比date类型更加精确
04-SEP-20 06.48.53.991000 PM +08:00
create table test1(
name1 varchar2(10),
name2 char(10),
age number(2,3)
);
insert into test1(name1,name2) values ('hello','hello');
select * from test1;
可以通过点击光标的方式看出对应的数据的长度
第一种情况:可以匹配查询到test1当中的name1的数据
select * from test1 where name1 like 'hello';
第二种情况:不可以匹配查询到test2当中name2对应的数据,因为name2当中的数据是有长度的并且有空格的,
select * from test1 where name2 like 'hello';//查不出数据
匹配查询的时候需要在后放加上空格
select * from test1 where name2 like 'hello ';
上面在定义的时候,设置总长度为2小数位长度为3(age number(2,3)
)
所以下面在插入数据的时候,小数位
insert into test1(age) values(2.333);
LONG/CLOB
:可以存放一本小说。
BLOB
(二进制大对象):存放电影 Java存进去,再读取出来。
create table 表名称 as 查询语句;
复制了scott用户下的emp表
create table emp as select * from scott.emp;
在itzhenguser下查询emp表
select * from emp;
这种方式创建的表是有一个弊端的。
注意:只会复制表结构和表当中的数据,不会复制列的约束。
上面这种方式查询语句是没有任何结果的。
select * from scott.emp where 1=2;
如果查询语句有结果,就是复制 表结果和数据
如果查询语句没有结果,就是复制表结果
所以通过查询出来空语句来通过子查询创建的表是没有任何结果
复制空表的结果还是空
create table emp1 as select * from scott.emp where 1=2;
select * from emp1;
create table stu(
stuid number,
sname varchar(10)
);
DDL:数据定义语言,修改结构alter create drop truncate
DML:数据操纵语言,操纵表中的数据 insert update delete
DCL:数据控制语言,grant
DQL :select
在SQL Windows窗口当中
在Command Window窗口当中:修改成功
alter table stu add (mobile varchar2(11),
sex varchar2(2)
);
alter table stu modify sex varchar2(4);
修改成功
alter table stu drop column gender;
删除成功
alter table stu rename column sex to gender;
修改成功
rename stu to student;
drop table student;
列的约束:约束主要用来约束表当中数据的规则
create table student (
stuid number primary key,
sname varchar2(10) unique,
age varchar2(10) not null,
gender varchar2(4) check(gender in ('男','女'))
);
select * from student;
创建成功
insert into student values(1,'张三','31','男');
insert into student values(2,'李四','31','男');
select * from student;
--主键的约束违反
insert into student values(1,'张三','31','男');
insert into student values(1,'李四','31','男');
违反非空约束
insert into student values(1,'赵六',null,'男');
违反唯一约束
-- 唯一约束违反
insert into student values(1,'王五','31','男');
insert into student values(2,'赵六','31','男');
-- 检查约束
insert into student values(1,'李七','31','妖');
主要是用来约束从表A当中的记录,必须是存在于主表B当中的
create table category(
cid number primary key,
cname varchar2(20)
);
create table product(
pid number primary key,
pname varchar2(20),
cno number
);
在category表当中插入数据
insert into category values(1,'手机数码');
在没有外键约束的情况下,向product表当中插入没有意义的数据依旧可以插入
insert into product values(10,'锤子',11);
在category 表当中没有11这个对应的cid
alter table product add foreign key(cno) references category(cid);
因为在上面插入的数据就有错误
先删除对应的数据
使用TRUNCATE TABLE用于删除表中的所有行
然后在添加外键约束
alter table product add foreign key(cno) references category(cid);
再次插入
insert into product values(2,'锤子',11);--插入失败
在category 表当中没有11这个cid ,所以报错
insert into product values(10,'锤子',1);
product
插入上面的10,'锤子',11号
,先在主表中插入数据,在往子表当中插入数据先向category当中插入
insert into category values(11,'电脑办公');
再向product表当中插入数据
insert into product values(2,'锤子',11);--插入成功
insert into product values(11,'外星人',11)
drop table category;
表中记录被外键关联,无法删除
drop table category cascade constraint;
查询表结构
select * from category;
证明已经没有该表
drop table product;
这是上面创建的两个表都没有了
create table category(
cid number primary key,
cname varchar2(20)
);
create table product(
pid number primary key,
pname varchar2(20),
cno number
);
alter table product add foreign key(cno) references category(cid) on delete cascade;
--在删除的时候使用级联删除
insert into category values(2,'办公电脑');
insert into product values(11,'外星人',2);
使用级联删除
delete from category where cid = 2;
两张表都没有了数据
insert into 表名 values(所有列的值都要对应写上)
insert into 表名(列1,列2) values (值1,,值2);
insert into 表名 查询语句
insert into emp1 select * from emp where deptno = 10;
插入成功
update 表名 set 列名 = 列的值 [where 条件]
update emp1 set ename = 'HUAAN' where ename = 'KING';
修改成功:将KING的名字改为了HUAAN
delete from 表名 {where 条件}
删除empno 为7839的所在行的数据
delete from emp1 where empno = 7839;
delete | truncate |
---|---|
DML | DDL |
逐条删除 | 先删除表再创建表 |
支持事务操作 | 不支持事务操作 |
通常情况下执行效率较高 |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。