oracel学习笔记基础篇

                                                                                                   oracel基础
创建用户:

create user 用户名 identified by 密码;//其中密码不能数字开头

例如:创建用户:sunday 密码:sunday

create user sunday identified by sunday;

用户授权:
grant 权限 to 用户名
例如:把connect,resource权限给sunday
grant connect,resource to sunday;

创建表:
create table 表名(字段名+字段类型+长度+约束);//字段间用逗号隔开,注意中英文的问题。

例如:创建用户表,里面包含用户id和用户名。

Create table tb1_user(
id number(5) primary key,
name varchar(8) not null);



        对数据库的增删改查(CURD)
-------------------------------------------------------------------

部门表tb1_dept
Id name city

create table tbl_dept(
id number(5) primary key,
name varchar2(10) not null,
city varchar2(10) not null
);
注意:由于tbl_dept表的id会作为tbl_emp表的外键,所以应该先创建tbl_dept表。

员工表

Tb1_emp
id,name,age,gender,dep_id
create table tbl_emp(
id number(5) primary key,
name varchar2(10) not null,
dept_id number(10) reference tbl_dept(id),
age number(2) not null,
gender varchar2(10)
);
注意:reference是依赖的意思,前面加了foreign就是表级依赖了,不加就是列级依赖。


DML:插入数据
Insert into tbl_dept values(1,‘sale’,’南昌’);

更新数据

update 表名 set 字段值=字段值的新值 where 条件


例如:更新部门id=1的id和name

update tbl_dept set id=2,name='teacher' where id=1;


删除数据:

delete from 表名 where id=1;

delete from tbl_salary where id=1;


查询数据:
1.简单查询
select* from tbl_emp;


2.条件查询
select id,name,from tbl_dept where id =1;


3.排序查询
select id,name,from tb1_dept order by id desc;

关键字:order by +字段名+排序方式
order by 一定是在查询语句的最后面。
排序方式:升序:asc 降序:desc
(聊天软件:desc date)

4.分组查询 组函数
avg() max() min() sum()
关键字:group by dept_id
select avg(salary) from tbl_salary group by dept_id;


----------------------------------------------------------------------


工资表tbl_salary
id,name,dept_id,salary

create table tbl_salary(id number(5) primary key,name varchar2(8) not null,dept_id number(8) not null,salary number(5) not null);

insert into tbl_salary values(1,'小明',1,1000);

insert into tbl_salary values(2,'小明',1,600);

insert into tbl_salary values(3,'小明',4,1700);

insert into tbl_salary values(4,'小明',1,1090);



//查询id为4的员工工资
select* from tbl_salary where id =4;


//按工资进行排序(降序)
select* from tbl_salary order by salsry desc;



//查询每个部门的平均工资
select avg(salary) from tbl_salary group by dept_id;


//查询平均工资大于100的部门id,并按平均工资降序排列
select dept_id from tbl_salary group by dept_id having avg(salary)>100 order by avg(salary)desc;


注意:having要和group by 连用,有having一定要有group by,但又group by不一定有having,group by只是一个帅选条件。

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

Create table 表名 字段名 数据类型 约束名
表名的命名规则:A-Z(a-z,0-9)不超过30个字符
数据类型:
Number(5) 5位的整数
Number(5,2)表示小数点后有两位的5位数
字符类型
Char(4) 4个字符,保存固定长度的字符,虽然浪费了内存,但效率高(按段匹配,只要匹配一次)
Varchar(5) 保存长度固定的字符,虽然节约了内存,但效率低(按位匹配,匹配多次)

Varchar2(2)oracle独有的数据类型,建议使用
Clob:可以保存2G的数据

日期类型new Date();util sql
SimpleDateFormat.format()
二进制数据:
Bolb:保存二进制文件

约束条件:
1.    primary key:主键约束 非空唯一
2.    foreign  key:外键约束 可以为空,一旦不为空,多为其他表的主键
3.    not null:指定字段不为空
4.    unique:唯一
5.    default : 默认
0和null(表示没有)以及“”(表示集合为空)是一个不一样的概念,
数据库里面“”表示转义
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章