关系型数据库基础操作(一)

显示所有的数据库

show databases;

指定使用的数据库

use mysql;

创建数据库

create database databasename;

删除数据库

drop database databasename;

创建表

create table 表名(字段名1 类型名1(类型长度) , 字段名2 类型名2(类型长度).........);

orcale 中数值类型为 number,包括 int,float,number 等
number(3,2)表示有三个有效数字,其中两位是小数,最大值为 9.99
number(3) 表示有三位有效数字,最大值为 999
name varchar2(20) Oracle 中字符串类型为 varchar2 而并不是 varchar
orcale 中没有自增,但有序列,要实现自增必须自己写序列,设置起点和终点,设置步调

例如:

create table sal(id number(3),name varchar(20));
CREATE TABLE els_column_define_update as SELECT * from els_column_define limit 0;
create table purchase_order_item_20230318_01 like purchase_order_item;
添加字段
alter table softdog add  status varchar(4) DEFAULT '0' COMMENT '加密狗状态0正常,1停用,默认正常';
修改字段的长度
alter table write_log modify column fail_reason varchar(255) NULL DEFAULT NULL COMMENT '失败原因';
修改字段的类型 (不能有记录的)
alter table student modify(name number(9));
删除一个字段
alter table student drop column name;
表的名字修改
rename student to stu;
字段如何改名字
先删除
alter table student drop column name;
再添加
alter table student add (salary number(7,2));

删除表

drop table 表名;

例如:

drop table sal;

在表中写入数据 (只有 values 没有 value)

部分插入:

insert into 表名(列名1,列名2,列名3)values(值1,值2,值3);

注意:如果列的类型是字符串,值要加 '' 相反 则不要加

全部插入:

insert into 表名 values(值1,值2,值3);

基于数据插入:

insert into els_column_define_update select * from els_column_define where table_code='PurchaseQuantityToleranceSetting' and els_account='1411141';

插入的值必须类型匹配,并且长度不能大于规定的字段的长度

例如:

insert into sel (name) values('a');
insert into sel values(9,'a');

修改数据

Update 表名 set 列名=值 where 字段名=值;

注意:如果不写 where 条件,会修改所有的信息

例如:

修改多字段时用, 隔开每个字段

update student set name='张三', sex='男', age=18, address='河南', detail='山东' where id=15;
update grade set name='which' where name='张si';

子查询可以使用特殊符号连接

update T_TASK_ASSIGN_INFOR  set TASKSTATUS='700809',STAGE='200512'   
where ID in ( select id from  T_TASK_ASSIGN_INFOR where PCH='80'  );
update T_BATCH set BATCHSTATUE='700801' where BATCHSTAGE='200512' 
and  AUDITER='jx' and (YWTYPE='600820' or  YWTYPE='600814');
update dept set (deptno,dname,loc)=(select f.deptno+1,f.dname||'1',f.loc||'1' from 
dept f where f.deptno=11) where deptno=11;

连表更新

alter table purchase_order_delivery_config  convert to CHARACTER SET utf8 COLLATE utf8_general_ci;
update purchase_order_delivery_config config,supplier_master_data maData set config.supplier_name=maData.supplier_name
where config.to_els_account=maData.to_els_account and config.supplier_name is null;
alter table purchase_order_delivery_config  convert to CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

删除数据

delete from 表名 where 字段名=值;

不加 'where 字段名 = 值' 会删除所有数据

例如:

delete from grade where name='张三';
delete from grade where id>3;

查询数据

Select  字段名1,字段名2,字段名3 from  表名;

注意:'*' 代表所有字段

例如:

select  * from grade;
select name from grade;
select name ,id from grade;

按条件查询

select * from 表名 where 字段名=值;

例如:

select * from orange where name='ale';
select * from student where id=5;
select ename,sal*12+1000 from emp;
select ename,(sal+1000)*12+1000 from emp;

运算符查询 (比较运算符)>,<,=

select * from emp where deptno>10;
select * from emp where deptno=10;

查询 (特殊符号)

and,or,between...and...,in,on,is,like

没有 & || ! 等符号

筛选有数据 1,数据 2 的项

select * from 表名 where 字段名 between数据1 and 数据2;
select * from 表名 where 字段名 in(数据1,数据2.......);
select * from 表名 where 字段名 表达式1 or  表达式2;
select * from 表名 where 字段名 条件语句1 and 字段名 条件语句2;
select * from 表名 where 字段名 条件语句1 ;

例如:

Select * from student where age between 20 and 25;
Select * from student where age in(20,23,25);
Select * from student where age>=20 or age <=30;
Select * from student where age>=20 and age <=30;
多特殊符号同时使用
select * from T_TASK_FORIDS where TASKSTATE='700414'
and  TASKID in ( select id from  T_TASK_ASSIGN_INFOR where PCH='80') ;
根据字段中字符串的长度筛选数据
select * from T_MIDENCOD_WORD where length(word)>1;
select * from student s where s.sno=183 order by nvl(length(trim(s.sname)),0) asc ;

-- asc 递增排序
-- nvl(attribute,default value) 当属性值为空时就将其值设默认值
-- length(attrbute) 得到字符串的长度
-- trim(attribute) 去掉字符的空格

下一篇 关系型数据库基础操作(二)