一、数据库简介
二、mysql安装、使用
三、sql语言
一、数据库简介
1.数据库:存数据的地方
2.常见数据库:关系型数据库,非关系型数据库
关系型数据库
a.informix IBM+HP 军、政、运、银 (小机)
b.db2 IBM 军、政、云、银
c.oracle 甲骨文 中大型企业
d.mysql 甲骨文 互联网公司
e.sqlserver 微软
非关系型数据库
a.mongodb 大数据、爬虫
b.redis 缓存、验证
c.cassandra facebook
二、mysql安装与使用
a.安装mysql相关软件
a.1安装mysql服务端
sudo apt-get install mysql-server (安装时后会提示输入新的数据库密码,不要以数字开头)
a.2安装mysql的客户端
sudo apt-get install mysql-client
a.3安装相关的库文件
sudo apt-get install libmysqlclient-dev
b.mysql基础
b.1mysql的主服务:mysqld
启动mysqld:service mysqld start
停止mysqld:service mysqld stop
重启mysqld:service mysqld restart
b.2检查mysql服务是否正常运行: ps -ef |grep mysqld
lolo@lala:/$ ps -ef |grep mysqld
mysql 8695 1 0 09:52 ? 00:00:01 /usr/sbin/mysqld
zelin 10741 4295 0 10:22 pts/0 00:00:00 grep --color=auto mysqld
b.3mysql的端口:3306
b.4查看端口:netstat -an | grep 3306
lolo@lala:/$ netstat -an |grep 3306
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
unix 3 [ ] 流 已连接 23306
b5.管理员:root
b6.mysql的登录: mysql -u root -p
lolo@lala:/$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.23-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
b.7管理员登录本地数据库:mysql -h 127.0.0.1 -u root -p (管理员登录IP为127.0.0.1的数据库)
b5.mysql基础
数据库分为2部分:数据库软件、数据库的数据
常说的数据库是数据库数据,数据库都是由表(表格)组成,表由数据组成
三、sql语句
1.查看当前有哪些数据库:show databases;
查看当前数据库中有哪些表:show tables;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
2.查看当前用户用户名select user();
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
3.查看当前数据库的名字:select database();
4.切换数据库为mysql: use mysql;
mysql> select database();
+------------+
| database() |
+------------+
| NULL |#为NULL
+------------+
1 row in set (0.00 sec)
mysql> use mysql#使用mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| mysql |#显示为mysql
+------------+
1 row in set (0.00 sec)
5.查看当前数据库中有哪些表:show tables;
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.00 sec)
6.简单查询:select * from tablename 查看表格的所有数据
select * from user;
select * from user\G;
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.00 sec)
c.表结构的创建
c1.数据类型
数字 int 123456
字符 char varchar 'heygor' 'abc'
日期 date '20180-11-01'
注意:char 和 varchar区别
char 定长 char(10)
varchar 变长 varchar(10)
c2.表结构的创建
语法:create table 表(
列名(字段) 数据类型,
列名(字段) 数据类型,
......
);
例子:创建表韩国(kr),泡菜名称(pname) 字符类型,泡菜价格(price) 数字类型,出厂日期(ddate) 日期类型
7.创建数据库:create database databasename
8.创建表格:create table kr(
pname char(10) not null primary key,
price int unique,
ddate date
);
补充:创建数据库:create database 名字
创建数据库test: create database test;
补充:查看表结构: desc 表名
查看kr表的表结构:desc kr
c3.约束
主键:列中的数据不能为空,而且不能重复(只能唯一),每张表中只能有一个主键
唯一:列中的数据不能重复,作为主键约束的补充
非空:列中的数据不能为空,只要向表中新增数据,就一定向该列进行操作
检查:列中的数据是否满足条件
外键:一张表中的列的数据依赖于另外一张表中的数据
补充:
create table 表(
列 数据类型 primary key, 主键约束
列 数据类型 unique, 唯一约束
列 数据类型 not null, 非空约束
列 数据类型 check(条件 and、or 条件), 检查约束
列 数据类型 references 被参考表(被参考列) 外键约束
);
5.数据的新增
字符类型需要加单引号
数字类型直接使用
日期类型需要加单引号
a.向表中所有列新增数据
语法:insert into 表 values(值1,值2..);
例子:向kr表中新增数据,泡菜名称为pc,价格250,出厂日期2019-01-01
insert into kr values('pc',250,'2019-01-01');
b.向表中指定列新增数据
语法:insert into 表(列1,列2) values(值1,值2);
例子:向kr表中新增数据,泡菜名称ladeng,价格119
insert into kr(pname,price) values('ladeng',119);
c.添加多条数据
insert into ach(id,mark) values(1,90),(2,96),(3,88),(4,86),(5,83),(6,85);
6.数据的修改
字符类型加单引号
数字类型直接使用
日期类型加单引号
语法:update 表 set 列=值 [where 条件];
注意:如果增加where条件,修改的是指定行的数据,如果不加where条件,修改的是整个列的数据
例子:修改kr表中价格为0
update kr set price=0;
例子:修改kr表中名字是pc的price为2000
update kr set price=2000 where pname='pc';
7.数据的删除
delete DML,可以删除指定数据,删除数据可以找回,不删表
truncate DDL,只能删除所有数据,删除数据无法找回,不删表
drop 删除表格
delete:
语法:delete from 表 [where 条件];
例子:删除kr表中名字是ladeng的所有信息
delete from kr where pname='ladeng';
例子:删除kr表中所有数据
delete from kr;
truncate:
语法:truncate table 表;
例子:删除kr表中所有数据
truncate table kr;
8.数据的查询
简单查询
多表查询
分组查询
子查询
a.简单查询
查询时候可以查询指定列、指定行,指定行的列
语法:select 列 from 表 [where 条件];
查询kr表中所有泡菜名称
select pname from kr;
查询kr表中价格是250的所有信息
select * from kr where price=250;
查询商品名称是heyga的价格
select price from kr where pname='heyga';
注意:select后面from前面是和列相关,where条件和行相关。
b.where条件(子句)
b1.比较运算符
>
<
=
>=
<=
!=
<>
例子:查询kr表中价格高于260的泡菜的名字
select pname from kr where price>260;
例子:查询kr表中名字不是heyga的所有信息
select * from kr where pname <>'heyga';
select * from kr where pname !='heyga';
b2.关系运算符
and
or
not
例子:查询kr表中价格小于等于250并且名字是simida的所有信息
select * from kr where price <=250 and pname='simida';
例子:查询kr表中名字是simida或者heygor的所有信息
select * from kr where pname='simida' or pname='heygor';
b3.区间(btween..and..)
例子:查询价格范围在250和290之间的所有信息
select * from kr where price >=250 and price <=290;
select * from kr where price between 250 and 290;
b3.模糊查询
当信息不完整时候使用。
like
_ 一个字符
% 任意个字符
例子:查询kr表中名称以h开头的所有信息
select * from kr where pname like 'h%';
例子:查询kr表中以ga结尾的所有信息
select * from kr where pname like '%ga';
例子:查询kr表中名字是4个字符的所有信息
select * from kr where pname like '____';
b4.空值查询
空(null)没有任何数据类型,没有任何的值
不能用于比较和运算,查询时候使用is null或者is not null
例子:查询kr表中price为空的所有信息
select * from kr where price is null;
b5.in
在...里面(或的关系)
例子:查询kr表中名字是gaga或者baga的所有信息
select * from kr where pname in('gaga','baga');
9.表结构的修改
修改表名
rename table oldtablename to newtablename;
移动表到别的数据库
rename table olddb.tablename to newdb.tablename;
删除列
alter table tablename drop column columnname;
增加列
alter table tablename add column columnname type;
修改列名(新名同旧名,也能达到修改列属性的目的)
alter table tablename change columnname_old columnname_new type;
修改列属性
alter table tablename modify columnname newtype
1.多表查询
补充:mysql中导入sakila数据库
下载地址
https://dev.mysql.com/doc/index-other.html
表结构说明
http://www.mamicode.com/info-detail-555168.html
1>下载sakila数据库
2>解压压缩包
3>进入到解压目录
4>管理员用户登录
5>执行导入
source sakila-schema.sql;
source sakila-data.sql;
6>测试
use sakila;
show tables;
a.迪卡尔积
两张表不做任何关联产生的数据,导致数据量成倍增加
select count(*) from city;
select count(*) from country;
select count(*) from city,country;
为了避免产生无效数据所以会使用内联查询,左联查询、右联查询
b.内联查询
判断关联列的方法:
1< 列名一致
2< 作用一致
3< 主外键关系
语法:
select 列 from 表1 ,表2 where 表1.列=表2.列;
select 列 from 表1 inner join 表2 on 表1.列=表2.列;
例子:查询国家名称是China的城市名称
分析:
分析表:
country
city
分析列:
country country
city city
关联:
select * from city,country
where city.country_id=country.country_id;
过滤:
select city from city,country
where city.country_id=country.country_id
and country='China';
例子:查询城市名称是Sanya的国家名称
分析表:
city
country
分析列:
city
country
关联:
select * from city c,country co
where c.country_id=co.country_id;
过滤:
select co.country from city c,country co
where c.country_id=co.country_id
and c.city='Sanya';
c.左联和右联查询
左联查询语法:
select 列 from 左表 left join 右表 where 左表.列=右表.列;
左联查询结果(左表中数据都有):
内联查询结果+左表有、右表没有的右表显示为空
create table a(
a int
);
create table b(
a int,
b char(10)
);
insert into a values(1),(2),(3);
insert into b values(1,'a'),(2,'b'),(4,'d');
select * from a left join b on a.a=b.a;
右联查询语法(右表中所有数据都有):
select 列 from 左表 right join 右表 on 左表.列=右表.列;
d.子查询
一条SQL语句的执行依赖于另外一条SQL语句的执行结果
例子:查询国家名字为China的城市名称
1.查询国家表中国家名字是China的country_id
select country_id from country where country='China';
2.查询城市表中country_id为上面查询出来结果的city
select city from city where country_id=23;
子查询:
select city from city where country_id=(select country_id from country where country='China');
子查询可以应用在数据库中的新增、修改、删除、查询中
注意:
子查询分类单行子查询、多行子查询
单行子查询,子句中返回的数据只有一个,使用等号即可
多行子查询,子句中返回的数据可能有多个,使用in方式
2 select first_name,last_name,email from city,address,customer where city.city_id=address.city_id and address.address_id=customer.address_id and city='sanya';
e.分组查询
e1.分组函数(聚合)
max()
min()
avg()
sum()
count()
分组函数通常和分组一起使用,也可以单独使用
例子:查询payment表中最高金额,最低金额,平均金额
select max(amount),min(amount),avg(amount) from payment;
e2.分组查询
语法:
select 列 from 表
[where 条件]
group by 分组条件
[having 分组后过滤条件];
例子:
查询city表中每个counrty_id下有多少个城市的名字?
select country_id,count(city) from city group by country_id;
例子:
查询city表中每个counrty_id下有多少个城市的名字,要求显示国家名字
注意:如果是题目分组+多表查询先进行关联再进行分组
select co.country,count(c.city) from city c,country co
where c.country_id=co.country_id
select count(first_name) from customer,country,address,city where customer.address_id=address.address_id and address.city_id=city.city_id and city.country_id=country.country_id group by country.country having country='china';