ImageVerifierCode 换一换
格式:DOCX , 页数:20 ,大小:22.79KB ,
资源ID:7368208      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7368208.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(mysql学习笔记.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

mysql学习笔记.docx

1、mysql学习笔记mysql学习内容1 建库2 建表3 对表中记录的查询4 用户的密码管理5 用户的授权与权限的撤消6 数据的备份与恢复7 用binlog搭建AB复制8 读写分离9 mysql群集mysql 开源 跨平台库是以目录的形式存放在服务器本机的物理磁盘上的 数据库名就是目录名rootrhel6 bin# ./mysqld_safe -user=mysql &启动数据库rootrhel6 bin# ./mysqlWelcome to the MySQL monitor. Commands end with ; or g.Your MySQL connection id is 1Serv

2、er version: 5.0.56-log Source distributionType help; or h for help. Type c to clear the buffer.rootrhel6 bin# netstat -utnalp |grep mysqltcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 30581/mysqld mysql 启动数据库mysql数据库管理员 root 密码默认为空 在没有作任何授权的情况下,管理员帐号为root每条结束使用;结束c;结束这条sql语句mysql select database(); 查看当前操作的数据

3、库是哪个+-+| database() |+-+| NULL | +-+ (man,wo1 row in set (0.00 sec)mysql select user(); 查询当前操作的数据库的登录用户+-+| user() |+-+| rootlocalhost | +-+1 row in set (0.00 sec)mysql show databases;+-+| Database |+-+| information_schema | 是虚拟库不占用磁盘空间 数据存放在内存中 存放的是mysql服务的初始信息| mysql | 是服务器的授权信息库 不能删除和损坏| test | 是

4、个公共库+-+3 rows in set (0.00 sec)建立自定义数据库 库名具有唯一性 区分大小写 要有标识性 不要用关键字 不能使用纯数字mysql create database studb; 建立数据库Query OK, 1 row affected (0.00 sec)mysql drop database studb; 删除数据库;如果库中有表一并删除Query OK, 0 rows affected (0.00 sec)mysql use studb; 切换数据库Database changed建表(建表时要使定一个数据库)具有唯一性,区分大小写,要有标识性,不能用关键字m

5、ysql create table stu_tab( - name char(8), - sex char(4), - age int(3) - );Query OK, 0 rows affected (0.04 sec)mysql show tables; 显示当前库中已有的表+-+| Tables_in_studb|+-+| stu_tab | +-+1 row in set (0.02 sec)mysql desc stu_tab; 查看表结构+-+-+-+-+-+-+| Field | Type | Null | Key | Default | Extra |+-+-+-+-+-+-+

6、| name | char(8) | YES | | NULL | | | sex | char(4) | YES | | NULL | | | age | int(3) | YES | | NULL | | +-+-+-+-+-+-+3 rows in set (0.02 sec)mysql数据类型1 数据类型整型 默认int类型有符号 unsigned浮点型(有小数位) 单精度 双精度2 字符型char 定长,不够指定宽度补空格varchar 变长(0-255)3 日期类型日期 date时间 time年 year日期时间 datetime获取当前系统时间mysql insert into

7、shijian values(hehe,now(),now();Query OK, 1 row affected, 2 warnings (0.03 sec)mysql select * from shijian;+-+-+-+| name | rzsh | shri |+-+-+-+| hehe | 2012 | 2012-05-21 | +-+-+-+1 row in set (0.00 sec)mysql insert into shijian values (zz,2012,19001213);Query OK, 1 row affected (0.00 sec)mysql selec

8、t * from shijian;+-+-+-+| name | rzsh | shri |+-+-+-+| hehe | 2012 | 2012-05-21 | | zz | 2012 | 1900-12-13 | +-+-+-+2 rows in set (0.00 sec)年份是以四份显示的,不足四位的以四位补齐,在0069区间内自动以20补齐,在7099之间以19补齐4 复合类型set 多选enum 单选mysql create table t4 ( - name char(8), - sex enum(girl,boy), - loves set(book,film,music) -

9、 );Query OK, 0 rows affected (0.05 sec)插入记录mysql insert into stu_tab values (zz,girl,999);Query OK, 1 row affected (0.01 sec)查看表中的记录 mysql select * from stu_tab;+-+-+-+ | name | sex | age |+-+-+-+ | zz | girl | 999 | +-+-+-+1 row in set (0.00 sec) 删除记录mysql delete from stu_tab;Query OK, 3 rows affec

10、ted (0.00 sec)删除某条记录mysql delete from shijian where name=zz;Query OK, 1 row affected (0.03 sec)mysql select * from shijian;+-+-+-+| name | rzsh | shri |+-+-+-+| hehe | 2012 | 2012-05-21 | +-+-+-+1 row in set (0.00 sec)mysql insert into shijian values(hehe,now(),now();Query OK, 1 row affected, 2 warn

11、ings (0.03 sec)mysql show create table t4; 查看表的建表过程+-+-+| Table | Create Table |+-+-+| t4 | CREATE TABLE t4 ( name char(8) default NULL, sex enum(girl,boy) default NULL, loves set(book,film,music) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1 | +-+-+1 row in set (0.02 sec)mysql insert into t4 v

12、alues (zz,girl,book,film);Query OK, 1 row affected (0.00 sec)约束条件在tea库中建teachertab表有老师的名字 老师的名字最多是4个汉字,要求插入记录时必须写老师的名字 (name)老师的性别 要求只能是男性或女性 也可以保密 (sex)还要有年龄字段 老师的年龄大多数都在30岁左右 (nl)还要有所教的科目 host kernel oracle (km)入职年份 (rznf)生日 (sr)工资(月薪最高在2W左右) (gz)还要有上班的迟到记录字段 (cd)表结构的修改语法格式alter table 表名add 添加新字段d

13、rop 删除字段change 给字段改名modify 修改字段的类型(字段类型改变,值也改变)在age字段下面添加一个字段,存放老师的爱好 film,music,footbalmysql alter table teachertab add hobby set(film,music,football) default football after age;添加一个新字段 num 存放老师的编号 00000001 在所有记录上方mysql alter table teachertab add num varchar(8) first;删除字段 mysql alter table teachert

14、ab drop mail;Query OK, 0 rows affected (0.06 sec)Records: 0 Duplicates: 0 Warnings: 0修改字段名(也可字段类型)mysql alter table teachertab change km subject set (host,kernel,oracle);Query OK, 0 rows affected (0.05 sec)Records: 0 Duplicates: 0 Warnings: 0修改字段类型mysql alter table teachertab modify sr year;Query OK

15、, 0 rows affected (0.04 sec)Records: 0 Duplicates: 0 Warnings: 0改变表的名字mysql alter table teachertab rename teacher;Query OK, 0 rows affected (0.03 sec)表的复制mysql create table t2 select num,name,sex,mail from teacher;Query OK, 7 rows affected (0.04 sec)Records: 7 Duplicates: 0 Warnings: 0复制表时若字段有key属性,

16、那原表的key属性不会被复制key1 主键 primary key 具有唯一标识表中每条记录的值的一个或多个域。主键不允许为Null,并且必须始终具有唯一索引。主键用来将表与其他表中的外键相关联。2 索引 index 3unique 设置unique的字段其数据不能重复4外键 保持数据一致性,完整性,主要目的是通过主表中的数据控制存储在从表中的数据。使两张表形成关联,外键只能引用外表中的列的值或使用空值。索引会增加查询的速度,减慢插入速度,占用磁盘空间创建index字段1 建表时就把字段设置成索引字段mysql create table t5( - name char(8) not null,

17、 - age int(3) not null , - index (name) - );Query OK, 0 rows affected (0.07 sec)mysql desc t5;+-+-+-+-+-+-+| Field | Type | Null | Key | Default | Extra |+-+-+-+-+-+-+| name | char(8) | NO | MUL | NULL | | | age | int(3) | NO | | NULL | | +-+-+-+-+-+-+2 把表中的某个字段设置成索引字段mysql create index name(index名字

18、) on t6(name)(字段名);Query OK, 0 rows affected (0.03 sec)Records: 0 Duplicates: 0 Warnings: 03 把表中的多个字段设置成索引字段(一个表中最多有15个索引字段)mysql create table t7( - name char(8), - sex enum(g,b), - age int, - index(name) , - index(sex) - );Query OK, 0 rows affected (0.07 sec)删除indexmysql drop index name on t5;Query

19、 OK, 0 rows affected (0.07 sec)Records: 0 Duplicates: 0 Warnings: 0unique 数据不允许重复mysql create table t8( stu_num char(8) not null, name char(8) not null, age tinyint(2) unsigned not null, unique(stu_num), index(name) );Query OK, 0 rows affected (0.03 sec)mysql desc t8;+-+-+-+-+-+-+| Field | Type | Nu

20、ll | Key | Default | Extra |+-+-+-+-+-+-+| stu_num | char(8) | NO | PRI | NULL | | | name | char(8) | NO | MUL | NULL | | | age | tinyint(2) unsigned | NO| | NULL | | +-+-+-+-+-+-+更新updatemysql update t8 set stu_num=00000003 where stu_num=;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed:

21、1 Warnings: 0mysql select * from t8;+-+-+-+| stu_num | name | age |+-+-+-+| 00000001 | zz | 1 | | 00000002 | zz | 2 | | 00000003 | zz | 250 | +-+-+-+mysql update t8 set age=18,stu_num=00000004 where name=zz and age=2;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql sele

22、ct * from t8;+-+-+-+| stu_num | name | age |+-+-+-+| 00000001 | zz | 1 | | 00000004 | zz | 18 | | 00000003 | zz | 250 | +-+-+-+主键mysql alter table t8 - add id int(3) primary key auto_increment;Query OK, 3 rows affected (0.04 sec)Records: 3 Duplicates: 0 Warnings: 0mysql desc t8;+-+-+-+-+-+-+| Field

23、| Type | Null | Key | Default | Extra |+-+-+-+-+-+-+| stu_num| char(8) | NO | UNI | NULL | | | name | char(8) | NO | MUL | NULL | | | age | tinyint(2) unsigned | NO | | NULL | | | id | int(3) | NO | PRI | NULL | auto_increment |+-+-+-+-+-+-+复合主键mysql create table server( hostip char(15) not null, se

24、rname char(10), serport char(5), access enum(y,n), primary key(hostip,serport) );Query OK, 0 rows affected (0.04 sec)只要复合主键的字段值不同时出现时就允许插入 表中复合主键的值不能够同时重复外键要给表设置外键字段需要指定当前表的存储引擎为innodb存储引擎:一段用来执行sql语句的代码 是mysql服务器提供 不同存储引擎支持的功能、存储数据方式 位置都不同1 当前的mysql服务器支持哪些引擎mysql show enginesG;2 查看当前的数据库默认使用的存储引擎mysql show variables like table_type;+-+-+| Variable_name | Value |+-+-+| table_type | MyISAM | +-+-+3 建表时指定表使用的存储引擎mysql create table b (n

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1