1.1 数据库简介
一.分类:
1. 关系型数据库(SQL) 存储方式固定,安全
?
2. 非关系型数据库(NoSQL (Not Only SQL))
存储方式比较灵活,存储数据的效率比较高, 不太安全
-
2.非关系型数据库(MongoDB) 语法基本不一样
{ id:1, 姓名:‘王刚”,sex: ‘男’, age: 2日, tel :13811371377,增加项目:{ XXX:xxX, xXX: xXX,}
}
二.MySQL是关系型数据库管理系统之一:
? 关系型数据库管理系统:采用关系模型(二维表结构)
? 来组织管理数据的数据库系统。
?
把数据保存在不同的表中,而不是将数据放在一个大仓库中。
?
MySQL不是数据库,它是数据库管理软件
?
一个MySQL 可以创建多个数据库 每个数据库可以创建多个表
每个表有多个字段
三.MySQL表中的数据:
先创建字段 在插入信息
?
1.2 MySQL库/表级操作
1.MySQL进入与退出
mysql –uusername -ppassword
mysql> exit
2.库级操作语句
1.显示所有的库:show databases;
2.创建库:create database [if not exists] db_name;
重复创建会报错, 可以加上if not exists
?
3.删除库:drop database [if exists] db_name;
如果不知道数据库是否存在,记得加if exists
?
4.进入数据库:use db_name;
3.表级操作语句
1.显示所有的表:show tables;
2.创建表:create table [if not exists] tb_name (create definition…);
重复创建会报错, 可以加上if not exists
?
3.显示创建表的信息:show create table tb_name;
4.删除表:drop table tb_name;
5.细节表字段 :desc tb_name;
4.注意事项
1.大小写:不严格区分大小写,默认大写为程序代码,
小写为程序员写的代码但是 插入的数据,字段区分大小写
?
2.语句结束符:每个语句都以;或者G结束
3.类型:强制数据类型,任何数据都有自己的数据类型
4.逗号:创建表的时候最后一行不需要逗号
?
?
1.create table student (
? name varchar(20),age int,sex char(20)
? );
2.show create table student;
?
?
?
1.3 MySQL表中数据的操作
1.插入数据:
1.指定字段插入:
INSERT INTO tb_name(field_name) VALUES (‘field_values‘);
默认为空
2.全字段插入:
INSERT INTO tb_name VALUES (‘all_values‘);
3.多行插入:
INSERT INTO tb_name(field_name) VALUES (value_1),(value_2),…;
2.查询数据:
1.全字段查询:
SELECT * FROM tb_name;
2.指定字段查询:
SELECT field_names,field_age FROM tb_name;
3.带条件的查询:
SELECT field_names FROM tb_name WHERE conditions;
3.修改数据:
1.修改所有数据:
UPDATE tb_name SET field_1=value_1
2.修改多个:
UPDATE tb_name SET field_1=value_1,field_2=value_2 …;
3.修改满足条件的数据:
UPDATE tb_name SET field_1=value_1 WHERE conditions;
注意:一定要写where条件,不然会修改表中全部数据
?
4.删除数据:
1.删除表中所有数据:
DELETE FROM tb_name;
2.删除表中满足条件的数据:
DELETE FROM tb_name WHERE conditions;
注意:一定要写where条件,不然会删除表中全部数据
?
?
1.insert into student values(‘爱我‘,25);
2.insert into student(name) values(‘爱我‘,25);
3.insert into student values(‘爱我‘,25),(‘中华‘,18);
?
4.select name,age from student;
?
5.select name,age from student where name=‘‘ ;
?
6.update student set age=20 where name=‘‘;
?
?
?
?
1.4 MySQL数据类型
1.数值类型 :
数值类型 ? ? 特性
?
TINYINT 用一个字节存放整数(0,255)
?
SMALLINT 两个字节(0,65535)
?
MEDIUMINT 三个字节(0,16777215)
?
INT 四个字节(0,4294967295 )
?
B IGINT 八个字节
?
FLOAT(M,N) 单精度浮点型(4个字节)
?
DOUBLE(M,N) 双精度浮点型,m总个数,d小数位(8字节 )
?
2.字符类型 :
字符类型 ? ? 特性
?
#char(size) 保仔固定长度的字符串(可包含字母、数字
以及特殊字符)。在括号中指定字符审的长度
最多255个字符
#VARCHAR(size) ? 保存可变长度的字符串(可包含字母、数字以及特
殊字符)。在括号中指定字符中的最人长度。最多
255个字符。如果值的长度人干255,
则被转换为TEXT类型。
TINYTEXT/TINYBLOB ? ? 用来存放较短文本数据!
二进制数据,最多255个字符
?
TEXT /BLOB 用来存放长文本数据/存放二进制
数据,最多65535个字符
LONGT EXT/LONGBLOB 同上,区别在与可以存放的
数据最大为4294967295个字符
?
ENUM ENUM类型的数据实际是一个包含多 个固定值的
列表,贝能选择这些值(包括NULL值)。例如,
如果希望某个字段包含"A”、"B" 和"C",必须
这样定义: ENUM (‘A‘,‘B’‘, ‘C‘),只有这
些值(或NULL值)能够填充到该字段中。
3.时间日期类型:
时间日期类型 ? ? ? ? 特性
date 日期格式: 2014-09-18
time 时间,格式: 08:42:30
datetime 日期时间,格式: 2014-09-18 08:42:30
timestamp 自动存储记录修改的时间
year 存放年
?
create table tb2(
? ? id INT,
? ? name VARCHAR(20), #指定长度,最多65535个字符。 变长字符串
? ? sex CHAR(4),? ? ? ? #指定长度,最多255个字符。 ? 定长字符串
? ? price DOUBLE(4,2), #双精度浮点型,m总个数,d小数位
? ? detail text, #可变长度,最多65535个字符
? ? dates DATETIME, #日期时间类型 YYYY-MM-DD HH:MM:SS
? ? ping ENUM(‘好评‘,‘差评’) #枚举, 在给出的value中选择
);
insert into tb values (1,‘裤子‘,‘男‘,20.0,‘这条裤子超级好!!!‘,now(),‘好评‘);
?
create table su(
id int,
name varchar(20),
age tinyint,
sex enum(‘M‘,‘F‘));
?
?
1.5 MySQL数据库(二)
1.筛选条件
?
-
2.逻辑运算符:
AND 与 OR 或 NOT 非
?
-
6.模糊查询: like ‘%’
-
任意多个字符: %
-
任意一个字符: _
-
7.范围查询:
-
连续范围: BETWEEN a AND b a <= value <= b
-
间隔返回: IN a in (10,20,30 […])
?
?
?
select * from student where id <>3;
select * from student where age is NULL ;
select * from student where class="1" and id=3 ;
select * from student order by class;
select * from student limit 3;
select * from student limit 3,4;
?
select distinct * from student ;
?
select * from student where name like ‘李%‘;
? select * from student where name like ‘李__‘;
? select * from student where id between 2 and 4;
select * from student where id in (1,3,5)
?
?
?
?
?
2.聚合与分组(重点和难点)
-
2.分组查询(group by):
SELECT group_column,aggregations FROM tb_name group BY group_column;
在分组的情况下,只能够出现分组字段和聚合字段 其他的字段没有意义,会报错!
-
3.聚合筛选(having): (在聚合筛选后在筛选)
SELECT group_column,aggregations FROM tb_nameGROUP BY group_column HAVING conditions; 加having条件表达式,可以对输出的结果做出限制
-
4.执行顺序:
先是执行:where 然后执行:聚合函数和别名 最后执行:having
?
?
select count(age) from student;
select class,group_concat(name) from student group by class;
select age,group_concat(name) from student group by age;
select age,group_concat(name) from student group by age having age <20;
?
?
3 连接查询(需要多张表)
Mysql> select * from student [inner] join 表2 on student.id =表2.id;
?
-
3.外连接( {left | right} join ):
左外连接: (以左表为基准) 哪个重要放左边
两张表做连接的时候,在连接条件不匹配的时候 留下左表中的数据,而右表中的数据以NULL填充
mysql> select * from student left join 表2 on student.id= 表2.id;
?
?
1.6 MySQL数据库(三)
?
1.表结构修改
-
1.修改表名:
alter table tb_name rename to new_name;
-
2.修改字段名:
alter table tb_name change name new_name data_type;
-
3.修改字段类型:
ALTER TABLE tb_name MODIFY field_name data_type;
-
4.添加字段:
ALTER TABLE tb_name ADD [COLUMN] field_name data_type;
-
5.删除字段:
ALTER TABLE tb_name DROP [COLUMN] field_name;
?
?
1.alter table new_table rename to old_table;
2.alter table old_table change id stu_id int;
3.alter table old_table modify stu_id tinyint;
?
4.alter table old_table add name varchar(20);
?
5.alter table old_table drop name ;
?
?
2.约束条件 (所有条件都要写全 覆盖关系)
1.默认约束(default):
插入数据的时候,如果没有明确为字段赋值,则自动赋予默认值 在没有设置默认值的情况下,默认值为NULL
CREATE TABLE tb(
? id int default ‘a’,
? name varchar(20)
);
2.非空约束(not null)
限制一个字段的值不能为空, Insert的时候必须为该字段赋值空字符不等于NULL
CREATE TABLE tb(
? id int not null,
? name varchar(20)
);
?
3.唯一约束(unique key)
限制一个字段的值不重复 该字段的数据不能出现重复的确保字段中值的唯一
CREATE TABLE tb(
? id int unique key,
? name varchar(20)
);
4.主键约束(primary key)
通常每张表都需要一个主键来体现唯一性 每张表里面只能有一个主键 主键 = 非空 + 唯一
CREATE TABLE tb(
? id int primary key,
? name varchar(20)
);
?
5.自增长约束(auto_increment)
自动编号,只能和主键组合使用, 一个表里面只能有一个自增长 auto_increment 要求用在主键上
CREATE TABLE tb(
? id int auto_increment,
? name varchar(20)
);
6.外键约束(foreign key)
保持数据的一致性 我有的你一定有, 你没有的, 我绝对没有
-
B表中的id_b字段,只能添加 id_a中已有的数据。
-
A表中id_a 被参照的数据, 不能被修改和删除
CREATE TABLE a( id_a int primary key, name varchar(20) );
CREATE TABLE b( id_b int primary key, name varchar(20), foreign key (id_b) references a(id_a) );
?
?
?
1.CREATE TABLE tb(
? id int default ‘0’,
? name varchar(20) default ‘无名‘
);
insert into tb(id,name) values(1,‘撒旦‘);
insert into tb(id) values(2);
insert into tb(name) values(‘阿斯顿‘);
insert into tb values(default,default);
?
###########################
2. alter table tb modify id int not null default 0;
?
?
?
?
3.表关系(通过外键实现 主表字段必须唯一)
1.一对一
一个学生只有一条详细信息
用外键的方式, 把两个表的主键关联
?
2.一对多
学院与学生之间的关系就是一对多的关系,通过外键关联来实现这种关系。
?
insert into department values(1,‘外语学院‘),(2,‘计算机学院‘);
?
insert into student values(1,‘佳能‘,‘lucky‘,1);
3.多对多
学生要报名选修课,一个学生可以报名多门课程,一个课程有很多的学生报名,那么学生表和课程表两者就形成了多对多关系。
?
对于多对多关系,需要创建中间表实现。
insert into cours values (1,’python编程’),’大学英语’),(3,‘音乐鉴赏’);
?
insert into `select` values(1,3); ? 表示学号为一的同学选择了音乐鉴赏这门课程
?
?
?
?
?
?
##创建学院表
create table department(
? d_id int primary key auto_increment,? ? # 学院id
? d_name varchar(20) not null ? ? ? ? # 学院名
);
?
?
?
##创建学生表
?
create table student(
? s_id int primary key auto_increment,? # 学生id
? s_name varchar(20) not null,? ? ? ? ? # 学生名字
? dept_id int not null, ? ? # 所属学院 id
? foreign key(dept_id) references department(d_id) #外键
?
#建立详细学生表:
create table student_details(
? id int primary key,
? sex varchar(20) not null,
? age int,
? address varchar(20) comment ‘家庭住址‘,
? parents varchar(20),
? home_num varchar(20),
?
? foreign key (id) references student(s_id)
);
);
?
?
#建立课程表:
?
create table cours(
? cours_id int primary key auto_increment,
? cours_name varchar(20) not null
);
?
?
?
# 选课表 (中间表)
?
create table select(
? s_id int,? ? ? #用来记录学生id
? cours_id int, ? ? #用来记录 课程id
? primary key(s_id,cours_id),? # 联合主键
?
? foreign key(s_id) references student(s_id),? ? ? # 关联学生id
?
? foreign key(cours_id) references cours(cours_id) # 关联 课程id
);
?
?
?
select * from institute join student on institute.id = student.ins_id;
select student.name,course.name from student join sel join course on student.id = sel.s_id and course.id = sel.c_id;
select student.name,course.name from student,sel,course where student.id = sel.s_id and course.id = sel.c_id;
?