数据类型
数据类型 | 表示 |
---|---|
数字 | number |
日期时间 | date |
字符串 | char(长度)/varchar2(长度) |
约束条件
名称 | 约束 |
---|---|
唯一 | unique |
非空约束 | not null |
主键约束 | primary key |
检查约束 | check(条件) |
外键约束/FK约束 | references 表(列) |
默认值约束 | default 值 |
创建表
表是数据库的重要组成部分,就跟我们的EXCLE表一样
每张表都会有自己的表名与列名
取名称的标准
- 不要以数字开头
- 名称中只有字母、数字、下划线
创建表语法:
create table 表名(
列名1 数据类型,
列名2 数据类型,
列名3 数据类型
);
eg:创建student表
create table student(
name varchar2(30),
age number,
sex char(2),
birthday date
);
注意,针对表数据的所有操作,数据是严格区分大小写的
新增数据:
insert into 表名(列1,列2) values(值1,值2);
values里面的值依次对应前面的列(值1对应列1...)
eg:往student表中新增一条 name为张三,age 为 18, sex 为 男的数据
insert into student(age,sex,name) values(18,'男','张三');
如果对表的所有列都增加数据
insert into 表名 values(值1,值2,值3....);
eg:往student表中新增一条 name为张三,age 为 18, sex 为 男 brithday 为 sysdate的数据
扫描二维码关注公众号,回复:
7530672 查看本文章
insert into student values('张三',18,'男',sysdate);
查询数据
select 列1,列2 from 表名
如果想要查询所有数据,用*表示
select * from 表名
eg: 查询student表中,所有学生的姓名和年龄
select name,age from student;
eg: 查询student表中,所有信息
select * from student;
- 带条件的查询语句,则还需要使用where 过滤条件
语法:
select 列1,列2 from 表 where 条件;
eg:查询student表中,年龄大于20的学生的姓名和性别
select name,age from student where age > 20;
如果涉及到多个条件时,则我们需要用到一些逻辑操作符(and/or)
逻辑操作符 | 作用 |
---|---|
and | and两边的条件需要同时满足 |
or | or两边的条件只需要满足一个即可 |
eg:查询student表中,年龄大于20,并且性别为男的学生的姓名和性别
select name,age from student where age>20 and sex='男';
修改数据
update 表名 set 列1=值1,列2=值2 where 条件
eg:将student表中,所有年龄小于20的学生,将其性别修改为女
update student set sex='女' where age < 20;
删除数据
删除表的全部数据
delete from 表
带条件的删除
eg:删除student表中,年龄小于20的学生
delete from student where age < 20;