收藏 分销(赏)

MySQL基础-学习.doc

上传人:w****g 文档编号:2816882 上传时间:2024-06-06 格式:DOC 页数:14 大小:534.54KB 下载积分:8 金币
下载 相关 举报
MySQL基础-学习.doc_第1页
第1页 / 共14页
MySQL基础-学习.doc_第2页
第2页 / 共14页


点击查看更多>>
资源描述
<p>(word完整版)MySQL基础 学习 超级详细 一、MySQL概述 1、什么是数据库 ?  答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2、什么是 MySQL、Oracle、SQLite、Access、MS SQL Server等 ?  答:他们均是一个软件,都有两个主要的功能: o a。 将数据保存到文件或内存 o b. 接收特定的命令,然后对文件进行相应的操作 3、什么是SQL ?  答:MySQL等软件可以接受命令,并做出相应的操作,由于命令中可以包含删除文件、获取文件内容等众多操作,对于编写的命令就是是SQL语句. 二、MySQL安装 &nbsp; &nbsp; &nbsp; MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一. 想要使用MySQL来存储并操作数据,则需要做几件事情:   a. 安装MySQL服务端   b。 安装MySQL客户端   b. 【客户端】连接【服务端】   c。 【客户端】发送命令给【服务端MySQL】服务的接受命令并执行相应操作(增删改查等) &nbsp; &nbsp; &nbsp;下载 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; http://dev。mysql。com/downloads/mysql/ &nbsp; &nbsp; &nbsp; &nbsp;安装 &nbsp; &nbsp; &nbsp; &nbsp; windows: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://jingyan。baidu。com/article/f3ad7d0ffc061a09c3345bf0.html &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;linux: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yum install mysql—server &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mac: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 一直点下一步 &nbsp; 客户端连接 连接: &nbsp; &nbsp; &nbsp;1、mysql管理人默认为root,没有设置密码则直接登录 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysql -h host -u root &nbsp;-p 不用输入密码按回车自动进入 &nbsp; &nbsp; &nbsp;2、如果想设置mysql密码 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysqladmin -u root password 123456 &nbsp; &nbsp; &nbsp;3、如果你的root现在有密码了(123456),那么修改密码为abcdef的命令是: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mysqladmin -u root —p password abcdef 退出: &nbsp; &nbsp; &nbsp; QUIT 或者 Control+D 三、数据库基础 &nbsp;分为两大部分: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1、数据库和表的创建; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2、数据库和表内容的操作 &nbsp;数据库操作-思路图 &nbsp; 1、数据库和表的创建 &nbsp; &nbsp; &nbsp;(一)数据库的创建 &nbsp;&nbsp;1。1、显示数据库 1 SHOW DATABASES; &nbsp; &nbsp; 默认数据库:   &nbsp; &nbsp;    &nbsp;mysql — 用户权限相关数据       test - 用于用户测试数据       information_schema — MySQL本身架构相关数据 &nbsp;&nbsp;1。2、创建数据库 &nbsp;# utf-8 &nbsp;CREATE DATABASE 数据库名称 DEFAULT CHARSET utf8 COLLATE utf8_general_ci; &nbsp;# gbk &nbsp;CREATE DATABASE 数据库名称 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; &nbsp; 1.3、打开数据库 &nbsp;USE db_name; &nbsp;注:每次使用数据库必须打开相应数据库 显示当前使用的数据库中所有表:SHOW TABLES; &nbsp;1。4、用户管理 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 用户设置: 创建用户 &nbsp; &nbsp;create user &#39;用户名&#39;@’IP地址&#39; identified by &#39;密码&#39;; 删除用户 &nbsp; &nbsp;drop user &#39;用户名&#39;@’IP地址&#39;; 修改用户 &nbsp; &nbsp;rename user ’用户名&#39;@&#39;IP地址&#39;; to &#39;新用户名’@’IP地址’;; 修改密码 &nbsp; &nbsp;set password for &#39;用户名’@&#39;IP地址&#39; = Password(’新密码’) &nbsp; PS:用户权限相关数据保存在mysql数据库的user表中,所以也可以直接对其进行操作(不建议) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 用户权限设置: show grants for ’用户’@&#39;IP地址&#39; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;—— 查看权限 grant &nbsp;权限 on 数据库。表 to &nbsp; &#39;用户&#39;@’IP地址&#39; &nbsp; &nbsp; &nbsp;-— 授权 revoke 权限 on 数据库。表 from ’用户&#39;@&#39;IP地址&#39; &nbsp; &nbsp; &nbsp;—- 取消权限 &nbsp;all privileges &nbsp;除grant外的所有权限 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;仅查权限 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select,insert &nbsp; 查和插入权限 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.。. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;usage &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 无访问权限 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alter &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 使用alter table &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alter routine &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 使用alter procedure和drop procedure &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;create &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;使用create table &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;create routine &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;使用create procedure &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;create temporary tables 使用create temporary tables &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;create user &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 使用create user、drop user、rename user和revoke &nbsp;all privileges &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;create view &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 使用create view &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;delete &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;使用delete &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;drop &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;使用drop table &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;execute &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 使用call和存储过程 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;file &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;使用select into outfile 和 load data infile &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;grant option &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;使用grant 和 revoke &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;index &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 使用index &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;insert &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;使用insert &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lock tables &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 使用lock table &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;process &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 使用show full processlist &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;使用select &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;show databases &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;使用show databases &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;show view &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 使用show view &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;update &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;使用update &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;reload &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;使用flush &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;shutdown &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;使用mysqladmin shutdown(关闭MySQL) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;super &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;#1113154;&amp;#1113096;使用change master、kill、logs、purge、master和set global。还允许mysqladmin&amp;#1113431;&amp;#1113432;&amp;#1113226;&amp;#1113227;调试登陆 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;replication client &nbsp; &nbsp; &nbsp;服务器位置的访问 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;replication slave &nbsp; &nbsp; &nbsp; 由复制从属使用 对于权限设置 对于目标数据库以及内部其他: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;数据库名.* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 数据库中的所有 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;数据库名。表 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;指定数据库中的某张表 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;数据库名.存储过程 &nbsp; &nbsp; 指定数据库中的存储过程 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*.* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;所有数据库 对于数据库名的解释 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;用户名@IP地址 &nbsp; &nbsp; &nbsp; &nbsp; 用户只能在改IP下才能访问 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;用户名@192.168。1。% &nbsp; 用户只能在改IP段下才能访问(通配符%表示任意) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;用户名@% &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 用户可以再任意IP下访问(默认IP地址为%) 对于ip地址的访问 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grant all privileges on db1.tb1 TO ’用户名’@’IP&#39; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;grant select on db1。* TO &#39;用户名&#39;@&#39;IP’ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;grant select,insert on *。* TO ’用户名’@’IP’ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;revoke select on db1。tb1 from ’用户名&#39;@&#39;IP’ 实际例子 1。4、备份库和恢复库 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;备份库: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MySQL备份和还原,都是利用mysqldump、mysql和source命令来完成。 &nbsp; &nbsp; &nbsp; 1.在Windows下MySQL的备份与还原 备份 1、开始菜单 | 运行 | cmd |利用“cd /Program Files/MySQL/MySQL Server 5。0/bin”命令进入bin文件夹 2、利用“mysqldump &nbsp;-u 用户名 -p databasename &gt;exportfilename”导出数据库到文件,如mysqldump —u root —p voice〉voice。sql,然后输入密码即可开始导出。 &nbsp; 还原 1、进入MySQL Command Line Client,输入密码,进入到“mysql&gt;”。 2、输入命令”show databases;”,回车,看看有些什么数据库;建立你要还原的数据库,输入”create database voice;&quot;,回车. 3、切换到刚建立的数据库,输入&quot;use voice;”,回车;导入数据,输入&quot;source voice.sql;&quot;,回车,开始导入,再次出现&quot;mysql&gt;&quot;并且没有提示错误即还原成功。 &nbsp; &nbsp; &nbsp;2、在linux下MySQL的备份与还原 2.1 备份(利用命令mysqldump进行备份) &nbsp; [root@localhost mysql]# mysqldump —u root —p voice〉voice.sql,输入密码即可。 2.2 还原 方法一: &nbsp; [root@localhost ~]# mysql —u root —p 回车,输入密码,进入MySQL的控制台”mysql〉”,同1.2还原。 方法二: &nbsp; [root@localhost mysql]# mysql —u root -p voice</p><voice.sql,输入密码即可。 mysqldump="" -uroot="" -p123456="" a="" sql="" -d="" uroot="">F:\all_struct.sql 3。备份全部数据库的数据(加 —t 参数) mysqldump —uroot —p123456 -A -t&gt;F:\all_data。sql 4。备份单个数据库的数据和结构(,数据库名mydb) mysqldump —uroot -p123456 mydb〉F:\mydb。sql 5。备份单个数据库的结构 mysqldump -uroot —p123456 mydb -d〉F:\mydb。sql 6。备份单个数据库的数据 mysqldump —uroot —p123456 mydb -t〉F:\mydb。sql 7。备份多个表的数据和结构(数据,结构的单独备份方法与上同) mysqldump -uroot —p123456 mydb t1 t2 〉f:\multables.sql 8.一次备份多个数据库 mysqldump —uroot -p123456 --databases db1 db2 &gt;f:\muldbs.sql 还原: 还原部分分(1)mysql命令行source方法 和 (2)系统命令行方法 1.还原全部数据库: (1) mysql命令行:mysql&gt;source f:\all。sql (2) 系统命令行: mysql -uroot —p123456<f:\all。sql mysql="">use mydb mysql〉source f:\mydb.sql (2) mysql -uroot —p123456 mydb 〈f:\mydb.sql 3.还原单个数据库的多个表(需指定数据库) (1) mysql〉use mydb mysql〉source f:\multables。sql (2) mysql —uroot —p123456 mydb 〈f:\multables。sql 4。还原多个数据库,(一个备份文件里有多个数据库的备份,此时不需要指定数据库) (1) mysql命令行:mysql&gt;source f:\muldbs。sql (2) 系统命令行: mysql -uroot —p123456 &lt;f:\muldbs.sql 更多备份 更多备份知识: http://www。jb51。net/article/41570.htm (二)数据表的创建 &nbsp;1.1、显示数据表 show tables; &nbsp;1。2、创建数据表 create table 表名( &nbsp; &nbsp;列名 &nbsp;类型 &nbsp;是否可以为空, &nbsp; &nbsp;列名 &nbsp;类型 &nbsp;是否可以为空 )ENGINE=InnoDB DEFAULT CHARSET=utf8 &nbsp; 是否可空,null表示空,非字符串 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;not null &nbsp; &nbsp;— 不可空 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;null &nbsp; &nbsp; &nbsp; &nbsp;— 可空 设置是否为空 &nbsp; 默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;create table tb1( &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nid int not null defalut 2, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num int not null &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;) 设置默认值 &nbsp; &nbsp; &nbsp; 自增,如果为某列设置自增列,插入数据时无需设置此列,默认将自增(表中只能有一个自增列) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;create table tb1( &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nid int not null auto_increment primary key, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num int null &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;或 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;create table tb1( &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nid int not null auto_increment, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num int null, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;index(nid) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;注意:1、对于自增列,必须是索引(含主键)。 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2、对于自增可以设置步长和起始值 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; show session variables like ’auto_inc%’; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set session auto_increment_increment=2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set session auto_increment_offset=10; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shwo global &nbsp;variables like &#39;auto_inc%’; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set global auto_increment_increment=2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set global auto_increment_offset=10; 设置自增 &nbsp; &nbsp; &nbsp; 主键,一种特殊的唯一索引,不允许有空值,如果主键使用单个列,则它的值必须唯一,如果是多列,则其组合必须唯一。 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;create table tb1( &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nid int not null auto_increment primary key, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num int null &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;或 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;create table tb1( &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nid int not null, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num int not null, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;primary key(nid,num) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;) 设置主键 外键,一个特殊的索引,只能是指定内容 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;creat table color( &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nid int not null primary key, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;name char(16) not null &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;create table fruit( &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nid int not null primary key, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;smt char(32) null , &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;color_id int not null, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;constraint fk_cc foreign key (color_id) references color(nid) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;) 设置外键 主键与外键关系(非常重要) http://www。cnblogs。com/programmer—tlh/p/5782451。html &nbsp;1。3删除表 drop table 表名 1.4、清空表 delete from 表名 truncate table 表名 1。5、基本数据类型 MySQL的数据类型大致分为:数值、时间和字符串 &nbsp; bit[(M)] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;二进制位(101001),m表示二进制位的长度(1-64),默认m=1 &nbsp; &nbsp; &nbsp; &nbsp;tinyint[(m)] [unsigned] [zerofill] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;小整数,数据类型用于保存一些范围的整数数值范围: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;有符号: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;—128 ~ 127. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;无符号: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 ~ 255 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;特别的: MySQL中无布尔值,使用tinyint(1)构造。 &nbsp; &nbsp; &nbsp; &nbsp;int[(m)][unsigned][zerofill] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;整数,数据类型用于保存一些范围的整数数值范围: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;有符号: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-2147483648 ~ 2147483647 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;无符号: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 ~ 4294967295 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;特别的:整数类型中的m仅用于显示,对存储范围无限制。例如: int(5),当插入数据2时,select 时数据显示为: 00002 &nbsp; &nbsp; &nbsp; &nbsp;bigint[(m)][unsigned][zerofill] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;大整数,数据类型用于保存一些范围的整数数值范围: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;有符号: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;—9223372036854775808 ~ 9223372036854775807 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;无符号: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp;~ &nbsp;18446744073709551615 &nbsp; &nbsp; &nbsp; &nbsp;decimal[(m[,d])] [unsigned] [zerofill] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;准确的小数值,m是数字总个数(负号不算),d是小数点后个数。 m最大值为65,d最大值为30. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;特别的:对于精确数值计算时需要用此类型 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decaimal能够存储精确值的原因在于其内部按照字符串存储。 &nbsp; &nbsp; &nbsp; &nbsp;FLOAT[(M,D)] [UNSIGNED] [ZEROFILL] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;单精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;无符号: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;—3。402823466E+38 to —1.175494351E—38, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1。175494351E-38 to 3.402823466E+38 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;有符号: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.175494351E-38 to 3.402823466E+38 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;**** 数值越大,越不准确 **** &nbsp; &nbsp; &nbsp; &nbsp;DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;双精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;无符号: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;—1。7976931348623157E+308 to —2.2250738585072014E-308 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2.2250738585072014E—308 to 1.7976931348623157E+308 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;有符号: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2.2250738585072014E-308 to 1.7976931348623157E+308 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;**** 数值越大,越不准确 **** &nbsp; &nbsp; &nbsp; &nbsp;char (m) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;char数据类型用于表示固定长度的字符串,可以包含最多达255个字符.其中m代表字符串的长度。 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PS: 即使数据小于m长度,也会占用m长度 &nbsp; &nbsp; &nbsp; &nbsp;varchar(m) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;varchars数据类型用于变长的字符串,可以包含最多达255个字符。其中m代表该数据类型所允许保存的字符串的最大长度,只要长度小于该最大值的字符串都可以被保存在该数据类型中。 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;注:虽然varchar使用起来较为灵活,但是从整个系统的性能角度来说,char数据类型的处理速度更快,有时甚至可以超出varchar处理速度的50%.因此,用户在设计数据库时应当综合考虑各方面的因素,以求达到最佳的平衡 &nbsp; &nbsp; &nbsp; &nbsp;text &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;text数据类型用于保存变长的大字符串,可以组多到65535 (2**16 − 1)个字符。 &nbsp; &nbsp; &nbsp; &nbsp;mediumtext &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;A TEXT column with a maximum length of 16,777,215 (2**24 − 1) characters. &nbsp; &nbsp; &nbsp; &nbsp;longtext &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 − 1) characters。 &nbsp; &nbsp; &nbsp; &nbsp;enum &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;枚举类型, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;An ENUM column can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;示例: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CREATE TABLE shirts ( &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;name VARCHAR(40), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;size ENUM(&#39;x-small&#39;, ’small&#39;, &#39;medium&#39;, &#39;large&#39;, ’x-large’) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;INSERT INTO shirts (name, size) VALUES (’dress shirt’,’large’), (’t—shirt&#39;,’medium&#39;),(&#39;polo shirt’,&#39;small&#39;); &nbsp; &nbsp; &nbsp; &nbsp;set &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;集合类型 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;A SET column can have a maximum of 64 distinct members. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;示例: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CREATE TABLE myset (col SET(’a’, &#39;b’, &#39;c’, &#39;d’)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;INSERT INTO myset (col) VALUES (’a,d&#39;), (&#39;d,a’), (’a,d,a&#39;), (’a,d,d’), (’d,a,d&#39;); &nbsp; &nbsp; &nbsp; &nbsp;DATE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;YYYY-MM-DD(1000-01-01/9999-12-31) &nbsp; &nbsp; &nbsp; &nbsp;TIME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HH:MM:SS(&#39;—838:59:59&#39;/&#39;838:59:59&#39;) &nbsp; &nbsp; &nbsp; &nbsp;YEAR &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;YYYY(1901/2155) &nbsp; &nbsp; &nbsp; &nbsp;DATETIME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;YYYY—MM—DD HH:MM:SS(1000-01-01 00:00:00/9999—12—31 23:59:59 &nbsp; &nbsp;Y) &nbsp; &nbsp; &nbsp; &nbsp;TIMESTAMP &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;YYYYMMDD HHMMSS(1970—01-01 00:00:00/2037 年某时) 1。6、修改表(alter)&nbsp; &nbsp; 添加列:alter table 表名 add 列名 类型 删除列:alter table 表名 drop column 列名 修改列: &nbsp; &nbsp; &nbsp; &nbsp;alter table 表名 modify column 列名 类型; &nbsp;-— 类型 &nbsp; &nbsp; &nbsp; &nbsp;alter table 表名 change 原列名 新列名 类型; —- 列名,类型 &nbsp; 添加主键: &nbsp; &nbsp; &nbsp; &nbsp;alter table 表名 add primary key(列名); 删除主键: &nbsp; &nbsp; &nbsp; &nbsp;alter table 表名 drop primary key; &nbsp; &nbsp; &nbsp; &nbsp;alter table 表名 &nbsp;modify &nbsp;列名 int, drop primary key; &nbsp; 添加外键:alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段); 删除外键:alter table 表名 drop foreign key 外键名称 &nbsp; 修改默认值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000; 删除默认值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT; 修改表 更多参考: · http://www。runoob。com/mysql/mysql—data-types。html 1.7、数据表关系 关联映射:一对多/多对一 &nbsp; 存在最普遍的映射关系,简单来讲就如球员与球队的关系; &nbsp; 一对多:从球队角度来说一个球队拥有多个球员 即为一对多 &nbsp; 多对一:从球员角度来说多个球员属于一个球队 即为多对一 数据表间一对多关系如下图: 关联映射:一对一 &nbsp; 一对一关系就如球队与球队所在地址之间的关系,一支球队仅有一个地址,而一个地址区也仅有一支球队。 &nbsp; 数据表间一对一关系的表现有两种,一种是外键关联,一种是主键关联。图示如下: &nbsp; 一对一外键关联: 一对一主键关联:要求两个表的主键必须完全一致,通过两个表的主键建立关联关系 关联映射:多对多 &nbsp; 多对多关系也很常见,例如学生与选修课之间的关系,一个学生可以选择多门选修课,而每个选修课又可以被多名学生选择。 &nbsp; 数据库中的多对多关联关系一般需采用中间表的方式处理,将多对多转化为两个一对多 1。8、数据表之间的约束 约束是一种限制,它通过对表的行或列的数据做出限制,来确保表的数据的完整性、唯一性. MYSQL中,常用的几种约束: =================================================== 主键(PRIMARY KEY)是用于约束表中的一行,作为这一行的标识符,在一张表中通过主键就能准确定位到一行,因此主键十分重要.主键要求这一行的数据不能有重复且不能为空。 &nbsp;还有一种特殊的主键——复合主键。主键不仅可以是表中的一列,也可以由表中的两列或多列来共同标识 =================================================== 默认值约束(DEFAULT)规定,当有DEFAULT约束的列,插入数据为空时该怎么办。 DEFAULT约束只会在使用INSERT语句(上一实验介绍过)时体现出来,INSERT语句中,如果被DEFAULT约束的位置没有值,那么这个位置将会被DEFAULT的值填充 =================================================== 唯一约束(UNIQUE)比较简单,它规定一张表中指定的一列的值必须不能有重复值,即这一列每个值都是唯一的。 当INSERT语句新插入的数据和已有数据重复的时候,如果有UNIQUE约束,则INSERT失败。 =================================================== 外键(FOREIGN KEY)既能确保数据完整性,也能表现表之间的关系. 一个表可以有多个外键,每个外键必须REFERENCES(参考)另一个表的主键,被外键约束的列,取值必须在它参考的列中有对应值。 在INSERT时,如果被外键约束的值没有在参考列中有对应,比如以下命令,参考列(department表的dpt_name)中没有dpt3,则INSERT失败 =================================================== 非空约束(NOT NULL),听名字就能理解,被非空约束的列,在插入值时必须非空。 在MySQL中违反非空约束,不会报错,只会有警告。 CREATE DATABASE mysql_shiyan; &nbsp; use mysql_text; &nbsp; CREATE TABLE department ( &nbsp; dpt_name &nbsp; CHAR(20) NOT NULL, &nbsp; people_num INT(10) DEFAULT &#39;10’, &nbsp; CONSTRAINT dpt_pk PRIMARY KEY (dpt_name) &nbsp;); CREATE TABLE employee ( &nbsp; id &nbsp; &nbsp; &nbsp;INT(10) PRIMARY KEY, &nbsp; name &nbsp; &nbsp;CHAR(20), &nbsp; age &nbsp; &nbsp; INT(10), &nbsp; salary &nbsp;INT(10) NOT NULL, &nbsp; phone &nbsp; INT(12) NOT NULL, &nbsp; in_dpt &nbsp;CHAR(20) NOT NULL, &nbsp;UNIQUE &nbsp;(phone), &nbsp; CONSTRAINT emp_fk FOREIGN KEY (in_dpt) REFERENCES department(dpt_name) &nbsp;); &nbsp; CREATE TABLE project ( &nbsp; proj_num &nbsp; INT(10) NOT NULL, &nbsp; proj_name &nbsp;CHAR(20) NOT NULL, &nbsp; start_date DATE NOT NUL</f:\all。sql></voice.sql,输入密码即可。>
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服