使用 alter table
语句可以实现以下功能
eg: 创建一个相册表
create table photo(
photoId number(12,0) primary key,
photoName varchar2(20) not null
)
语法:alert table 表名 add(属性名,属性)类型(长度));
eg: 给相册表添加两个属性,分别是
alter table photo add(photoTime date);
alter table photo add(photoDescribe varchar2(6));
语法: alter table 表名 modify(属性名 属性类型(长度));
eg: 把相册的文字描述长度增加
alter table photo modify(photoDescribe varchar2(20));
语法:alter table 表名 drop(属性名);
eg:删除相册文字描述列
alter table photo drop(photoDescribe);
语法:RENAME table_ name TO new_ table_ name;
rename table photo to photo1;
drop 会删除表名
语法:drop table xxx
truncate table photo; -- 只是截断表,表还存在
drop table photo; -- 删除表,表不存在了
使用一个表之前,我们可以先创建一个表
create table photo(
photoId number(12,0) primary key,
photoName varchar2(20) not null
)
基本语法:insert into 表名[字段1(), 字段2(), 字段3()]... values [值1,值2,值3....]
Tips: 但是这样一次只能插入一条数据
几种插入数据的方式:
insert into photo(photoId,varchar2) values(112233445522,'回忆')
insert into photo(photoId,varchar2) values(null,'回忆')
insert into photo(photoId,varchar2) values(empsequence.nextval,'回忆')
日期的字段是to_date("2019-10-10","yyyy-mm-dd");
create into 表名 (字段1,字段2,字段3...)
select (值1,值2,值3....) from dual
union (all)
select (值1,值2,值3....) from dual
union (all)
select (值1,值2,值3....) from dual
......
Tips:
update 表名
set 字段名1 = 值, 字段名2 = 值...
where 条件
eg1:更新表中某咧数据
update photo set photoId = 112233445566;
eg2: 更新表中满足条件的数据
update emp set address = "上海" where empno = 1003
基本语法:delete from 表名 where 条件
eg1:删除表中全部数据
delete from emp;
eg2:删除指定条件的数据,删除一整行的数据(Oracle 不能删除某一列的数据)
delete from emp where empno = 1111
DQL 即数据查询语言,用来查询数据的,包含 SELECT
基本语法: select *| from 具体的列名 别名 from 表名
Tips:
eg1: 查询某列数据
select ename,hiredate form emp;
eg2: 查询表中所有数据
select * from emp
create table tb_person( -- 创建表
pid varchar2(18),
name nvarchar2(200) not null unique,
age number(3) not null check(age between 0 and 150),
birthday date,
sex varchar2(2) default '男',
CONSTRAINT tb_person_pid_pk PRIMARY KEY(pid) --设置这个表中的某个字段为主键,并且可以自定义主键的名称
)
select * from tb_person --查询表的所有元素
drop table tb_person -- 删除表
insert into tb_person (pid,name,age,birthday,sex) values ('111111111111111112','张三1',99,to_date('1987-12-12','yyyy-MM-dd'),'女');
CREATE TABLE book -- 创建表
(
bid NUMBER PRIMARY KEY NOT NULL ,
bname VARCHAR(30) ,
bprice NUMBER(5,2) ,
pid VARCHAR2(18) REFERENCES tb_person(pid)
) ;
--插入数据的时候要先插入主表,在插入从表,而删除数据的时候必须要先删除从表的数据在删除主表的数据
insert into book (bid,bname,bprice,pid) values(1,'三国演义',32,'111111111111111112');
--模拟删除主表的数据
delete from tb_person where pid='111111111111111112'
delete from book where bid=1
select * from emp1 as select * from emp where 1 == 2
所有字段都会被赋值
create table A as select * from B; -- 把 B中的字段赋值给 A
select * from emp1 as select * from emp
alter table tb_teacher add(teacherTitle nvarchar2(4));
alter table tb_teacher modify(teacherTitle nvarchar2(8));
alter table teacher drop column teacherTitle --(删除某一列的数据)
-- 方式一:直接插入数据,根据字段插入数据
insert into tb_student (stuId,stuIdCard,stuGender,stuAge,tid) values(20171111034,320101197905156354,'男',19,2)
insert into tb_student (stuId,stuIdCard,stuGender,stuAge,tid) values(20171111024,120101197905156334,'女',19,3)
-- 方式二:插入数据,不指定字段,直接插入所有字段
insert into tb_student values(20171111033,320101197925156374,'男',20,3)
insert into tb_student values(20171111024,120101197905156334,'女',19,3)
insert into tb_student(stuId,stuIdCard,stuGender,stuAge,tid)
select 20171111034,320101197905156354,'男',19,2 from dual -- dual 不存在的虚拟表
union
select 20171111024,120101197905156334,'女',19,3 from dual
union
select 20171111004,510101197905157354,'女',21,8 from dual
union
select 20171111044,320101192605156354,'男',19,9 from dual
union
select 20171111043,320102192605176355,'男',20,10 from dual