--创建表:
create table tName(
id int,
userName char(10),
password char(10)
);
id int,
userName char(10),
password char(10)
);
//多写点没关系
create table student(
id int,
userName char(10),
password char(10),
address varchar(10),
userPhone char (13),
primary key(id)
);
//参数解释:
char: 定长 给的长度是多少 就分配多少空间,n介于 1 和 8,000 之间的数值,存储大小为 n个字节;索引效率高,程序里使用trim可去掉多余的空格
varchar 变长 100 可以释放空间,n介于 1 和 8,000 之间的数值。存储大小为输入数据的字节的实际长度,而不是 n个字节;索引效率较低
--drop 命令,与该命令拥有同样功能的还有delete和truncate。
// 三者区别在于:
drop语句执行删除过程会将表彻底删除,且不可恢复,表所占空间全部释放,恢复初始;
delete语句执行删除的过程是每次从表中删除一行,并且同时将该行的删除操作作为事务记录在日志中保存以便进行进行回滚操作;
truncate则一次性地从表中删除所有的数据并不把单独的删除操作记录记入日志保存,删除行是不能恢复的。并且在删除的过程中不会激活与表有关的删除触发器。执行速度快。
范围而较:drop > truncate > delete
delete和truncate只删除数据,而drop则删除整个表(结构和数据)。(PS : o(╯□╰)o这个不是本篇日志重点,不多作解释了...)
//用法示例:
drop database // 库名
drop table // 表名
--向表里面插入数据
//方式一:
insert into student values(1,'无忧','123','beijing','1393292392');
//方式二:个人觉得比较方便
insert into student(id,userName,password,address,userPhone)
select 1,'无忧','123456','地球','1354679854' union
select 2,'进阶',123456','火星','1354679855' union
select 3,'咖啡','123456','未知行星','1354679856'
select 1,'无忧','123456','地球','1354679854' union
select 2,'进阶',123456','火星','1354679855' union
select 3,'咖啡','123456','未知行星','1354679856'
--向表里面插入数据 指定列
insert into student (id,userName,password,address,userPhone) values(1,'zhangsan','123','beijing','1393292392');
insert into student (id,userName,password,address,userPhone) values(1,'zhangsan','123','beijing','1393292392');
select * from aa // * 代表所有字段名
select id from aa // 指定列查询
//也可以这么写:
select
id,
name
from
aa
id,
name
from
aa
select
id,
userName,
password,
address,
userPhone
from
student
//主要看个人代码习惯
--给列 和 表名 起别名1
select
id 用户ID,
userName 用户名,
password,
address,
userPhone
from
student a
给列 和 表名 起别名
select
id as 用户ID,
userName as 用户名,
password,
address,
userPhone
from
student as a
select
id as 用户ID,
userName as 用户名,
password,
address,
userPhone
from
student as a
where 语句(限制条件)
select
id,
userName,
password
address
from
student
where
id='1'
select
userName
from
student
where
id='1'
--双重条件限制 并且关系(and)
select
userName
from
student
where
id='1'
and
password='1'
--双重条件限制 或者关系(or)
select
userName
from
student
where
id='1'
or
password='2'
select
userName
from
student
where
id='1'
or
password='2'
in语句
select
*
from
student
where
id in(1,3,4,6);
select
*
from
student
where
id in(1,3,4,6);
--逻辑操作符
select
*
from
student
where id>1 and id<=5
select
*
from
student
where id>1 and id<=5
--between语句 注意事项:1、必须是数字类型 2、and 的左边 是小的数字 右边是大的数字
例:
select
*
from
student
where
id between 1 and 5
select
*
from
student
where
id between 1 and 5
--like 模糊查询 %任意匹配
select
*
from
student
where
password like '%3%'
*
from
student
where
password like '%3%'
--like 模糊查询 _单个字符匹配
select
*
from
student
where
password like '_3%';
select
*
from
student
where
password like '_3%';
--排序
select * from emp order by empno asc; // asc可以不写 不指定默认升序
select * from emp order by empno asc; // asc可以不写 不指定默认升序
select * from emp order by ename desc; // 降序
--去除重复
select distinct password from student
select distinct password from student
// 分页 语句 这个语句在oracle里面不好用
select * from emp limit 0, 10 从第0条查询 查询出十条记录
select * from emp limit 0, 10 从第0条查询 查询出十条记录
----------------------------------------------------------------------------------------
求平均值: select avg (字段名)
总记录数: select count (字段名)
最小值: select min (字段名)
最大值: select max (字段名)
求和: select sum (字段名)
总记录数: select count (字段名)
最小值: select min (字段名)
最大值: select max (字段名)
求和: select sum (字段名)
-------------------------------多表查询-------------------------------------------------------
第一种连表查询:
select
a.empno,
a.ename,
a.job,
a.deptno,
b.dname,
loc
from
emp a,
dept b
where a.deptno = b.deptno
select
a.empno,
a.ename,
a.job,
a.deptno,
b.dname,
loc
from
emp a,
dept b
where a.deptno = b.deptno
第二种连表查询:
select
a.empno,
a.ename,
a.job,
a.deptno,
b.dname,
b.loc
from
dept b
join
emp a
on
a.deptno = b.deptno
a.empno,
a.ename,
a.job,
a.deptno,
b.dname,
b.loc
from
dept b
join
emp a
on
a.deptno = b.deptno
第三种连表查询:
select
a.empno,
a.ename,
a.job,
a.deptno,
b.dname,
b.loc
from
emp a
right join
dept b
on
a.deptno = b.deptno
a.empno,
a.ename,
a.job,
a.deptno,
b.dname,
b.loc
from
emp a
right join
dept b
on
a.deptno = b.deptno
增 删 改 查
--------------------------删除-----------------------------
delet from student --删除所有
delet from student where id= '1'
delete from student where userName= '无忧';
delete from student where userName= '无忧' and id='1';
-----------------------------------------------------------
update student set userName='new',password = 'aaa' where id = '1'
-------------------------------------------------------------------------------
--建索引
--建索引
create index indexName on dept(dname); // indexName :索引名 dept:表名
create Unique index name on dept(dname); // 创建唯一索引 不指定默认是通用的
----------------------------------------------------------------------------------
--视图:视图是一张虚拟的表
创建视图的优点: 1 简化复杂的查询
2 不占用磁盘空间
不足之处:不支持子查询
2 不占用磁盘空间
不足之处:不支持子查询
例:
create view dept_emp
as
select
a.empno,
a.ename,
a.job,
a.deptno,
b.dname,
loc
from
emp a,
dept b
where a.deptno = b.deptno
create view dept_emp
as
select
a.empno,
a.ename,
a.job,
a.deptno,
b.dname,
loc
from
emp a,
dept b
where a.deptno = b.deptno
视图使用方法:select * from dept_emp;