数据库
一、数据类型
- 网状数据库:用有向图结构表示实体类型及实体间联系的数据模型
- 关系型数据库:认为数据库存储的表和表之间是有关系的-----MySQL
- MySQL:体积小、速度快、成本低、可移植性、开源。
二、 数据的完整性:存储在数据库中的护具应该保持准确性和可靠性。
分类:
实体完整性(行):对应关系中的记录唯一性,定义表中的所有行唯一的标识。 主属性不能为空,不能有相同值,一般使用主键、unique、唯一索引实现。
主键:
表中一个或多个字段,用于唯一标识表中某一条记录。创建时指定。手动添加或代码{直接加primary key\constant 约束名 primary key()\primary key (约束名)}
修改约束–alter
1、 alter table 表名 modify 列名 数据类型 primary key;
2、alter table 表名 ADD primary key (列);
3、alter table 表名 ADD constant 指定约束名 primary 可以(列);
4、删除主键约束:alter table 表名 drop primary key;
唯一约束:
1、不能重复 、可为多个空
2、如果不给唯一约束名称,就默认和列名相同
3、MySQL会给唯一约束的列会默认创建一个唯一索引
eg:
1、creat table temp(
id int not null,
name varcher(25)((或者直接加unique)),
constraint uk_name_pwd unique(name)
);
2、 alter table temp add unique(name,passwd);
3、 alter table temp modify name varchar(25)
unique;------修改唯一约束
alter table temp drop index name;删除约束
域完整性:是对数据表中字段属性的约束,限制数据类型,确保不会输入无效值
添加默认约束:
1、 在创建表时添加:
create table 表名(项 类型 default 3);
2、 通过alter语句添加:
alter table 表名 modify 项 类型 default 2;
3、删除默认约束:
alter table 表名 modify 项 类型;
参照完整性—表与表之间的数据参照引用,使用外键约束使用(外键必须能找到或为空)
1、当主表的记录被从表参照时,主表的记录不允许删除
2、如果要删除数据,需先从表中删除依赖该记录的数据
外键约束:constraint 约束名 foreign key (约束项)reference 表名(引用项)
或者:alter table 表名 add constraint 约束名 foreign key
三、 语法
1.create table 表名{
属性名 数据类型 【约束条件】,
属性名 数据类型 【约束条件】,
属性名 数据类型 【约束条件】
};
eg:creat table student{
id int primary key,
name varchar(10) not null,
age tinyint unsigned default 0,
height decimal(5,2),
gender enum(“male”,“famale”)
};
2.
四、CMD中mysql的使用
4.1 在cmd中查看初始数据库信息及命令
a.-在CMD中run mysql -u root -p,(如无密码)回车,进入MariaDB/mysql;
这里我进入的是MariaDB,两者的基本区别:
a.1–MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.
a.2–MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品.
b.-输入‘show databases ;’ <记得分号> 然后出现以下界面
c.-use mysql ## 开始进入mysql—已从’none’ 切换为‘mysql’,这时无“;”
notes: 使用use + 数据库名,进错库时使用“exit;” 退出
我:use test
d.-show tables; ## 显示数据库中的表
e.-show * from mysql.user; ## 查询mysql库下user表中的数据(出现很多很迷)
f.-desc user; # 查看user表中的数据结构,such as 下图
g.- flush privileges; # 刷新数据库信息命令
h.-select host,user,password from user; #查看user表中host,user,password信息
i.-select host,user,password from user where host="::1"; # 查询表中字段有关“::1”的数据
4.2 传说中的增删改查
A.-新建数据表
a.1–Create table Person(表名) (
属性名 数据类型 【约束条件】,
属性名 数据类型 【约束条件】,
属性名 数据类型 【约束条件】
);
a.2–desc Person(表名);
显示数据表的结构b.新增列
alter table person(表名) add work[列名] varchar(255)[类型];
c.删除列
alter table person[表名] drop column work[列名];
d.查询所需列数据
Select * from people; ##如果数据库数据多,使用此查询费时
select * from people where Id_P=02;
e.修改表名
rename table people[旧表名] to person[新表名];
e.1–修改某一项数据值,如修改Id
update person set Id_p=5 where LastName=‘Nana’;
1、ORDER BY+ 排序:DESC:降序 ASC:升序
select * from 表名 order by 字段名 ASC/DESC;
2、GROUP BY :结果集进行分组
select 列名A,count() from 表名 group by 列名A;
3、Having
a、在group by 之前筛选用where,筛选的是分组前的数据
b、在group by 之后筛选用having,筛选的是分组后的数据
c、HAVING子句仅用于*带有GROUP BY子句的查询中
d、having后面跟的条件判断的字段必须是聚合函数返回的结果,否则sql会报错
4、JION*
3.1 INNER JION:内连接 查询两表字段匹配关系的数据
select * from 表 a,表 b where a.列名=b.列名;
3.2 LEFT JION:左连接,以左表为主表,获取左表所有记录
select a. from 表 a LEFT JION 表 b ON where a.列名=b.列名;(以a 表为左表)
3.3 RIGHT JION:右连接,以右表为主表,获取右表所有记录*
select b.* from 表 a RIGHT JION 表 b ON where a.列名=b.列名;(以a 表为左表)
5、