前言
推荐资料:
数据库简单介绍
数据库可以分为关系型数据库和非关系型数据库:
1. 关系型数据库管理系统的介绍
数据库管理系统(英语全拼: Relational Database Management System简称 RDBMS)是为管理关系型数据库而设计的软件系统,如果大家想要使用关系型数据库就需要安装数据库管理系统?其实就是一个应用软件
B站视频:此处详细阐释了关系型数据库,个人觉得类似线程池的的作用,可以减少开销。
- mysql:关系型
- redis:一般做缓存
- mongodb:非关系型
关系型数据库管理系统可以分为
关系型数据库服务端软件: 主要负责管理不同的数据库,而每个数据库里面会有一系列数据文件,据文件是用来存储数据的,其实数据库就是一系列数据文件的集合
关系型数据库客户端软件: 主要负责和关系型数据库服务端软件进行通信,向服务端传输数据或者从服务端获取据。关系型数据库管理系统的效果图:
2. SQL的介绍
3.小结
关系型数据库管理系统是一个软件,它可以管理不同的数据库,想要对数据库进行操作安装关系型数据库管理系统软件即可。 SQL的作用是实现数据库客户端和数据库服务端之间的通信,好比是通信桥梁。
MySQL数据库
- MYSQL数据库的介绍
MSQL是一个关系型数据库管理系统,在WEB应用方面? MYSQL是最好的 RDBMS( Relational Database Management System,关系数据库管理系)应用软件?它是由瑞典 MYSQL AB公司开发?目前属于Oracle旗下产品, MYSQL是最流行的关系型数据库管理系统中的一个
MYSQL的特点:
- MYSQL是开源的,所以你不需要支付额外的费用
- MYSQL支持大型的数据库。可以処理拥有上千万条记录的大型数据库。
- MySQL使用标准的SL数据语言形式
- MYSQL可以安装在不同的操作系统,并且提供多种编程语言的操作接口。这些编程语言包括C、C++ Python、Java、Ruby等等。
1. Ubuntu下服务器端和客户端的MySQL的安装
ubuntu服务端安装
sudo apt-get install mysql-server
sudo service mysql start
sudo service mysql stop
sudo service mysql restart
sudo service mysql status
ps ajx|grep mysql
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
注意:此处截图是配置文件路径 /etc/mysql/mysql.conf.d/mysqld.cnf
这个问题解决之后,用Navicat就可以默认访问了,否则需要sudo命令启动Naviacat - Ubuntu下解决root账户登录mysql只能sudo登录的问题:这个文章里面提到的配置文件是错误的
ubuntu下客户端安装
sudo apt-get install mysql-client
chmod +x navicat16-mysql-cs.AppImage
./navicat16-mysql-cs.AppImage
-
- Navicat使用
2.window上安装MySQL服务端
# 数据类型与约束
对于图像、视频、音频文件一般存储于服务器上,然后在表中存储文件的路径
# 数据库使用-命令行脚本(重点)
## 数据库的操作
mysql -uroot -p
mysql -uroot -p密码;
exit/quit/ctrl+d
select version();
select now();
show databases;
create database python_test charset = utf8;
show create database python_test;
use python_test;
drop database python_test;
select database();
## 数据表的操作
show tables ;
create table students(
id int unsigned unique not null auto_increment primary key,
name varchar(30) not null,
age tinyint unsigned,
high decimal(5,2),
gender enum("男","女","中性","保密") default "保密",
cls_id int unsigned
);
desc table_test;
insert into students values (0, "老王", 23, 187, "男", 2);
select * from students;
create table classes(
id int unsigned not null auto_increment primary key,
name varchar(30)
);
alter table studuents add brithday datetime;
alter table studuents modify brithday date;
alter table studuents change brithday brith date default "2000-01-01";
alter table studuents drop brith;
delete from studuents where id > 3;
drop table studuents;
show create table studuent;
## 数据的增删改查
create database name chartset=utf8;
查看正在使用的是哪个数据库
select database();
创建表
CREATE TABLE tb_enp1
(
id int(11),
name varchar(25),
deptld int(11),
salary float
);
创建单字段主键
一,
CREATE TABLE tb_enp2
(
id int(11) PRIMARY KEY,
name varchar(25),
deptld int(11),
salary float
);
二,
CREATE TABLE tb_enp3
(
id int(11),
name varchar(25),
deptld int(11),
salary float,
PRIMARY KEY(id)
);
定义联合主键
CREATE TABLE tb_enp4
(
name varchar(25),
deptld int(11),
salary float,
PRIMARY KEY(name,deptld)
);
外键约束
创建一个表
CREATE TABLE tb_dept1
(
id int(11) PRIMARY KEY,
name varchar(22) NOT NULL,
location varchar(50)
);
CREATE TABLE tb_nep5
(
id int(11) PRIMARY KEY,
name varchar(25),
deptld int(11),
salary float,
CONSTRAINT fk_enp1 FOREIGN KEY(deptld) REFERENCES tb_dept1(id)
);
create table students(
id int unsigned not null auto_increment primary key,
name varchar(30),
age tinyint unsigned default 0,
high decimal(5,2),
gender enum("男","女"),
cls_id int unsigned
);
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVSION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
输入数据
insert into students values(0,"老王",18,188.88,"男",0);
查看表的内容
select * from name;
查看表的结构,名字,字段和字段属性。
desc name;
已经创建的表里添加字段
alter table students(表的名字) add 字段名 数据类型;
修改表里字段的数据类型
alter table students modify 字段名 数据类型;
修改字段的名称
alter table students change 字段名 新名字 数据类型;
删除字段!!
alter table students(表名) drop 字段名;
删除表
drop table 表名;
查看表的完全属性
show create table 表名;可以查看引擎
修改数据
插入数据
全部插入
insert into class(表名) values(1,2,3,...先desc 表名 查看数据结构,然后一一对应插入内容)
insert into students values (0,'小乔',18,160.00,02);
插入内容时 应为设置了主键primary key,以及自主增长auto_increment 所以插入主键id的那一行可以写0/null(空值)/default(默认).
写性别gender的数据时因为用了枚举enum,只能写枚举里的值,也可以写1,2,3,4一一对应当初设置的顺序性别。
部分插入
insert into students (name(字段名),gender(字段名)) values ('小乔','女');
一次插入多个数据
insert into students (name(字段名),gender(字段名)) values ('小乔','女'),('貂蝉','女');部分插入和全体插入都可.
修改数据
update studengs(表名) set gender(要修改的字段)=***,age=** where(条件限制) id=*(主键,数据的唯一标识);
当where后面的条件限制不明确时所有符合条件的数据都会更改!!
insert into students values (0,'貂蝉',20,160.00,2,02,2000.01.01);
查询字段
select * from students where id>3(限制条件),*代表全部字段。
select name,gender from students;查询两个字段,字段名顺序可以改变,显示时的顺序也会改变
查询字段时修改字段显示的名称
select name as 名字,gender as 性别 from students ;
删除数据
delete from students where name=**;
逻辑删除与物理删除
逻辑删除增加新行进行删除标记
alter table students add is_delete bit default 0;
update students set is_delete = 1 where id=*;
查询时筛除重复数据
select distinct gender(字段名) from students;
条件查询
select */字段名(表示所有字段) from 表名 where age>18(条件);
多条件查询 逻辑符号
and 同时满足条件
select */字段名(表示所有字段) from 表名 where age>18(条件)and age<30;
or 满足一种条件
select */字段名(表示所有字段) from 表名 where age>18(条件)or gender=2;
not
select */字段名(表示所有字段) from 表名 not(where age>18(条件)or gender=2);同时否定两个条件
select */字段名(表示所有字段) from 表名 notwhere age>18(条件)or gender=2;否定not
后面的条件。
模糊查询
like
select */name/字段名(表示所有字段) from 表名 where name like "%小%";能查询名字中包含小的。
select */name/字段名(表示所有字段) from 表名 where name like "小%" %代表字符或没有,能查询名字以小开头的。
查询名字是两个字
select */name/字段名(表示所有字段) from 表名 where name like "__"(两个下划线);
查询名字至少是两个字
select */name/字段名(表示所有字段) from 表名 where name like "__%";
rlike后面接正则表达式
查询以周开始的姓名
select */name/字段名(表示所有字段) from 表名 where name rlike "^周.*";
查询以周开始以伦结束的姓名
select */name/字段名(表示所有字段) from 表名 where name rlike "^周.*伦s";
in 查询年龄为12,18,30的
select */字段名(表示所有字段) from 表名 not where age = 18 or age=22 or age=30;
select */字段名(表示所有字段) from 表名 not where age in (18,22,30);
not in 查询年龄不是12,18,30的
select */字段名(表示所有字段) from 表名 not where age not in (18,22,30);
查询范围之内;
between...and..
select */字段名(表示所有字段) from 表名 where age between 18 and 35;
不在范围内
select */字段名(表示所有字段) from 表名 where age not between 18 and 35;
select */字段名(表示所有字段) from 表名 where not age not between 18 and 35;
空值判断
select */字段名(表示所有字段) from 表名 where 字段名 is null;
不空
select */字段名(表示所有字段) from 表名 where 字段名 is not null;
排序
按照年龄18到35的男性排序,默认从小到大
select */字段名(表示所有字段) from 表名 where (age between 18 and 35)and gender=1 order by age;
select */字段名(表示所有字段) from 表名 where (age between 18 and 35)and gender=1 order by age asc;
从大到小,支持按照多字段排序。
select */字段名(表示所有字段) from 表名 where (age between 18 and 35)and gender=1 order by age dasc,id dasc;
聚合函数
count总数
select count(*) as 男性人数(显示注解)from students where gender=1(条件);
最大值max,最小值min,求和sum,求平均值avg
select max/min/(age) from students;
在students里女性的最大身高
select max/sum(height) from students where gender=2;
求平均值
select sum(height)/count(*) from students where gender=2
保留几位小数round,还有四舍五入的作用
select round(sum(height)/count(*),2(两位小数)) from students where gender=2;
分组
group by
select gender(分组后的同一属性) from students group by gender;
select gender,group_concat(name) from students group by gender;可以显示分组后每一组里人的姓名;
分组后每一组的人数,count(*)是对分组后的数据进行聚合
select gender,count(*) from students group by gender;
select gender,max(age) from students group by gender;每组的最大年龄
select gender,avg(age) from students group by gender;
分组后男性的人数
select gender,count(*) from students where gender=1 group by gender;
分组后的跟多操作
分组后查询男性的名字,id和年龄,中间添加空格。
select gender,group_concat(name,' ',id,' ',gae) from students where gender=1 group by gender;
having对结果的判断,where对原始数据的判断所以写的位置不同。
select gender,group_concat(name),avg(age) from students where gender=1 group by gender having avg(age)>30;
select gender,group_concat(name) from students where group by gender having avg(age)>30
分页
select * from students where 条件 limit 2;只显示两行;
select * from students where 条件 limit 5(开始位置,根据列表,这是第六个),2(两行);
分页显示公式
select * from students where 条件 limit (第m页-1)*n,n;不能直接输入.
连接查询
inner join ...on on取交集,两个表的匹配列的交集
select *(表示显示所有字段) from students inner join calsses on students.cla_id=classes.id;通过students表里的cla_id和classes表里的id字段匹配连接。
select s.name,c.name from students as s inner join calsses as c on s.cla_id=c.id;只显示students表里的姓名和classes表里的班级。
例班级名字在前,学生所有信息在后,按班级排列后,按照id排列
select c.name,s.* from students as s inner join calsses as c on s.cla_id=c.id order by c.name,s.id;
左连接 left join on 两个表匹配以后把左边表的信息全部显示,右边表对不上的为空
select c.name,s.* from students as s left join calsses as c on s.cla_id=c.id order by c.name,s.id;
上边的语句可以当做新的表来查询
select c.name,s.* from students as s left join calsses as c on s.cla_id=c.id order by c.name,s.id having c.id si null;查询两个表内无法匹配的内容。
自关联 同一个表里的不同列有关联,例如省市县
select *(显示内容) from m(表1)as n inne join m(表1) as s(第二个名字,一个表就可以相当于两个表用。) on n.列=s.列 having(条件) n.列=??;
inne jion 表 on 里的表可以用sql语句代替作为一个表;
子查询
把一个sql语句当做另一个sql语句的条件
select * from students where height=(select max(height) ;
把一个表一列的值根据另一个相关表的列,替换成新的值。
update 表1(更新那个表) as 1 join 表2 as 2 on 1.列=2.列 set 1.列2=2.列2;
设置外键关联
alter table 表1 add forrign key (列) references 表2(列);
建立视图 用以方便查询
create view 视图名称 as select 语句
查看视图
show tables
使用视图
select * from 视图名称
删除视图
dorp view 视图名称
事务的 ACID
1,原子性,作为最小的事务单位,要不全部成功,要不失败回滚。
2,一致性,防止程序运行到一般死机,导致事务没有全部运行。
3,隔离性,一个事务操作着一个数据,会无视其他的事务
4,持久性,永久保存
开启事务
start transaction ;begin;
commit;结束提交
查看运行时间
show profiles;
建立索引
create index title_index on text_index(title(10));
title_index 索引名称 text_index表名 title(10)列名 长度,建立索引时如果是字符串类型(char,varchar)根据原列的长度建立能提高速度
# 数据库查询(可以复制练习)
## 创建数据库、数据表
create database `python_test` charset=utf8;
show databases;
use python_test;
select database();
create table students(
id int unsigned primary key auto_increment not null,
name varchar(20) default '',
age tinyint unsigned default 0,
height decimal (5,2),
gender enum('男', '女', '中性', '保密') default '保密',
cls_id int unsigned default 0,
is_delete bit default 0
);
create table classes(
id int unsigned auto_increment primary key not null,
name varchar(30) not null
);
show tables ;
show create tables students;
## 准备数据
insert into students values
(0,'小明',18,168.00,2,1,0),
(0,'小黄',17,175.00,1,2,0),
(0,'小红',14,177.00,2,3,0),
(0,'小汉',11,180.00,3,4,0),
(0,'小八',12,187.00,3,5,0),
(0,'小九',13,182.00,4,6,0),
(0,'小十',18,188.00,3,7,0),
(0,'小之',17,186.00,2,8,0),
(0,'小一',10,188.00,2,9,0),
(0,'小二',15,182.00,3,9,0),
(0,'小三',18,184.00,2,6,0),
(0,'小四',19,185.00,4,4,0),
(0,'小五',13,190.00,2,3,0),
(0,'小六',14,189.00,2,4,0),
(0,'小七',15,178.00,2,5,0),
(0,'十一',15,167.00,1,7,0),
(0,'十二',18,176.00,1,2,0);
insert into classes values
(0, "python01期"),
(0, "python02期"),
(0, "python04期");
desc classes;
## 查询
select * from students;
select * from classes;
select id, name from students;
select name as 姓名, age as 年龄 from students;
select students.name, students.age from students;
select students.name, students.age from students;
select s.name, s.age from students as s;
select distinct gender from students;
条件查询
select * from students where age > 18;
select name, gender, id from students where age > 18;
select * from students where age > 18 and age < 28;
select * from students where age > 18 and gender = "女";
select * from students where age > 18 and gender = 2;
select * from students where age > 18 or gender = 2;
select * from students where not age > 18 or gender = 2;
select * from students where not (age > 18 or gender = 2);
select * from students where not ( age < 18 or age = 18) and gender = 2;
select * from students where not age <=18 and gender = 2;
select * from students where name like "小%";
select * from students where name like "%小%";
select * from students where name like "小_";
select * from students where name like "__%";
select * from students where name rlike "^小.*";
select * from students where name rlike "^小*九$";
select * from students where name regexp "^小*九$";
select * from students where name regexp "一";
范围查询
select name from students where age = 18 or age = 34;
select name from students where age = 18 or age = 34 or age = 12;
select name, age from students where age in (12, 18, 34);
select name, age from students where age not in (12, 18, 34);
select name, age from students where age between 12 and 34;
select name, age from students where age not between 12 and 34;
select name, age from students where not age between 12 and 34;
select name, age from students where age not (between 12 and 34);
select name from students where age is null;
select name from students where age is not null;
排序查询
-- 排序
-- order by 字段
-- asc 从小到大排序 即升序
-- desc 从大到小排序 即将序
-- 在排序过程中遇到相同值时会按照主键进行排序
-- 查询年龄到12~23之间的男性,按照年龄从小到大排序
select * from students where (age between 12 and 23) and gender = 1;
select * from students where (age between 12 and 23) and gender = 1 order by age;
select * from students where (age between 12 and 23) and gender = 1 order by age asc;
-- 查询年龄到12~23之间的男性,按照年龄从大到小排序
select * from students where (age between 12 and 23) and gender = 1 order by age desc;
-- order by 多个字段
select * from students where (age between 12 and 23) and gender = 1 order by age desc, gender desc, height desc;
-- 在年龄相同的情况下按照性别按照升序的方式排序,如果前面排序字段都不相同则后面的字段不执行
聚合函数
select count(name), avg(height) from students;
select * from students where gender = 1;
select count(*) from students where gender = 1;
select count(*) as 男性人数 from students where gender = 1;
select * from students;
select max(age) as 最大值 from students;
select min(age) as 最小值 from students where gender = 2;
select sum(age) as 和 from students;
select sum(age)/count(*) as 均值 from students;
select avg(age) as 最大值 from students;
select round(avg(age), 2) as 四舍五入 from students;
select gender, count(gender) from students group by gender;
select gender, count(*) from students group by gender;
desc students;
select gender, count(gender), group_concat(name, ' ', id, ' ', height, ' ', cls_id) from students group by gender;
select gender, count(*) from students where gender = 1;
select gender, count(*) from students where gender = 1 group by gender;
+
| gender | count(*) |
+
| 男 | 3 |
+
1 row in set (0.00 sec)
select gender, group_concat(name, age, id), count(*) from students where gender = 1 group by gender;
+
| gender | group_concat(name, age, id) | count(*) |
+
| 男 | 小黄172,十一1516,十二1817 | 3 |
+
1 row in set (0.00 sec)
select gender, group_concat(name, "_", age, " ", id), count(*) from students where gender = 1 group by gender;
+
| gender | group_concat(name, "_", age, " ", id) | count(*) |
+
| 男 | 小黄_17 2,十一_15 16,十二_18 17 | 3 |
+
1 row in set (0.00 sec)
select gender, group_concat(name) from students group by gender having avg(age)>8;
select gender, group_concat(name), avg(age), count(*) from students group by gender having count(*) > 3;
select gender, group_concat(name), avg(age) from students group by gender having avg(age) > 11;
select gender, group_concat(name), count(name) from students group by gender with rollup having avg(age)>8;
select gender, cls_id, group_concat(name, ' ', cls_id) from students group by gender, cls_id;
select gender, group_concat(name, age, height) from students group by gender having avg(age)>8 order by count(height);
分页查询
select * from students limit 5;
select * from students order by age desc limit 5;
select * from students limit 7, 4;
select * from students where gender = 2 order by height desc limit 0, 2
连接查询
select * from students inner join classes;
select * from students inner join classes on students.cls_id = classes.id;
select students.* from students;
select classes.* from classes;
select students.*, classes.name from students inner join classes on students.cls_id = classes.id;
select s.*, c.name from students as s inner join classes as c on s.cls_id = c.id;
select s.*, c.name from students as s inner join classes as c on s.cls_id = c.id order by c.name, s.id ;
select c.name, s.* from students as s inner join classes as c on s.cls_id = c.id order by c.name, s.id ;
select c.name, s.* from students as s inner join classes as c on s.cls_id = c.id order by c.name, s.id, s.age;
select * from students as s left join classes as c on s.cls_id = c.id;
select * from students as s inner join classes as c on s.cls_id = c.id;
select * from students;
select * from classes as c left join students as s on c.id = s.cls_id;
select * from students as s left join classes as c on c.id = s.cls_id having c.id is NULL;
select * from students as s left join classes as c on c.id = s.cls_id where c.id is NULL;
自关联
earas.sql文件可以点击下载;
create table areas(
aid int primary key,
pid int,
atitle varchar(20),
types tinyint(1) default "2"
);
select * from areas where pid = 1;
select * from areas where atitle = '陕西';
+
| aid | pid | atitle | types |
+
| 24 | 1 | 陕西 | 1 |
+
1 row in set (0.00 sec)
select * from areas where pid = 24;
+
| aid | pid | atitle | types |
+
| 311 | 24 | 西安 | 2 |
| 312 | 24 | 安康 | 2 |
| 313 | 24 | 宝鸡 | 2 |
| 314 | 24 | 汉中 | 2 |
| 315 | 24 | 商洛 | 2 |
| 316 | 24 | 铜川 | 2 |
| 317 | 24 | 渭南 | 2 |
| 318 | 24 | 咸阳 | 2 |
| 319 | 24 | 延安 | 2 |
| 320 | 24 | 榆林 | 2 |
+
10 rows in set (0.00 sec)
select * from areas where pid = 311;
+
| aid | pid | atitle | types |
+
| 2596 | 311 | 莲湖区 | 3 |
| 2597 | 311 | 新城区 | 3 |
| 2598 | 311 | 碑林区 | 3 |
| 2599 | 311 | 雁塔区 | 3 |
| 2600 | 311 | 灞桥区 | 3 |
| 2601 | 311 | 未央区 | 3 |
| 2602 | 311 | 阎良区 | 3 |
| 2603 | 311 | 临潼区 | 3 |
| 2604 | 311 | 长安区 | 3 |
| 2605 | 311 | 蓝田县 | 3 |
| 2606 | 311 | 周至县 | 3 |
| 2607 | 311 | 户县 | 3 |
| 2608 | 311 | 高陵县 | 3 |
+
13 rows in set (0.01 sec)
select * from areas as province inner join areas as city on province.aid = city.pid having province.atitle = "陕西";
select province.atitle, city.atitle from areas as province inner join areas as city on city.pid = province.aid having province.atitle = "陕西";
+
| aid | pid | atitle | types | aid | pid | atitle | types |
+
| 24 | 1 | 陕西 | 1 | 311 | 24 | 西安 | 2 |
| 24 | 1 | 陕西 | 1 | 312 | 24 | 安康 | 2 |
| 24 | 1 | 陕西 | 1 | 313 | 24 | 宝鸡 | 2 |
| 24 | 1 | 陕西 | 1 | 314 | 24 | 汉中 | 2 |
| 24 | 1 | 陕西 | 1 | 315 | 24 | 商洛 | 2 |
| 24 | 1 | 陕西 | 1 | 316 | 24 | 铜川 | 2 |
| 24 | 1 | 陕西 | 1 | 317 | 24 | 渭南 | 2 |
| 24 | 1 | 陕西 | 1 | 318 | 24 | 咸阳 | 2 |
| 24 | 1 | 陕西 | 1 | 319 | 24 | 延安 | 2 |
| 24 | 1 | 陕西 | 1 | 320 | 24 | 榆林 | 2 |
+
10 rows in set (0.01 sec)
select * from areas as province inner join areas as city on city.pid = province.aid having province.atitle = "西安";
+
| aid | pid | atitle | types | aid | pid | atitle | types |
+
| 311 | 24 | 西安 | 2 | 2596 | 311 | 莲湖区 | 3 |
| 311 | 24 | 西安 | 2 | 2597 | 311 | 新城区 | 3 |
| 311 | 24 | 西安 | 2 | 2598 | 311 | 碑林区 | 3 |
| 311 | 24 | 西安 | 2 | 2599 | 311 | 雁塔区 | 3 |
| 311 | 24 | 西安 | 2 | 2600 | 311 | 灞桥区 | 3 |
| 311 | 24 | 西安 | 2 | 2601 | 311 | 未央区 | 3 |
| 311 | 24 | 西安 | 2 | 2602 | 311 | 阎良区 | 3 |
| 311 | 24 | 西安 | 2 | 2603 | 311 | 临潼区 | 3 |
| 311 | 24 | 西安 | 2 | 2604 | 311 | 长安区 | 3 |
| 311 | 24 | 西安 | 2 | 2605 | 311 | 蓝田县 | 3 |
| 311 | 24 | 西安 | 2 | 2606 | 311 | 周至县 | 3 |
| 311 | 24 | 西安 | 2 | 2607 | 311 | 户县 | 3 |
| 311 | 24 | 西安 | 2 | 2608 | 311 | 高陵县 | 3 |
+
13 rows in set (0.01 sec)
子查询
select max(height) from students;
+
| max(height) |
+
| 190.00 |
+
1 row in set (0.00 sec)
select * from students where height = (select max(height) from students);
+
| id | name | age | height | gender | cls_id | is_delete |
+
| 13 | 小五 | 13 | 190.00 | 女 | 3 | |
+
1 row in set (0.00 sec)
select * from areas where pid = (select aid from areas where atitle = "陕西");
+
| aid | pid | atitle | types |
+
| 311 | 24 | 西安 | 2 |
| 312 | 24 | 安康 | 2 |
| 313 | 24 | 宝鸡 | 2 |
| 314 | 24 | 汉中 | 2 |
| 315 | 24 | 商洛 | 2 |
| 316 | 24 | 铜川 | 2 |
| 317 | 24 | 渭南 | 2 |
| 318 | 24 | 咸阳 | 2 |
| 319 | 24 | 延安 | 2 |
| 320 | 24 | 榆林 | 2 |
+
10 rows in set (0.00 sec)
数据库设计
数据库逻辑设计之三大范式通俗理解,一看就懂,书上说的太晦涩
- 维基百科的表格
后记
- 在这里,更加深刻的体会到:网络编程、服务器与数据库的关系了,推动互联网的发展尤为重要。
|