MySQL 02 初体验

MySQL 02 初体验

一、学习目标
  1. 掌握MySQL在Windows系统中的安装方法
  2. 学会使用Navicat数据库管理工具
  3. 掌握MySQL命令行操作数据库常用命令
  4. 掌握创建库的SQL语句
  5. 掌握创建数据表以及数据字段的类型和属性
二、MySQL简介

在这里插入图片描述

MySQL 是最流行的关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的 RDBMS(Relational Database Management System:关系数据库管理系统)应用软件之一。

MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司。MySQL 是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。

MySQL 是开源的。
MySQL 支持大型的数据库。可以处理拥有上千万条记录的大型数据库。
MySQL 使用标准的 SQL 数据语言形式。
MySQL 可以运行于多个系统上,并且支持多种语言。这些编程语言包括 C、C++、Python、Java、Perl、PHP、Eiffel、Ruby 和 Tcl 等。
MySQL 支持大型数据库,支持 5000 万条记录的数据仓库,32 位系统表文件最大可支持 4GB,64 位系统支持最大的表文件为8TB。
MySQL 是可以定制的,采用了 GPL 协议,你可以修改源码来开发自己的 MySQL 系统。

MySQL的优势

  1. 运行速度快
  2. 使用成本低
  3. 可移植性强
  4. 适用用户广

MySql下载地址:https://dev.mysql.com/downloads/installer/

三、MySQL服务启动与关闭

方式一: 打开系统开始菜单——>搜索 “服务” 或者在"运行" 中输入services.msc打开Windows系统服务

在这里插入图片描述

在这里插入图片描述

方式二: 使用cmd命令启动

net start mysql服务名字

在这里插入图片描述

cmd停止MySQL服务命令:

在这里插入图片描述

更改cmd默认以管理员的身份运行

在这里插入图片描述

四、MySQL登录

方式一:使用MySQL Command Line Client

在这里插入图片描述
在这里插入图片描述

方式二:使用DOS

前提要求:需要配置好MySQL环境变量 PATH:

在这里插入图片描述

服务启动语法:

mysql –h服务器主机地址 –u 用户名 –p密码

命令: mysql -uroot -p 按回车键

C:\WINDOWS\system32>mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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>

启动常见错误:

在这里插入图片描述

错误原因: 服务未启动

显示所有数据库:

show databases;
#选中数据库
use 数据库的名称

显示所有表:

show tables;

回顾常用的sql语句

#查询
select * from 表名;
select * from 表名 [where 查询条件]; #and or
select * from 表名 [where 查询条件]
[order by 排序列 (ASC|DESC)]
#新增
insert into 表名(列名1,列名2,列名n)
values ('值1','值2','值n');
#删除
delete from 表名 [where 删除条件]
#修改
update 表名 set 列名1='值1',列名2='值2',列名n='值n'
[where 修改条件]

注意事项:

  • 连接MySQL服务前必须先检查服务是否启动

  • 启动方式有两种,可以点击右键-属性,在属性窗口中设计 ,也可以在DOS中使用net start mysql命令启动服务

  • 如何修改了配置文件my.ini,密码重启服务后才能生效

五、MySQL配置文件

当打开my.ini文件查可以看几个常用配置参数
通过修改MySQL配置文件来手动配置数据库服务器MySQL时,常用的参数如下。

  1. default-character-set:客户端默认字符集
  2. character-set-server:服务器端默认字符集
  3. port:客户端和服务器端的端口号 3306
  4. default-storage-engine:MySQL默认存储引擎

my.ini配置信息:

[client]
# pipe=

# socket=MYSQL
port=3306

[mysql]
no-beep
# default-character-set=

# SERVER SECTION
# ----------------------------------------------------------------------
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this 
# file.
# server_type=3
[mysqld]

# The next three options are mutually exclusive to SERVER_PORT below.
# skip-networking
# enable-named-pipe
# shared-memory

# shared-memory-base-name=MYSQL

# The Pipe the MySQL Server will use
# socket=MYSQL

# The TCP/IP Port the MySQL Server will listen on
port=3306

# Path to installation directory. All paths are usually resolved relative to this.
# basedir="C:/Program Files/MySQL/MySQL Server 8.0/"

# Path to the database root
datadir=C:/ProgramData/MySQL/MySQL Server 8.0\Data

# The default character set that will be used when a new schema or table is
# created and no character set is defined
# character-set-server=

# The default authentication plugin to be used when connecting to the server
default_authentication_plugin=caching_sha2_password

# The default storage engine that will be used when create new tables when
default-storage-engine=INNODB

# Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"

# General and Slow logging.
log-output=FILE

general-log=0

general_log_file="LAPTOP-M7EOS388.log"

slow-query-log=1

slow_query_log_file="LAPTOP-M7EOS388-slow.log"

long_query_time=10

# Error Logging.
log-error="LAPTOP-M7EOS388.err"

# ***** Group Replication Related *****
# Specifies the base name to use for binary log files. With binary logging
# enabled, the server logs all statements that change data to the binary
# log, which is used for backup and replication.
log-bin="LAPTOP-M7EOS388-bin"

# ***** Group Replication Related *****
# Specifies the server ID. For servers that are used in a replication topology,
# you must specify a unique server ID for each replication server, in the
# range from 1 to 2^32 − 1. “Unique” means that each ID must be different
# from every other ID in use by any other source or replica.
server-id=1

# ***** Group Replication Related *****
# The host name or IP address of the replica to be reported to the source
# during replica registration. This value appears in the output of SHOW REPLICAS
# on the source server. Leave the value unset if you do not want the replica to
# register itself with the source.
# report_host=0.0

# NOTE: Modify this value after Server initialization won't take effect.
lower_case_table_names=1

# Secure File Priv.
secure-file-priv="C:/ProgramData/MySQL/MySQL Server 8.0/Uploads"

# The maximum amount of concurrent sessions the MySQL server will
# allow. One of these connections will be reserved for a user with
# SUPER privileges to allow the administrator to login even if the
# connection limit has been reached.
max_connections=151

# The number of open tables for all threads. Increasing this value
# increases the number of file descriptors that mysqld requires.
# Therefore you have to make sure to set the amount of open files
# allowed to at least 4096 in the variable "open-files-limit" in
# section [mysqld_safe]
table_open_cache=2000

# Maximum size for internal (in-memory) temporary tables. If a table
# grows larger than this value, it is automatically converted to disk
# based table This limitation is for a single table. There can be many
# of them.
tmp_table_size=76M

# How many threads we should keep in a cache for reuse. When a client
# disconnects, the client's threads are put in the cache if there aren't
# more than thread_cache_size threads from before.  This greatly reduces
# the amount of thread creations needed if you have a lot of new
# connections. (Normally this doesn't give a notable performance
# improvement if you have a good thread implementation.)
thread_cache_size=10

#*** MyISAM Specific options
# The maximum size of the temporary file MySQL is allowed to use while
# recreating the index (during REPAIR, ALTER TABLE or LOAD DATA INFILE.
# If the file-size would be bigger than this, the index will be created
# through the key cache (which is slower).
myisam_max_sort_file_size=100G

# The size of the buffer that is allocated when sorting MyISAM indexes
# during a REPAIR TABLE or when creating indexes with CREATE INDEX
# or ALTER TABLE.
myisam_sort_buffer_size=142M

# Size of the Key Buffer, used to cache index blocks for MyISAM tables.
# Do not set it larger than 30% of your available memory, as some memory
# is also required by the OS to cache rows. Even if you're not using
# MyISAM tables, you should still set it to 8-64M as it will also be
# used for internal temporary disk tables.
key_buffer_size=8M

# Size of the buffer used for doing full table scans of MyISAM tables.
# Allocated per thread, if a full scan is needed.
read_buffer_size=64K

read_rnd_buffer_size=256K

#*** INNODB Specific options ***
# innodb_data_home_dir=

# Use this option if you have a MySQL server with InnoDB support enabled
# but you do not plan to use it. This will save memory and disk space
# and speed up some things.
# skip-innodb

# If set to 1, InnoDB will flush (fsync) the transaction logs to the
# disk at each commit, which offers full ACID behavior. If you are
# willing to compromise this safety, and you are running small
# transactions, you may set this to 0 or 2 to reduce disk I/O to the
# logs. Value 0 means that the log is only written to the log file and
# the log file flushed to disk approximately once per second. Value 2
# means the log is written to the log file at each commit, but the log
# file is only flushed to disk approximately once per second.
innodb_flush_log_at_trx_commit=1

# The size of the buffer InnoDB uses for buffering log data. As soon as
# it is full, InnoDB will have to flush it to disk. As it is flushed
# once per second anyway, it does not make sense to have it very large
# (even with long transactions).
innodb_log_buffer_size=1M

# InnoDB, unlike MyISAM, uses a buffer pool to cache both indexes and
# row data. The bigger you set this the less disk I/O is needed to
# access data in tables. On a dedicated database server you may set this
# parameter up to 80% of the machine physical memory size. Do not set it
# too large, though, because competition of the physical memory may
# cause paging in the operating system.  Note that on 32bit systems you
# might be limited to 2-3.5G of user level memory per process, so do not
# set it too high.
innodb_buffer_pool_size=8M

# Size of each log file in a log group. You should set the combined size
# of log files to about 25%-100% of your buffer pool size to avoid
# unneeded buffer pool flush activity on log file overwrite. However,
# note that a larger logfile size will increase the time needed for the
# recovery process.
innodb_log_file_size=48M

# Number of threads allowed inside the InnoDB kernel. The optimal value
# depends highly on the application, hardware as well as the OS
# scheduler properties. A too high value may lead to thread thrashing.
innodb_thread_concurrency=17

# The increment size (in MB) for extending the size of an auto-extend InnoDB system tablespace file when it becomes full.
innodb_autoextend_increment=64

# The number of regions that the InnoDB buffer pool is divided into.
# For systems with buffer pools in the multi-gigabyte range, dividing the buffer pool into separate instances can improve concurrency,
# by reducing contention as different threads read and write to cached pages.
innodb_buffer_pool_instances=8

# Determines the number of threads that can enter InnoDB concurrently.
innodb_concurrency_tickets=5000

# Specifies how long in milliseconds (ms) a block inserted into the old sublist must stay there after its first access before
# it can be moved to the new sublist.
innodb_old_blocks_time=1000

# It specifies the maximum number of .ibd files that MySQL can keep open at one time. The minimum value is 10.
innodb_open_files=300

# When this variable is enabled, InnoDB updates statistics during metadata statements.
innodb_stats_on_metadata=0

# When innodb_file_per_table is enabled (the default in 5.6.6 and higher), InnoDB stores the data and indexes for each newly created table
# in a separate .ibd file, rather than in the system tablespace.
innodb_file_per_table=1

# Use the following list of values: 0 for crc32, 1 for strict_crc32, 2 for innodb, 3 for strict_innodb, 4 for none, 5 for strict_none.
innodb_checksum_algorithm=0

# The number of outstanding connection requests MySQL can have.
# This option is useful when the main MySQL thread gets many connection requests in a very short time.
# It then takes some time (although very little) for the main thread to check the connection and start a new thread.
# The back_log value indicates how many requests can be stacked during this short time before MySQL momentarily
# stops answering new requests.
# You need to increase this only if you expect a large number of connections in a short period of time.
back_log=80

# If this is set to a nonzero value, all tables are closed every flush_time seconds to free up resources and
# synchronize unflushed data to disk.
# This option is best used only on systems with minimal resources.
flush_time=0

# The minimum size of the buffer that is used for plain index scans, range index scans, and joins that do not use
# indexes and thus perform full table scans.
join_buffer_size=256K

# The maximum size of one packet or any generated or intermediate string, or any parameter sent by the
# mysql_stmt_send_long_data() C API function.
max_allowed_packet=4M

# If more than this many successive connection requests from a host are interrupted without a successful connection,
# the server blocks that host from performing further connections.
max_connect_errors=100

# Changes the number of file descriptors available to mysqld.
# You should try increasing the value of this option if mysqld gives you the error "Too many open files".
open_files_limit=4161

# If you see many sort_merge_passes per second in SHOW GLOBAL STATUS output, you can consider increasing the
# sort_buffer_size value to speed up ORDER BY or GROUP BY operations that cannot be improved with query optimization
# or improved indexing.
sort_buffer_size=256K

# The number of table definitions (from .frm files) that can be stored in the definition cache.
# If you use a large number of tables, you can create a large table definition cache to speed up opening of tables.
# The table definition cache takes less space and does not use file descriptors, unlike the normal table cache.
# The minimum and default values are both 400.
table_definition_cache=1400

# Specify the maximum size of a row-based binary log event, in bytes.
# Rows are grouped into events smaller than this size if possible. The value should be a multiple of 256.
binlog_row_event_max_size=8K

# If the value of this variable is greater than 0, a replica synchronizes its master.info file to disk.

# If the value of this variable is greater than 0, the MySQL server synchronizes its relay log to disk.
# (using fdatasync()) after every sync_relay_log writes to the relay log.
sync_relay_log=10000

# If the value of this variable is greater than 0, a replica synchronizes its relay-log.info file to disk.
# (using fdatasync()) after every sync_relay_log_info transactions.
sync_relay_log_info=10000

# Load mysql plugins at start."plugin_x ; plugin_y".
# plugin_load

# The TCP/IP Port the MySQL Server X Protocol will listen on.
loose_mysqlx_port=33060
六、MySQL安装目录

主要文件夹

  1. bin文件夹:该文件夹下存放着可执行文件
  2. include文件夹: 该文件夹下存放着头文件
  3. lib文件夹: 该文件夹下存放着库文件
  4. share文件夹:该文件夹下存放着字符集、语言等信息。
七、MySQL存储引擎

存储引擎的类型
MyISAMInnoDB 、Memory、CSV等9种

MyISAM与InnoDB类型主要区别

名称InnoDBMyISAM
事务处理支持不支持
数据行锁定支持不支持
外键约束支持不支持
全文索引不支持支持
表空间大小较大约2倍较小

适用场合

  • 使用 MyISAM: 不需事务,空间小,以查询访问为主
  • 使用 InnoDB: 多删除、更新操作,安全性高,事务处理及并发控制

查看当前默认存储引擎

show variables like 'storage_engine%'; 

修改存储引擎

  • 修改my.ini配置文件
default-storage-engine= InnoDB

设置表的存储引擎

#语法:
CREATE TABLE 表名(
 	#省略代码
)ENGINE=存储引擎;

#示例:
CREATE TABLE `myIsam` (
	id  INT(4)
)ENGINE=MyISAM;
八、MySQL运行机制

运行机制图解:
在这里插入图片描述

运行机制分析:

如上图所示,当应用程序发送一个查询请求到MySQL服务器时,如 select * from tableName ,从支持接口进来后,进入连接池后做权限、验证等环节,然后判断是否有缓存,有则直接放回结果,否则进入SQL接口,在查询之前查询优化器进行优化,最后进行解析,查询。并通过存储引擎与文件交互。

名词解释:

**1.支持接口:**不同的编程语言与SQL的交互

**2.连接池:**管理缓冲用户连接,线程处理等需要缓存的需求

**3.SQL接口:**接受用户的SQL命令,并且返回用户需要查询的结果。比如select from就是调用SQL接口

**4.解析器:**SQL命令传递到解析器的时候会被解析器验证和解析。解析器是由Lex和YACC实现的,是一个很长的脚本。
主要功能:
a . 将SQL语句分解成数据结构,并将这个结构传递到后续步骤,以后SQL语句的传递和处理就是基于这个结构的;例如将 select * from tablename where 1=1;分解为select、*、from、tablename、where 、1=1,并去解析。
如果在分解构成中遇到错误,那么就说明这个SQL语句是不合理的。

5.查询优化器:

SQL语句在查询之前会使用查询优化器对查询进行优化。他使用的是“选取-投影-联接”策略进行查询。
例: select id,name from user where gender = 1;
a.先根据where 语句进行选取,而不是先将表全部查询出来以后再进行gender过滤
b.先根据id和name进行属性投影,而不是将属性全部取出以后再进行过滤
将这两个查询条件联接起来生成最终查询结果。

6.缓存:

如果查询缓存有命中的查询结果,查询语句就可以直接去查询缓存中取数据。
这个缓存机制是由一系列小缓存组成的。比如表缓存记录缓存,key缓存,权限缓存等

7.存储引擎:

存储引擎是MySql中具体的与文件打交道的子系统。也是MySql最具有特色的一个地方。

MySql的存储引擎是插件式的。它根据MySql AB公司提供的文件访问层的一个抽象接口来定制一种文件访问机制(这种访问机制就叫存储引擎)。

现在有很多种存储引擎,各个存储引擎的优势各不一样,最常用的MyISAM,InnoDB,BDB。

MyISAM引擎,它查询速度快,有较好的索引优化和数据压缩技术。但是它不支持事务。

InnoDB支持事务,并且提供行级的锁定,应用也相当广泛。 Mysql也支持自己定制存储引擎,甚至一个库中不同的表使用不同的存储引擎,这些都是允许的

MySQL默认使用InnoDB存储引擎

九、系统数据库和用户数据库

安装完MySQL服务器后,MySQL会附带系统数据库,包括:

information_schema:主要存储系统中的一些数据库对象信息,如用户表信息、字段信息、权限信息、字符集信息和分区信息等。

performance_schema:主要存储数据库服务器性能参数

mysql:主要存储系统的用户权限信息

test:MySQL数据库管理系统自动创建的测试数据库,任何用户都可以使用

在这里插入图片描述

十、创库删库选择库

创库语法:

CREATE DATABASE 数据库名;

创库示例:

CREATE DATABASE hospitaldb;

删库语法:

DROP DATABASE 数据库名;

删库示例:

DROP DATABASE hospitaldb;

选择库语法:

USE 数据库名字;  

选择库示例:

USE hospitaldb;

注意: 分号为结束符,不可缺!

十一、结构化查询语言

SQL语言包括以下4个部分。
(1)DML(数据操作语言):用来插入、修改和删除表中的数据,如:INSERT、UPDATE、DELETE语句。
(2)DDL(数据定义语言):在数据库中创建或删除数据库对象等操作。如:CREATE、DROP、ALTER等语句。
(3)DQL(数据查询语言):用来对数据库中的数据进行查询,指SELECT语句。
(4)DCL(数据控制语言):用来控制数据库组件的存取许可、存取权限等,如GRANT、REVOKE等。

十二、MySQL数据类型——数值类型

在这里插入图片描述

UNSIGNED属性

  • 标识为无符号数

ZEROFILL属性

  • 宽度(位数)不足以0填充,假设id int(4)当数值为12时,则呈现:0012

注意:若某数值字段指定了ZEROFILL属性,将自动添加UNSIGNED属性

十三、MySQL数据类型——字符串、日期类型

在这里插入图片描述

注意:若某日期字段默认值为当前日期,一般设置为TIMESTAMP类型

十四、创表、删表

创表语法:

CREATE TABLE [IF NOT EXISTS]  表名 (
	字段1 数据类型 [字段属性|约束][索引][注释],
	 ……
	字段n 数据类型 [字段属性|约束][索引][注释]
)[表类型][表字符集][注释];

创表示例:

#创建学生表
CREATE TABLE `patient` (
`patientID` INT  PRIMARY KEY, #多字段使用逗号分隔
`address` VAVCHAR(255),#保留字用撇号括起来
……);

#单行注释:#......
#多行注释:/*……*/

删表语法:

DROP TABLE [IF EXISTS] 表名;

删表示例:

DROP TABLE IF EXISTS patient;
十五、字段的约束及属性

主键、默认、唯一约束

CREATE TABLE patient(
   `patientID` INT NOT NULL PRIMARY KEY,#主键设置
   `address` VAVCHAR(255) DEFAULT '地址不详',#默认值设置
   `identityNum` varchar(18)  UNIQUE KEY,#唯一约束
……);

添加字段和表注释

CREATE TABLE patient (
 `patientID` INT UNSIGNED COMMENT ‘病人编号’
)COMMENT='病人信息表’;

设置字符集编码

CREATE TABLE [IF NOT EXISTS] 表名(
  #省略代码
)CHARSET = 字符集名;
名称关键字说明
非空约束NOT NULL字段不允许为空
默认约束DEFAULT赋予某字段默认值
唯一约束UNIQUE KEY(UK)设置字段的值是唯一的 允许为空,但只能有一个空值
主键约束PRIMARY KEY(PK)设置该字段为表的主键 可唯一标识该表记录
外键约束FOREIGN KEY(FK)用于在两表之间建立关系, 需要指定引用主表的哪一字段
自动增长AUTO_INCREMENT设置该列为自增字段 默认每条自增1 通常用于设置主键
十六、创建病人信息表

病人信息表

#选择hospital数据库
USE hospital;
#创建Patient表
CREATE TABLE IF NOT EXISTS patient(
	patientID INT(4) NOT NULL COMMENT '病人编号' PRIMARY KEY AUTO_INCREMENT, #非空,主键,自增
	password VARCHAR(20) NOT NULL COMMENT '登录密码',
	birthDate DATE COMMENT '出生日期',
	gender VARCHAR(4) DEFAULT '男' NOT NULL COMMENT '性别',#非空,默认值“男”
	patientName VARCHAR(50) NOT NULL COMMENT '病人姓名',
	phoneNum VARCHAR(50) COMMENT '联系电话',
	email VARCHAR(70) COMMENT '邮箱',
	identityNum VARCHAR(20) UNIQUE KEY COMMENT '身份证号', #唯一
	address VARCHAR(255) DEFAULT '地址不详' COMMENT '地址' #默认值“地址不详” 
)COMMENT='病人表'; #表注释“病人表”

#查看数据库中的表
show tables;

#查看表结构
describe patient;
十七、MySQL8.0数据表的存储文件介绍

MyISAM类型表文件
*.sdi:表结构元数据
*.MYD:数据文件
*.MYI:索引文件
InnoDB类型表文件
*.ibd:数据文件

注意: 存储位置因操作系统而异,可查看 my.ini 文件

#数据库创建
create database t147myschool;
use t147myschool;
#表的创建(DDL)
create table grade(
	gradeId int not null comment'年级编号',
	gradeName varchar(20) comment '年级名称'
)comment='年级信息表';
#创建学生表
create table if not exists Student(
	StudentNo int not null primary key comment '学生学号',
	StudentName varchar(20) not null comment '学生姓名',
	Sex char(2) not null default '男' comment '性别',
	Birthday date  not null comment '出生日期',
	GradeId int not null comment '年级编号(外键)',
	Phone char(11) comment '手机号码',
	Address  varchar(100) default '地址不详' comment '地址',
	IDCard char(18) unique key comment '身份证号码'
)comment='学生信息表',engine=InnoDB;

#创建科目表
create  table  if not exists `subject`(
	subjectNo int primary key not null comment '科目编号',
	subjectName varchar(20) not null comment '科目名字',
	classHour int not null comment '课时',
	gradeId int not null comment '年级编号(外键)'
)comment ='科目信息表',engine=MYISAM;

在这里插入图片描述

十八、MySQL系统帮助

语法:

HELP 查询内容;

示例:

HELP contents;
HELP Data Types;
HELP INT;

dos命令运行示例:

Microsoft Windows [版本 10.0.19042.1586]
(c) Microsoft Corporation。保留所有权利。

C:\WINDOWS\system32>mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.26 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> HELP contents;
You asked for help about help category: "Contents"
For more information, type 'help <item>', where <item> is one of the following
categories:
   Account Management
   Administration
   Components
   Compound Statements
   Contents
   Data Definition
   Data Manipulation
   Data Types
   Functions
   Geographic Features
   Help Metadata
   Language Structure
   Loadable Functions
   Plugins
   Prepared Statements
   Replication Statements
   Storage Engines
   Table Maintenance
   Transactions
   Utility

mysql> HELP Data Types;
You asked for help about help category: "Data Types"
For more information, type 'help <item>', where <item> is one of the following
topics:
   AUTO_INCREMENT
   BIGINT
   BINARY
   BIT
   BLOB
   BLOB DATA TYPE
   BOOLEAN
   CHAR
   CHAR BYTE
   DATE
   DATETIME
   DEC
   DECIMAL
   DOUBLE
   DOUBLE PRECISION
   ENUM
   FLOAT
   INT
   INTEGER
   LONGBLOB
   LONGTEXT
   MEDIUMBLOB
   MEDIUMINT
   MEDIUMTEXT
   SET DATA TYPE
   SMALLINT
   TEXT
   TIME
   TIMESTAMP
   TINYBLOB
   TINYINT
   TINYTEXT
   VARBINARY
   VARCHAR
   YEAR DATA TYPE

mysql> HELP INT;
Name: 'INT'
Description:
INT[(M)] [UNSIGNED] [ZEROFILL]

A normal-size integer. The signed range is -2147483648 to 2147483647.
The unsigned range is 0 to 4294967295.

URL: https://dev.mysql.com/doc/refman/8.0/en/numeric-type-syntax.html

十九、本章总结

在这里插入图片描述

一、数据库的背景及功能需求 进入21世纪以来,计算机的普及应用和信息技术、网络技术的发展给人们的工作和生活带来了极大的便利和高效,信息化、电子化已经成为节约运营成本,提高工作效率的首选。 相比之下,国内的相当数量的中小型医院的病人资料工作流程还采用相对保守的人工工作方式,数据信息的查询和存储的成本较高,而且效率还很低下。所以需要一种对于医院的病人资料管理系统来高效、低成本、便捷的进行医院病人信息数据的查询和存储。 1. 病人的相关信息应该由医院数据库管理员进行添加、删除、修改、查询等维护操作。 2. 需通过病人所患的疾病来确定病人的治疗 3. 对病人的编号、出生日期、性别、工作、住址信息进行查询。 4. 对病人的治疗进程,缴费情况等进行及时的更新与统一管理。 5. 医院需通过治疗结果来查看病人是否结束治疗,以进行必要的及时续约等行为。 6. 病人可通过数据库进行对所交费用,治疗情况的相关信息进行查看。 7. 病人信息的更新等等由数据库管理员进行维护。 系统功能的基本功能: 1病人信息包含编号,姓名,性别,出生日期,工作单位及地址,住址,工作,保险,医保号,电话,邮箱,死亡日期。 2交费项目信息包含项目序号,项目类型,缴费金额等 3回访记录包含住院号,病人编号,回访日期,回访人,记录,生命体征。 4 可通过对数据库的查询了解病人的相关信息,以及病情,并确定治疗方案。 所有关系模式都属于BC范式 (1)在关系模式patient中patientID是主键,所以在包含属性patientID的函数依赖是一个superkey。 (2)在关系模式中inpatient中number为主键,所以在所包含属性number的函数依赖是一个superkey。 (3)在关系模式outpatient中patientID为主键,所以包含patientID的函数依赖是一个superkey。 (4)在关系模式bed中number是主键,所以包含patientID的函数依赖是一个superkey。 (5)在关系模式department中depname是主键,所以在包含属性depname的函数依赖是一个superkey。 (6)在关系模式中re_call中number为主键,所以在所包含属性number的函数依赖是一个superkey。 (7)在关系模式case中caseID为主键,所以包含caseID的函数依赖是一个superkey。 (8)在关系模式charge1中chargeID是主键,所以包含chargeID的函数依赖是一个superkey。 (9)在关系模式charge2中chargeID是主键,所以在包含属性chargeID的函数依赖是一个superkey。 (10)在关系模式中inotice中inoticeID为主键,所以在所包含属性inoticeID的函数依赖是一个superkey。所以一定属于BCNF。 二、数据库的概念结构设计 病人资料管理系统的E-R模型 三、数据库的物理结构设计 住院病人inpatient 列名 数据类型 字段长度 字段描述 备注 Number Bigint 住院号 主键 patientID bigint 病人编号 非空,外码 Name Bigint 姓名 Inday Datetime 入院时间 Bedroom char 4 床号 Sort varchar 20 入院科别 病人信息表patient 列名 数据类型 长度 字段描述 备注 patientID bigint 病人编号 主键 Name varchar 20 姓名 不能为空 Sex char 4 性别 Birth datetime 出生日期 Dep varchar 40 单位 Depadd varchar 60 单位地址 address varchar 60 住址 Work varchar 10 工作 在职、离休、退休 Insure char 4 医保 insnumb varchar 30 医保号 有、无,不为空 Tel varchar 10 电话 Email varchar 50 E-mail deathday varchar 20 死亡日期 门诊病人表outpatient 列名 数据类型 字段长度 字段描述 备注 patientID Bigint 病人编号 主键 Jz_date Varchar 20 就诊时间 Pay varchar 10 缴费情况 病人就诊花费 Name varchar 20 姓名 非空 Sex char 10 性别 Age Int 年龄 床位bed 列名 数据类型 字段长度 字段描述 备注 Number Bigint 住院号 主键 Name Varchar( 20 姓名 非空 Bedroom bigint 床号 Doctor Varchar 20 主治医生 主管本床位的医生姓名 Result Varchar 20 治疗结果 Department varchar 20 所属病区 病区department 列名 数据类型 字段长度 字段描述 备注 Depname varchar 10 病区名称 主键 Bedamount bigint 病床数 Responsor varchar 20 负责人姓名 非空 Inpeople bigint 入住人数 Wellpor real 好转率 Death real 、 死亡率 回访re_call 列名 数据类型 长度 字段描述 备注 Number bigint 序号 主键,自动产生 patientID bigint 病人编号 不为空 Callday varchar 20 回访时间 Callbody varchar 20 回访人 Record varchar 500 回访记录 Life char 8 生命特征 死亡 门诊病历ccase 列名 数据类型 字段长度 字段描述 备注 caesID Bigint 病历号 主键 Name Varchar 20 姓名 非空 Context Varchar 60 病例内容 Diadate datetime 诊断时间 Doctor Varchar 20 主治医生 联系 支付2pay2 列名 数据类型 字段长度 字段描述 备注 PatientID bigint 病人编号 主键 chargeID Bigint 支付项目号 主键 Amount varchar 20 payDate datetime 支付时间 收费项目2charge2 列名 数据类型 字段长度 字段描述 备注 chargeID bigint 项目序号 主键 Chargesort Varchar 20 项目类型 Amount Varchar 10 收费金额 Name Varchar 20 病人姓名 Operator Varchar 20 收款员 收费项目1charge1 列名 数据类型 字段长度 字段描述 备注 chargeID bigint 项目序号 主键 Name Varchar 20 病人姓名 number bigint 住院号 外码 category Varchar 10 收费类型 chargeday datetime 收费日期 opertor varchar 20 收款人 Amount Varchar 10 应收金额 payamount Char 10 交费金额 入院通知单inotice 列名 数据类型 字段长度 字段描述 备注 inoticeID bigint 通知单号 主键 doctor varchar 20 医师姓名 非空 Name varchar 20 病人姓名 非空 PatientID bigint 病人编号 外码 Diagadvice varchar 40 诊断建议 Pay varchar 20 收费情况 Pass Char 4 是否批准 是 否。非空 关系及各属性 Patient(patientID,name,sex,birth,dep,depadd,address,work,insure,insnumb,tel,email,deathday) Inpatient(number,patientID,name,inday,sort,bedroom) Outpatient(patientID,name ,sex,age,jz_date,pay) Bed(number,name,bedroom,doctor,result,department,empty) Department(depname,bedamount,responsor,inpeople,wellpor,,deathpor) Re_call(number,patientID,callday,life,record,callbody) Case(caseID,name,context,diadate,doctor) Charge2(chargeID,chargesort,amount,name,operator) Charge1(chargeID,category,name ,number,chargeday,payamount,amount) inotice(inoticeID,doctor,name,patientID,diaadvice,pay,pass) 联系 Pay2(patientID,amount,paydate,chargeID) 触发器 1提醒触发器 create trigger reminder on patient after insert,update as raiserror('你在插入或修改病人的数据',16,10); 2.更新操作的触发器 create trigger hehe on outpatient for update as begin update outpatient set pay=pay*0.9 end 存储过程 1病人信息插入的存储过程 create procedure patientInsert ( @patientID bigint, @name varchar(20), @sex char(4), @birth datetime, @dep varchar(40), @depadd varchar(60), @address varchar(60), @work varchar(10), @insure char(4), @insnumb varchar(30), @tel varchar(15), @email varchar(50), @deathday datetime ) as insert into patient( name,sex ,birth ,dep,depadd ,address ,work ,insure,insnumb ,tel ,email ,deathday ) values(@name,@sex ,@birth ,@dep,@depadd ,@address ,@work ,@insure,@insnumb ,@tel ,@email ,@deathday ) 2 住院病人信息插入的存储过程 create procedure inpatientInsert ( @number bigint, @patientID bigint, @name varchar(20), @inday datetime, @sort char(20), @bedroom char(4) ) as insert into inpatient( number,patientID ,name ,inday ,sort ,bedroom ) values( @number ,@patientID ,@name ,@inday ,@sort ,@bedroom ) 索引 1 use hospital_patient create unique nonclustered index inpatient_name on inpatient ( name ) 2 use hospital_patient create nonclustered index charge_amount on charge1 ( amount desc )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

众生云海,一念初见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值