第一章
这是我参与更文挑战的第16天,活动详情查看: 更文挑战
数据库系统概述
数据(data),数据库(DataBase,DB),数据库管理系统(DBMS)和数据库系统是数据库技术密切相关的4个基本概念。
数据库系统的特点:
1.数据结构化
2.数据的共享性高,冗余度低易扩充
3.数据独立性高
4.数据库由数据库管理系统统统一管理和控制
数据管理技术经历了人工管理,文件系统,数据库系统三个阶段。
cls(清除上面的内容)
一。在CMD中打开
MsSql的登录与退出
-D ,--database=name 打开指定数据库
--delimiter = name 指定分隔符
-h, --host = name 服务器名称
-p, --password[=name] 密码
-P, --port=# 端口号
--prompt = name 设置提示符
-u , --user=name 用户名
-v, --version 输出版本信息并且退出
mysql登录 mysql -uroot -p -h127.0.0.1 -P3306
mysql退出 exit; quit; \q;
修改mysql提示符
1.连接客户端通过参数指定 mysql -uroot -proot -prompt 提示符
2.连接上客户端后通过prompt命令修改
prompt 提示符
promp /u
mysql 提示符
\D :完整的日期
\d :当前数据库
\h : 服务器名称
\u : 当前用户
mysql常用命令
显示当前服务器版本
select version();
显示当前日期时间
select now()
显示当前用户
select user()
1.1 数据模型
模型:是对现实世界的模拟和抽象
数据库模型分为四种:
层次模型,网关模型,关系模型,面向对象模型。
第二章 mysql(关系型数据库)
1.查询数据库服务器中所有的数据库
show databases;
2.选中某一个数据库进行操作
use 数据库名称
3.退出数据库
exit;
4.数据库中创建我们的数据库
create database 数据库名;
5.查看某个数据库中所有的数据表,查看数据表是否创建成功。
show tables;
6.创建一个数据表
create table 数据表名()
CREATE TABLE cctv(name VARCHAR(20),OWNER VARCHAR(20),species VARCHAR(20),sex CHAR(1),birth DATE,death DATE);
7.查看创建好的数据表的结构
describe 数据表名字; desc
8.查看数据表中的记录
select * from 数据表名字;
9.向数据表中添加数据记录
insert into 数据表名字 values(添加内容);
10.删除数据
delete from 数据表名字 where 条件;
11.修改数据
update 数据表名字 set 要修改的东西 where 条件;
update a set age=18 where name="b";
12. 删除数据表:drop table [is exists] 表1,表2,表3...;
drop table 数据表名字;
13. 排除重复 distinct
select distinct 查找元素 from 数据表名;
14 查询区间 between...and...
scort:成绩表 degree:成绩
select * from scort where degree between 60 and 80;
或者 select * from score where degree>60 and degree<80
15 表示或者关系的查询 in
查询score表中成绩为85,86,88,
select * from score where degree in(85,86,88)
16 or 表示或者
where c="c" or cb="cb";
17 升序,降序
降序:select * from student order by class desc ;
升序: select * from student order by class asc;
order by
desc:降序 asc:升序
18. 统计 count
select count(*) from student where class=9511;
19.最高分 max
select max()
20. limit
第一个数子表示从多少开始,第二个数字表示查多少条
limit 0,2;
select * from new limit 0,2;
21. 平均值
select avg( )
22. group by 分组
select * from new group by name;
如果group by后面跟条件的话 加个having
select * from new group by id having id=3;
23.多表查询
select s_name,c_no,sc_degree from student,score where student.s_no =score.s_no;;
24.三表关联查询
as:给表从新命名
select sname,student.sno as stu_sno where 条件
25.year函数与带in关键字的子查询。
year:选择年份
select year(s_birthday) from student;
26. union 求并集(就是两个表连在一起)
select c_name from course
union
select c_no from course;
27.大于其中至少一个,any
select t_no from course where t_no >any(select t_no from course);
28.all()表示所有
select all(t_no) from course;
29. not , like ,%,_
not:不等于
like:比较字符
王%:只要第一个字符是王的
王_:_取一个字符
30. 当前年份new
select year(now());
31. 添加字段:alter table 表名 add 新字段名 数据类型;
alter table book add abc int;
31.1 添加有完成约束条件的字段:alter table 表名 add 新字段名 数据类型 约束;
alter table book add abc int default '0';
31.2 在表的第一列添加一个字段:alter table 表名 add 新字段名 数据类型 first;
alter table book add def int first;
31.3 在表的指定列添加一个字段:alter table 表名 add 新字段名 数据类型 after 字段名;
alter table book add ggg int after def;
32.删除字段:alter table 表名 drop 字段名;
alter table MyClass drop name;
33.列的定义修改
alter table 数据库外 modify 修改的列名 修改的类型;
alter table bumen modify name int;
after 列名: 放到那个列的后面
alter table bumen modify name varchar(5) after dianhua;
34. 修改表名:alter table 旧表名 rename to 新表名;
alter table bumen rename to bumen01;
35. 修改字段的数据类型:alter table 表名 modify 字段名 数据类型;
alter table book modify name varchar(10);
36.修改字段名:alter table 表名 change 旧字段名 新字段名 新数据类型;
alter table book change jli_name j_name varchar(6);
37. 修改字段的排列位置:alter table 表名 modify 字段1 数据类型 first | after 字段2;
alter table book modify abc int first;
38. 更改表的存储引擎:alter table 表名 engine=新引擎名;
39. 删除被其他表关联的主表:先删除外键约束,再删表;
常用的数据类型
a).数据类型
类型 大小 范围(有符号) 范围(无符号) 用途
TINYINT 1 字节 (-128,127) (0,255) 小整数值
SMALLINT 2 字节 (-32 768,32 767) (0,65 535) 大整数值
MEDIUMINT 3 字节 (-8 388 608,8 388 607) (0,16 777 215) 大整数值
INT或INTEGER 4 字节 (-2 147 483 648,2 147 483 647) (0,4 294 967 295) 大整数值
BIGINT 8 字节 (-9,223,372,036,854,775,808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) 极大整数值
FLOAT 4 字节 (-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38) 0,(1.175 494 351 E-38,3.402 823 466 E+38) 单精度 浮点数值
DOUBLE 8 字节 (-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 双精度 浮点数值
DECIMAL 对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2 依赖于M和D的值 依赖于M和D的值 小数值
b).日期和时间
类型 大小(字节) 范围 格式 用途
DATE 3 1000-01-01/9999-12-31 YYYY-MM-DD 日期值
TIME 3 '-838:59:59'/'838:59:59' HH:MM:SS 时间值或持续时间
YEAR 1 1901/2155 YYYY 年份值
DATETIME 8 1000-01-01 00:00:00/9999-12-31 23:59:59 YYYY-MM-DD HH:MM:SS 混合日期和时间值
TIMESTAMP 4 1970-01-01 00:00:00/2038
c).字符串类型
类型 大小 用途
CHAR 0-255字节 定长字符串
VARCHAR 0-65535 字节 变长字符串
TINYBLOB 0-255字节 不超过 255 个字符的二进制字符串
TINYTEXT 0-255字节 短文本字符串
BLOB 0-65 535字节 二进制形式的长文本数据
TEXT 0-65 535字节 长文本数据
MEDIUMBLOB 0-16 777 215字节 二进制形式的中等长度文本数据
MEDIUMTEXT 0-16 777 215字节 中等长度文本数据
LONGBLOB 0-4 294 967 295字节 二进制形式的极大文本数据
LONGTEXT 0-4 294 967 295字节 极大文本数据
mysql服务的启动与停止
启动:net start mysql
关闭:net stop mysql
第三章
mysql建表约束
主键约束
它能够唯一确定一张表中的一条记录,也就是我们通过某个字段添加约束,就可以使得改字段不重复且不为空。
create table user( id int primary key , name varchar(20));
联合主键:只要值加起来不重复就可以
create table user( id int , name varchar(20),primary key(id,name))
加了primary key 不重复不为空
自增约束
create table user3(id int primary key auto_increment,name varchar(20));
insert into user3(name) values("cc");
加 primary key auto_increment 从1开始自动增长,每条数据加一
!!!!!如果我们创建表的时候,没有添加主键。
create table user(id int,name,varchar(2));
alter table user add primary key(id);
!!!!!!删除主键
alter table user drop primary key;
!!!!!!修改主键
alter table user modify id int primary key;
唯一约束
约束修饰的字段的值不可以重复
create table user(id int,name varchar(20);
add unique:添加唯一约束
alter table user add unique(name);
建表添加约束 create table user(id int ,name varchar(20),unique(name);
uniques(id,name);
添加唯一约束
alter table user modify name varchar(20) unique;
删除唯一约束
alter table user drop index name;
非空约束
修饰的字段不能为空 NULL
create table user(id int,name varchar(20) not null);
默认约束
插入字段值的时候,如果没有传值,就会使用默认值。
create table user(id int,name int default 10));
insert into uservalues(1,2); 如果传入了值,就不会使用默认值。
外键约束
涉及两个表:父表,子表
主表,副表。
create table classes(id int primary key,name varchar(20));
create table students(id int primary key,name varchar(20),class_id int,foreign key(class_id) references classes(id));
create table stude1(name1 int primary key,scort int,ccc_name varchar(20),foreign key(ccc_name) references ccc(name));
用foreign key(选择的外键) references 关联主表名(关联的键)
结语
文章篇幅较长,给看到这里的小伙伴点个大大的赞!由于作者水平有限,文章中难免会有错误之处,欢迎小伙伴们反馈指正。
如果觉得文章对你有帮助,麻烦 点赞、评论、收藏
你的支持是我最大的动力!!!