MySQL, Oracle, PostgreSQL
create table dept(
depetno int primary key,
dname varchar(12) not null,
loc char(10)
);
create table emp(
empno int primary key,
ename char(10) not null,
deptno int,
hiredate date,
sal numeric(8,2),
comm numeric(8,2)
);
Output:
Table created
drop table emp;
再次删除会报错,为了保证它不出错,删除前判断
drop table if exists emp;
insert into dept values(1,'Development','New YORK');
insert into dept(deptno,dname,loc) values(2,'Testing','CHICAGO');
insert into dept(depto,dname) values(3,'Marketing')
select * from emp;
select empno,ename,deptno,hiredate,sal,come from emp;
select ename, sal from emp;
select ename, sal,sal*12 from emp;
select ename, sal, sal*12 as annual_salary from emp;
select ename "select" from emp;
select deptno from emp;
select DISTINCT deptno from emp;
select ename,sal,deptno,hiredate from emp where hiredate>='2010-01-01';
…
不等于的两种表示:
!=
<>
select * from emp where deptno=2 or (sal>10000 and hiredate>'2015-01-01')
select * from emp whre depto not 2
select ename, empto from emp where empno=3 or empno=4 or empno=5;
更简单 用in
select ename, empto from emp where empno in (3,4,5);
不在
select ename, empto from emp where empno not in (3,4,5);
select ename,hiredate from emp where hiredata>='2013-01-01' and hiredate<='2013-12-31';
select ename,hiredate from emp where hiredata between '2013-01-01' and '2013-12-31';
%
匹配零个,一个,多个字符_
匹配单个字符select * rom emp where ename like 'J%';
匹配有名字种有a的
select * from emp where ename like '%a%';
匹配第三个字符是a的,使用两个下划线
select * from emp where ename like '__a%';
select ename,deptno,sal from emp order by sal;
select ename,deptno,sal from emp order by sal desc;
select ename,deptno,sal from emp order by depto,sal;
select ename,deptno,sal from emp order by depto desc,sal asc;
-- xxxxxx
/* xxx
xxx
xxx */
select * from emp where empno=3;
update emp set deptno=1 where empno=3;
update emp set sal=sal+1000 where empno=3;
update emp set deptno=1, sal=sal+1000 where empno=3;
select deptno from dept where dname='Development';
update emp set sal=sal+1000 where deptno=1;
等价于
update emp set sal=sal+1000 where deptno=(select deptno from dept where dname='Development');
select * from emp where empno=5
delete from emp where empno=5
delect from emp;
truncate table emp;
select 1+null
output
null
select null-null
output
null
再MySQL,pastgreSQL,输出为null, 在Oracle,中不会有任何值
select 1 where null=null;
output
?column?
--------
(0 rows)
select 1 where null!=null;
output
?column?
--------
(0 rows)
不能进行比较
判断是否为null
select 1 where null is null;
select 1 where 0 is not null;
select 1 where '' is not null;
update dept set loc=null where deptno=2;
insert into dept values(4,'Operation',null);
insert into dept(deptno,dname) values(5,'Operation');