数据库基础


use runnoob
use命令用来选择数据库 use use use

分号是在数据库系统中分隔每条SQL语句的标准方法。

一些重要的SQL命令
select 提取数据
update 更新数据
delete 删除数据
insert into 插入数据
create database 创建新的数据库
alter database 修改数据库
create table 创建新表
alter table 修改数据库表
drop table 删除表
create index 创建索引
drop index 删除索引

alter alter alter


select 语句从数据库中选取数据,取到的数据存储在一个结果集中,称为结果集

select * from websites;
select name ,counter from websites;


select distinct 取结果去重
select distinct country from websites;
DISTINCT DISTINCT DISTINCT DISTINCT

where 用于提取那些满足指定条件的记录
select * from websites where country='cn';
SQL使用单引号来环绕文本值(大部分数据库系统也可以接受双引号)
如果数值字段,则不需要使用引号。
select * from websites where id=1;
=<>><>=<=
between between between
like like like
in in in

AND & OR
如果第一个条件和第二个条件都成立,则AND运算符显示一条记录。两者都要满足
两个条件只需要满足一个就能显示一条记录。
select * from websites where country='cn' snd alexa>50;
select * from websites where country='cn' or country='usa';
select * from websites where alexa>15 and (country='cn' or country='usa');

order by 用于对结果集进行排序
order by 关键字用于对结果集按照一个列或者多个列进行排序。
order by 关键字默认按照升序ASC对记录进行排序,如果需要按照降序对记录进行排序,你可以使用DESC关键字。
select * from websites order by alexa;
select * from websites order by alexa DESC;
order by 多列的时候,先按照第一个column name 排序,在按照第二个column name排序。


insert into 语句用于向表中插入新纪录。
insert into语句有两种编写形式。
第一种形式不用指定插入数据的列名,只需提供被插入的值即可。
Insert into table_name values(value1,value2...);
第二种形式需要指定列名及被插入的值。
insert into table_name (column1,column2...) values (value1,value2...);
没有指定插入数据的列名时,需要插入行数据的每一列数据。
insert into websites(name,url,alexa,count) values ('百度','http://www.baidu.com/','4','cn');
有的时候插入语句不需要插入id,因为数据库会自动更新id字段。


update 用于更新表中的记录
update table_name set comumn1=value1,column2=value2... where some_column=some_value;
update websites set alexa='5000',country='usa' where name='菜鸟教程';
在使用更新记录时要格外小心,不能省略where字句,如果省略where子句,那么表中所有字段的数据都将被更新。
在mysql中可以通过设置sql_safe_updates这个自带的参数来解决,当该参数开启的情况下,你必须在update语句后携带where条件,否则就会报错。
set sql_safe_updates=1;表示开启该参数
set sql_safe_updates=1;
set sql_safe_updates=1;

delete 用于删除表中的数据行。
delete from table_name where some_column=some_value;
和Update一样,使用delete时,需要携带where字句,不然表中所有字段都将被删除。
delete from websites where name='fecebook' and country='usa';

SQL关于删除的三个语句:drop truncate delete
drop test;
删除表test,并释放空间,将所有东西删除的一干二净。
truncate test;
删除test里的内容,并释放空间,但是不删除表的定义,表的结构还在。
delete from test where age='30' and country='usa';
删除表中的相应数据。
delete from test; 或则delete * from test;
删除表中的所有内容,但是保留表的结构,只有这个不释放表的空间。


SQL高级教程:
select top 字句用于规定要返回的记录的数目。对于拥有数千条的大型表来说,是非常有用的。
并非是所有的数据库系统都支持select top 语句。mysql支持limit语句来选取指定的条数数据,oracle可以使用rowmun来选取。
mysql:
select column_name(s) from table_name limit number;
select * from person limit 5;
select comumn_name(s) from table_name where rownum<=number;
select * from person rownum<=5;
select * from websites limit 2;
在Microft sql server中还可以使用百分比作为参数。
select top 50 persent * from websites;


like
在where子句中搜索列中的指定模式。
select column_name(s) from table_name where column_name like pattern;
select * from websites where name like 'g%';
选取name以字母“g”开头的所有客户。
select * from websites where name like '%g';
选取name以字母"g"结尾的所有客户。
select * from websites where name like '%gg%';
选取name中包含字母gg的所有客户。
通过使用not关键字,你可以选取不匹配模式的记录。
select * from websites where name not like '%gg%';
选取Name中不包含gg的所有客户。


SQL通配符
可用于替代字符串中任何其他字符。
在sql中通配符与like操作符一起使用。
% 替代0个过多个字符。
_替代一个字符。
[charlist]字符列中的任意单一字符
[^charlist]或[!charlist]不在字符列中的任何单一字符。
select * from websites where url like 'https%';
选取url以字母https开始的所有网站。
select * from websites where url like '%oo%';
选取url包含oo的所有网站。
select * from websites where name like '_oogle';
name以任意字符开始,然后是oogle的所有客户。
mysql中使用regexp或not regexp运算符(或rlike not rlike)来操作正则表达式。
select * from websites where name regexp '^[GFS]';
选取name以G,F,S开始的所有网站。
select * from websites where name resexp '^[A-H]';
选取Name以A到H字母开头的网站。
select * from websites where name regexp '^[^A-H]';
选取name不以A到H字母开头的网站。


in
允许你在where字句中规定多个值。
select column_name frpm table_name where column_name in(value1,value2...);
select * from websites where name in ('google','菜鸟教程');
等同于
select * from websites where name='google' or name='菜鸟教程';


between
用于选取介于两个值之间的数据范围的值。这些值可以是数值、文本或日期。
select column_name(s) from table_name where column_name between value1 and value2;
select * from websites where alexa between 1 and 20;
select * from websites where alexa not between 1 and 20;
select * from websites where (alexa between 1 and 20) and country not in ('usa','ind');
select * from websites where name between 'A' and 'H';
select * from access_log where date between '2016-05-10' and '2016-05-14';
注意:不同的数据库,between选取的结构可能包含两个测试值,也可能不包含,还有可能只包含一个,前一个或者后一个。


别名
可以为表名称或者列名称指定列名。可读性更强。
select name as n,country as c from websites;
select name,councat(url,',',aleax,',',country) as site_info from websites;
把三个列(url,alexa,country)结合在一起,并创建一个名为”site_info"的别名。
select w.name, w.url, a.count, a.date from websites as w, access_log as a where a.site_id=w.id and w.name='菜鸟教程';
在下面的情况下,使用别名很有用:
- 在查询中涉及超过一个表
- 在查询中使用了函数
- 列名很长或者可读性差
- 需要把两个列或者多个列结合在一起


join
left join, right join, inner join, outer join七种用法。
join子句用来把来自两个或多个表的行结合起来,基于这些表之间的共同字段。
inner join 简单的join ,从多个表中返回满足join条件的所有行。
select websites.id, websites.name, access_log.count, access_log.date from websites inner join access_log on
websites.id=access_log.site_id;
连接的结果可以在逻辑上看作是由select语句指定的列组成的新表。
select websites.name, access_log.count, access_log.date from websites inner join access_log on websites.id=access_log.site_id order by access_log.count;
inner join 关键字在表中存在至少一个匹配时返回行。如果两个表中没有匹配,则不会列出这些行。

left join
从左表返回所有的行,即使右表中没有匹配,结果为null.
select websites.name, access_log.count, access_log.date from websites left join access_log on websites.id=access_log.site_id order by access_log.count DESC;


right join
从右表返回表中所有的行,即使左表中没有匹配,结果为null.
select websites.name, access_log.count, access_log.date from websites right join access_log on access_log.site_id=websites.id order by access_log.count DESC;


full outer join
取并集。
select websites.name, access_log.count, access_log.date
from websites full outer join access_log on websites.id=access_log.site_id order by access_log.count DESC;
全部取出,即使两个表中数据不匹配。


使用Join时,on 和 where 条件的区别:
on 是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左表中的记录。
where 是在临时表生成后,在对临时表进行过滤的条件,这时已经没有left join的含义,条件不为真的就全部过滤掉。


union
合并两个或多个select语句的结果。
union内部的每个select语句必须拥有相同数量的列。列也必须拥有相似的数据类型,同时,每个select 语句中列的顺序必须相同。
select country from websites union select country from apps;
连接两个表的查询结果集,重复的不显示。
select country from websites union all select country from apps
连接两个表的查询结果集,显示重复。


insert into select
从一个表复制信息到另一个表。
insert into websites (name,country) select app_name, country from apps;
复制apps中的数据插入到websites中。
insert into websites (name,country) select app_name, country from apps where id=1;


create database dbname;
create database dbname;
create database dbname;
创建数据库。


create table table_name;
create table table_name;
create table table_name;
创建数据库表。
create table person
(
personid int,
lastname varchar(255),
address varchar(255)
);


约束(constraints)
create table table_name
(
column_name1 data_type(size) constraint_name,
...
);
not null 非空
unique 唯一
primary key 主键标识 primary primary primary
foreign key 外键标识 foreign foreign foreign
check 保证列中的值符合指定的条件
default 默认值


create index
用于在表中创建索引
在不读取整个表的情况下,索引使数据库应用程序可以更快的查找数据。
注意:更新一个包含索引的表需要比更新一个没有索引的表花费更多的时间,这是由于索引本身也需要更新。因此,理想的做法是仅仅在常常被搜索的列及表上面创建索引。
表上创索引:
create index index_name on table name(coiumn_name)
唯一索引
create unique index index_name on table_name(column_name)
列上创建索引
create index pindex on persons (Lastname)
希望索引不止一个列
create index pindex on person (lastname,firstname)


使用drop语句,可以轻松地删除索引、表和数据库。

使用alter table 语句用于在已有的表中添加、修改或修改列。
alter table table_name add column_name datatype;
alter table table_name drop column_name
alter table table_name modify column column_name datatype;


auto increment
自动生成
create table persons
(
id int not null auto increment
)


sql date 数据类型
mysql
- date yyyy-mm-dd
- datetime yyyy-mm-dd hh:mm:ss
- timestamp yyyy-mm-dd hh:mm:ss
- year yyyy或yy


null
代表遗漏的位置数据
select lastname,firstname,address from persons where address is null;
选取在address中有null的记录。
select lastname,firstname,address from persons where address is not null;
选取在address中不带null的记录。


null函数
select id,name,url,ifnull(alexa,0) from websites;
select id,name,url,coalesce(alexa,0) from websites;
如果alexa列为null值,则赋予0,否则,去原值。
ifnull ifnull ifnull
coalesce coalesce coalesce

mysql 数据类型
text(文本)、number(数字)、date/time(日期、时间)。


sql函数
count()返回匹配指定条件的行数。
...