写给大忙人看的SQL

SQL基础

一、基础SQL语句
1.创建数据库\表
  create database 库名;
  create table 表名(列1 类型 primary key not null ,列2 类型 not null ,…);
1)直接创建
create table emp1(
name varchar2(20),
salary number(8,2)default 1000,
id number(4),
hire_date date
);
2)通过子查询的方式创建
create table emp2
select t_name name,employee_id id,hire_date
from employees;
或者
create table emp2
select t_name name,employee_id id,hire_date
from employees
where department_id = 80;/where 1=2;
2.展示所有已存在的数据库
  show databases;\show tables;
3.进入指定的数据库
  use 库名;
4.查询表数据
  select *from 表名 where 范围;
5.插入表数据
   insert into 表名(列1,列2,…)values (value1,value2,…);
6.更改表数据
  update 表名 set 列名=新值 where 列名=旧值;
7.删除表数据
  delete from 表名 where 范围;
8.用已有的表创建新表
  create table 新表 like 旧表; /新表展示的列名和旧表完全一致,只是无数据
9.增加表的列/主键
   alter table 表名 add column 列名 类型 primary key ; /列增加后将不能删除。列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。
10.增加/删除表的主键
  alter table 表名 add/drop primary key(列名);
11.删除数据库/表结构
  drop database 库名/table 表名;
12.重点注意的sql语句
  查找:select *from table1 where field1 like ’%value1%’
  排序:select * from table1 order by field1,field2 [desc] / desc是降序,asc是升序,一般默认就是升序
  总数:select count as totalcount from table1
  求和:select sum(field1) as sumvalue from table1
  平均:select avg(field1) as avgvalue from table1
  最大:select max(field1) as maxvalue from table1
  最小:select min(field1) as minvalue from table1
13.修改表
1)增加新的列

alter table emp1
add(birthday date)

2)修改现有的列

alter table emp1
modify(name varchar2(25) default 'abc'

3)重命名现有的列

alter table emp1
rename column salary to sal;

4)删除现有的列

alter table emp1

drop column birthday;

14.清空表中的数据(与delete from table_name区分开)
truncate table emp2;
15.重命名表
rename emp2 to emp3;
16.数据处理 DML
1)增

1.1增添一条记录
insert into  [表名](,,,,,)
values(,,,,,)

1.2从其它表中拷贝数剧
insert into [表名]
select .... from [另一个表]
where ....

2)改

update [表名]
set .....
where ....

3)删

delete from [表名]
where ....

4)查(最常用的数据库操作)

select ....
from …
where ….
group by …
having ....
order by ….

二、多表查询
例如:按照department_id查询employees(员工表)和departments(部门表)的信息。
方式一(通用型):SELECT … FROM … WHERE
SELECT e.t_name,e.department_id,d.department_name
FROM employees e,departments d
where e.department_id = d.department_id

方式二:SELECT … FROM … NATURAL JOIN …
有局限性:会自动连接两个表中相同的列(可能有多个:department_id和manager_id)
SELECT t_name,department_id,department_name
FROM employees
NATURAL JOIN departments

方式三:SELECT … JOIN … USING …
有局限性:好于方式二,但若多表的连接列列名不同,此法不合适
SELECT t_name,department_id,department_name
FROM employees
JOIN departments
USING(department_id)

方式四:SELECT … FROM … JOIN … ON
常用方式,较方式一,更易实现外联接(左、右、满)
SELECT t_name,e.department_id,department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
三、子查询

在这里插入图片描述
在这里插入图片描述
四、结果集的交并差运算
  union:返回两个结果集的并集,如
  select name from a
  union
  select name from b; \得出的结果是a表和b表的name并集,删除重复行,默认排序
注意:union all 则是不删除重复行全部都展示,且没有排序
  except:返回只在第一个结果集而不在第二个结果集中的数据,如
  select name from a
  except
  select name from b; \展示a表存在而b表不存在的数据,删除重复行
注意:except all 多了不删除重复行全部都展示
  intersect:返回两个结果集的交集,如
  select name from a
  intersect
  select name from b; \展示a表和b表都存在的数据,删除重复行
注意:intersect all 多了不删除重复行全部都展句

声明:以上所有内容来源于百度文库,和百度,阿猪筛选总结出来的

发布了10 篇原创文章 · 获赞 63 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/m0_46315852/article/details/104760019