数据库SQL基本语法+增删改查练习
2015-01-16 18:01
555 查看
13:55:58
王春 2015/1/9 13:55:58
create table depts(
dept_id int,
name varchar(10),
description varchar(50)
);
王春 2015/1/9 13:56:07
create table depts(
dept_id int,
name varchar(10),
description varchar(50)
);
王春 2015/1/9 13:56:12
#insert into depts(dept_id,name,description) values(1,'张三','11111111');
#insert into depts(dept_id,name,description) values(2,'李四','222222');
#insert into depts(dept_id,name,description) values(3,'王五','333333');
#delete from depts;
#update depts set description='000000' where dept_id=1;
select * from depts where name='张三';
15:14:56
王建 2015/1/9 15:14:56
-- create table employees(
-- emplyees_id int,
-- name varchar(10),
-- gender CHAR(10),
-- dept_id int,
-- JOIN_time DATETIME,
-- salary DOUBLE,
-- address VARCHAR(10),
-- image BLOB,
-- description varchar(50)
-- );
-- insert into employees VALUES (3,'王五','M',3,'2008-02-20 14:19:53',3000.00,'天津',null,null);
-- insert into employees VALUES (4,'赵六','F',4,'2009-02-20 15:19:53',4500.00,'沈阳',null,null);
-- insert into employees VALUES (5,'钱七','M',5,'2010-02-20 16:19:53',50000.00,'上海',null,null);
-- insert into employees VALUES (6,'孙八','F',6,'2011-02-20 17:19:53',3000.00,'深圳',null,null);
15:32:36
王春 2015/1/9 15:32:36
-- select * from employees where name='王五';
-- select * from employees where name!='王五';
-- select * from employees where salary>3000;
-- select * from employees where salary>=2000 and salary<=5000;
-- select * from employees where salary BETWEEN 2000 and 5000;
-- select * from employees where dept_id=1 or dept_id=3 or dept_id=5;
-- select * from employees where dept_id in (1,3,5);
-- select * from employees where description is not null;
-- select * from employees where name like '王%';
-- select * from employees where gender='M' and JOIN_time>'2008-01-01';
16:25:02
王春 2015/1/9 16:25:02
-- 排序:asc升序(默认)desc降序
select * from employees order by salary desc;
-- 分页:limit startIndex,count startIndex起始数(从0开始),count显示几条数据
select * from employees order by salary desc limit 0,4;
SELECT * from employees;
SELECT * from employees WHERE name='张三';
SELECT * from employees WHERE name !='张三';
SELECT * from employees where salary>3000;
SELECT * FROM employees where salary BETWEEN 2000 AND 5000;
SELECT * FROM employees where employee_id in(1,3,5);
-- SELECT * FROM employees where employee_id=1 or employee_id=3 or employee_id=5;
SELECT * from employees where dept_description is NOT NULL;
select * from employees where name LIKE '张%';
SELECT * from employees where gender='M' and join_time>'2008-01-01 00:00:00';
-- 条件查询
-- select * from employees WHERE gender='F' and salary>=3000 AND salary<=4000;
-- select * from employees;
-- 去重distinct和重命名as
-- select distinct dept_id as '部门编号' from employees;
-- 求平均数
-- select avg(salary) '部门4的平局工资' from employees where dept_id=4;
-- 求总和
-- select sum(salary) '部门4的总工资' from employees where dept_id=4;
-- 求最大:max() 最小值min()
-- select min(salary) '部门4的最高工资' from employees where dept_id=4;
-- 内连接(排除条件判断以外)
-- select * from depts d,employees e where d.dept_id=e.dept_id;
-- 外连接(左外连接:以左边的表为基准)
-- select e.* from depts d left join employees e on d.dept_id=e.dept_id;
-- 外连接(外连接:以右边的表为基准)
select d.*,e.* from depts d right join employees e on d.dept_id=e.dept_id;
13:55:58
王春 2015/1/9 13:55:58
create table depts(
dept_id int,
name varchar(10),
description varchar(50)
);
王春 2015/1/9 13:56:07
create table depts(
dept_id int,
name varchar(10),
description varchar(50)
);
王春 2015/1/9 13:56:12
#insert into depts(dept_id,name,description) values(1,'张三','11111111');
#insert into depts(dept_id,name,description) values(2,'李四','222222');
#insert into depts(dept_id,name,description) values(3,'王五','333333');
#delete from depts;
#update depts set description='000000' where dept_id=1;
select * from depts where name='张三';
15:14:56
王建 2015/1/9 15:14:56
-- create table employees(
-- emplyees_id int,
-- name varchar(10),
-- gender CHAR(10),
-- dept_id int,
-- JOIN_time DATETIME,
-- salary DOUBLE,
-- address VARCHAR(10),
-- image BLOB,
-- description varchar(50)
-- );
-- insert into employees VALUES (3,'王五','M',3,'2008-02-20 14:19:53',3000.00,'天津',null,null);
-- insert into employees VALUES (4,'赵六','F',4,'2009-02-20 15:19:53',4500.00,'沈阳',null,null);
-- insert into employees VALUES (5,'钱七','M',5,'2010-02-20 16:19:53',50000.00,'上海',null,null);
-- insert into employees VALUES (6,'孙八','F',6,'2011-02-20 17:19:53',3000.00,'深圳',null,null);
15:32:36
王春 2015/1/9 15:32:36
-- select * from employees where name='王五';
-- select * from employees where name!='王五';
-- select * from employees where salary>3000;
-- select * from employees where salary>=2000 and salary<=5000;
-- select * from employees where salary BETWEEN 2000 and 5000;
-- select * from employees where dept_id=1 or dept_id=3 or dept_id=5;
-- select * from employees where dept_id in (1,3,5);
-- select * from employees where description is not null;
-- select * from employees where name like '王%';
-- select * from employees where gender='M' and JOIN_time>'2008-01-01';
16:25:02
王春 2015/1/9 16:25:02
-- 排序:asc升序(默认)desc降序
select * from employees order by salary desc;
-- 分页:limit startIndex,count startIndex起始数(从0开始),count显示几条数据
select * from employees order by salary desc limit 0,4;
SELECT * from employees;
SELECT * from employees WHERE name='张三';
SELECT * from employees WHERE name !='张三';
SELECT * from employees where salary>3000;
SELECT * FROM employees where salary BETWEEN 2000 AND 5000;
SELECT * FROM employees where employee_id in(1,3,5);
-- SELECT * FROM employees where employee_id=1 or employee_id=3 or employee_id=5;
SELECT * from employees where dept_description is NOT NULL;
select * from employees where name LIKE '张%';
SELECT * from employees where gender='M' and join_time>'2008-01-01 00:00:00';
-- 条件查询
-- select * from employees WHERE gender='F' and salary>=3000 AND salary<=4000;
-- select * from employees;
-- 去重distinct和重命名as
-- select distinct dept_id as '部门编号' from employees;
-- 求平均数
-- select avg(salary) '部门4的平局工资' from employees where dept_id=4;
-- 求总和
-- select sum(salary) '部门4的总工资' from employees where dept_id=4;
-- 求最大:max() 最小值min()
-- select min(salary) '部门4的最高工资' from employees where dept_id=4;
-- 内连接(排除条件判断以外)
-- select * from depts d,employees e where d.dept_id=e.dept_id;
-- 外连接(左外连接:以左边的表为基准)
-- select e.* from depts d left join employees e on d.dept_id=e.dept_id;
-- 外连接(外连接:以右边的表为基准)
select d.*,e.* from depts d right join employees e on d.dept_id=e.dept_id;
相关文章推荐
- sql语法复习:增删查改,各种数据库对象创建和函数使用
- 【数据库】 sql的一些基本语法(二)
- 数据库SQL基本语法
- 【数据库之SQL复杂查询】SQL复杂查询基本语法
- 【数据库】 sql的一些基本语法(三)
- 数据库基本查询语句(SQL常用增删改查语句 简单复习 mark)
- 数据库学习笔记之SQL查询的基本语法结构
- oracle 基本语句练习(四) SQL1999 语法规范 cross join ,等值连接,不等值连接,自连接与外连接
- 数据库之SQL基本语法
- Android ORM——greenDAO 3之Properties、QueryBuilder与数据库的增删改查的基本语法应用(二)
- 数据库的基本SQL操作(增删改查)
- 【sql入门】根据要求绘制E/R图,创建数据库(2)mySQL建表基本语法及主键外键选择
- 数据库SQL语句之Table的基本练习
- 【数据库】 sql的一些基本语法(四)
- 【数据库】 sql的一些基本语法(一)
- .Net_02_增删改查的基本语法 (Sql 语句)
- Sql链接数据库基本语法
- [SQL]动态sql语句基本语法
- 动态sql语句基本语法
- 数据库sql操作基本命令集锦