mysql初学者教程和练习.doc_第1页
mysql初学者教程和练习.doc_第2页
mysql初学者教程和练习.doc_第3页
mysql初学者教程和练习.doc_第4页
mysql初学者教程和练习.doc_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

-lamp134(数据库名)- MySQL数据库 小型关系型数据库 开源数据库- SQL命令 不仅仅适用于MySQL数据库,适用于所有的支持SQL命令的数据库- 进入mysql的方式 直接mysql回车,以匿名的方式进入到数据库,只能看到允许游客看到的数据库- mysql -h(mysql主机) localhost(本机) -u(用户) root(超级管理员) -p(密码) 回车C:UsersAdministratormysql -h localhost -u root -pEnter password:- 输入密码- show databases 显示当前服务器下所有的数据库mysql show databases;-显示:+-+| Database |+-+| information_schema | mysql | performance_schema | sys | ultrax |+-+5 rows in set (0.00 sec)mysql- 上下键可以调用历史记录命令- 每一行命令要以分号结束- create(创建) database(数据库) lamp134(数据库名)mysql create database lamp134;-显示:Query OK, 1 row affected (0.00 sec)mysql- 删除数据库 drop database 数据库名mysql drop database lamp134;-显示:Query OK, 0 rows affected (0.08 sec)mysql - 设置字符集的目的就是为了防止数据库存储中文时乱码- 创建数据库 如果lamp134不存在,同时设置默认的字符集为 utf8(没有-)mysql create database if not exists lamp134 default charset=utf8;-显示:Query OK, 1 row affected (0.00 sec)mysql- 删除数据库 如果存在lamp134mysql drop database if exists lamp134;-显示:Query OK, 0 rows affected (0.00 sec)mysql - 选择数据库lmap134mysql use lamp134-显示:Database changedmysql- 创建表mysql create table stu(- id 字段名 int 字段属性 not null 不为空 auto_increment 自增 primary key 主键(唯一、不能为空) - id int not null auto_increment primary key, - name varchar(20) unique,- varchar(长度) 字符串类型 - sex tinyint default 3,- tinyint 较小的整型 unique 唯一 default 3 设置默认值为3 (该位置没有写入数据时默认添加3) - age tinyint - )engine = myisam default charset=utf8;- engine 数据表的引擎 default charset=utf8 设置默认字符集-显示:Query OK, 0 rows affected (0.14 sec)mysql- show create table 数据表名 显示创建表的语句 G 把数据立起来显示mysql show create table stuG-显示:* 1. row * Table: stuCreate Table: CREATE TABLE stu ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(20) DEFAULT NULL, sex tinyint(4) DEFAULT 3, age tinyint(4) DEFAULT NULL, PRIMARY KEY (id), UNIQUE KEY name (name) ENGINE=MyISAM DEFAULT CHARSET=utf81 row in set (0.00 sec)mysql- 查看表结构mysql desc stu;-显示:+-+-+-+-+-+-+| Field | Type | Null | Key | Default | Extra |+-+-+-+-+-+-+| id | int(11) | NO | PRI | NULL | auto_increment | name | varchar(20) | YES | UNI | NULL | | sex | tinyint(4) | YES | | 3 | | age | tinyint(4) | YES | | NULL | |+-+-+-+-+-+-+4 rows in set (0.11 sec)mysql- 对于id是主键、自增,不要管他!- 增 insert 插入- 插入一条数据 insert into 表名(字段名1,字段名2 ) value(字段1的值,字段2的值 );mysql insert into stu(name,sex,age) value(李大锤,2,18);-显示:Query OK, 1 row affected (0.02 sec)mysql- 查询stu表的所有的数据 select 查mysql select * from stu;-显示:+-+-+-+-+| id | name | sex | age |+-+-+-+-+| 1 | 李大锤 | 2 | 18 |+-+-+-+-+1 row in set (0.06 sec)mysql- 直插入了name,age两个字段,sex字段使用默认值3mysql insert into stu(name,age) value(泰国,24);-显示:Query OK, 1 row affected (0.00 sec)mysql - 改(执行update一定要加条件,否则修改的就是所有的数据)- update 表名 set 字段名1=值1,字段名2=值2 where id=3mysql update stu set name=凯哥,sex=2 where id = 3;-显示:Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql -删 (执行删除时一定要加条件,否则会删除所有的数据)mysql delete from stu where id = 3;- exit 退出数据库- c 取消当前行命令- 导出数据库(退出数据库)C:UsersAdministratormysqldump -u root -p lamp134(数据库的名)lamp134.sql- 该位置没有分号Enter password:- 密码C:UsersAdministrator- 导入数据库(确保lamp134是存在的)C:UsersAdministratormysql -u root -p lamp134- 显示当前数据库下所有的数据表mysql show tables;-显示:+-+| Tables_in_lamp134 |+-+| stu |+-+1 row in set (0.00 sec)mysql- 如果stu 表存在,执行删除mysql drop table if exists stu;-显示:Query OK, 0 rows affected (0.01 sec)mysql- 更新 设置sex=2 条件 id=1mysql update stu set sex=2 where id =1;Query OK, 1 row affected (0.05 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql- 删除id=1的数据mysql delete from stu where id = 1;Query OK, 1 row affected (0.00 sec)mysql- 导出数据库 mysqldump -u root -p 数据库名数据库文件的保存路径和名(退出mysql数据库)C:UsersAdministratormysqldump -u root -p lamp134lamp134.sqlEnter password:- 输入密码C:UsersAdministrator- 导入 mysql -u root -p 数据库名 create table demo( - id int not null auto_increment primary key, - name varchar(25) not null, - age tinyint - );Query OK, 0 rows affected (0.10 sec)- 在创建表的时候没有选择默认的引擎,使用系统默认的引擎- 默认字符集:- 数据库遵循数据库系统默认的字符集-数据表遵循当前数据的默认的字符集- 字段遵循当前表的默认字符集mysql show create table demGmysql show create table demoG* 1. row * Table: demoCreate Table: CREATE TABLE demo ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(25) NOT NULL, age tinyint(4) DEFAULT NULL, PRIMARY KEY (id) ENGINE=InnoDB DEFAULT CHARSET=utf81 row in set (0.00 sec)- 对于 tinyint 类型 有符号最大值为127 无符号最大值 255- 修改表结构 alter table 表名 modify(修改) 字段名 新的属性和约束条件 mysql alter table demo modify age tinyint unsigned;- unsigned 表示无符号Query OK, 4 rows affected (0.12 sec)Records: 4 Duplicates: 0 Warnings: 0mysql- 向demo表中添加新字段 alter table 表名 add(添加) 属性名 类型和约束mysql alter table demo add price int(5) not null;Query OK, 0 rows affected (0.11 sec)Records: 0 Duplicates: 0 Warnings: 0mysql desc demo;+-+-+-+-+-+-+| Field | Type | Null | Key | Default | Extra |+-+-+-+-+-+-+| id | int(11) | NO | PRI | NULL | auto_increment | name | varchar(25) | NO | | NULL | | age | tinyint(3) unsigned | YES | | NULL | | price | int(5) | NO | | NULL | |+-+-+-+-+-+-+4 rows in set (0.02 sec)mysql- 修改表结构 zerofill 自动补零mysql alter table demo modify price int(5) zerofill;- 当数字不够5位时,自动补充前导零Query OK, 11 rows affected (0.06 sec)Records: 11 Duplicates: 0 Warnings: 0mysql - 设置price 的属性为 float(5,2) 2 表示保留两位小数 5 表示包含小数点一共5位mysql alter table demo modify price float(5,2);Query OK, 11 rows affected, 3 warnings (0.08 sec)Records: 11 Duplicates: 0 Warnings: 3mysql desc demo;+-+-+-+-+-+-+| Field | Type | Null | Key | Default | Extra |+-+-+-+-+-+-+| id | int(11) | NO | PRI | NULL | auto_increment | name | varchar(25) | NO | | NULL | | age | tinyint(3) unsigned | YES | | NULL | | price | float(5,2) | YES | | NULL | |+-+-+-+-+-+-+4 rows in set (0.02 sec)mysql- char() 指定存储时采用固定的长度 - 存密码 md5() 加密 32位- varchar()存储时采用可变长度- char(20)存储时,占用的都是20 - varchar(20)实际存储时,有几个占几个- 执行效率 char() 执行效率要高于varchar()- enum() 枚举类型,给的类型中选择一个,如果插入的语句没有在指定的范围内,什么都不写入mysql alter table demo add sex enum(nan,nv,yao);- 如果是在排序时,排序的规则遵守enum()单元的顺序Query OK, 0 rows affected (0.07 sec)Records: 0 Duplicates: 0 Warnings: 0mysql- 删除年龄为空 null 的数据 mysql delete from demo where age is null;Query OK, 8 rows affected (0.00 sec)mysql- 查询所有的数据,按照age进行排序,升序mysql select * from demo order by age;+-+-+-+-+-+| id | name | age | price | sex |+-+-+-+-+-+| 12 | xiaoze | 24 | 99.99 | NULL | 13 | xiaozeze | 24 | 999.99 | NULL | 1 | admin | 120 | 0.00 | NULL | 2 | admin | 126 | 0.00 | NULL | 3 | admin | 127 | 0.00 | NULL | 4 | admin | 127 | 0.00 | NULL | 5 | admin | 140 | 0.00 | NULL | 6 | admin | 255 | 0.00 | NULL | 7 | admin | 255 | 0.00 | NULL |+-+-+-+-+-+9 rows in set (0.00 sec)- desc 降序 asc (默认效果)升序mysql select * from demo order by age desc;+-+-+-+-+-+| id | name | age | price | sex |+-+-+-+-+-+| 6 | admin | 255 | 0.00 | NULL | 7 | admin | 255 | 0.00 | NULL | 5 | admin | 140 | 0.00 | NULL | 3 | admin | 127 | 0.00 | NULL | 4 | admin | 127 | 0.00 | NULL | 2 | admin | 126 | 0.00 | NULL | 1 | admin | 120 | 0.00 | NULL | 12 | xiaoze | 24 | 99.99 | NULL | 13 | xiaozeze | 24 | 999.99 | NULL |+-+-+-+-+-+9 rows in set (0.00 sec)mysql- 修改表结构mysql alter table demo modify sex enum(yao,nv,nan);- 排序Query OK, 9 rows affected (0.07 sec)Records: 9 Duplicates: 0 Warnings: 0mysql select * from demo order by sex;+-+-+-+-+-+| id | name | age | price | sex |+-+-+-+-+-+| 1 | admin | 120 | 0.00 | yao | 2 | admin | 126 | 0.00 | yao | 3 | admin | 127 | 0.00 | yao | 4 | admin | 127 | 0.00 | yao | 5 | admin | 140 | 0.00 | yao | 6 | admin | 255 | 0.00 | nv | 7 | admin | 255 | 0.00 | nan | 12 | xiaoze | 24 | 99.99 | nan | 13 | xiaozeze | 24 | 999.99 | nan |+-+-+-+-+-+9 rows in set (0.00 sec)mysql- text 存储大段的文字(评论的内容,商品的描述)- 修改表结构 alter table 表名 操作- 添加字段 (默认添加的字段为最后一个位置)mysql alter table demo add qq int;Query OK, 0 rows affected (0.06 sec)Records: 0 Duplicates: 0 Warnings: 0mysql - 添加email字段到第一个位置mysql alter table demo add email varchar(30) first;Query OK, 0 rows affected (0.06 sec)Records: 0 Duplicates: 0 Warnings: 0mysql - 添加aa字段 在email的后面mysql alter table demo add aa int after email;Query OK, 0 rows affected (0.06 sec)Records: 0 Duplicates: 0 Warnings: 0mysql- delete 清空 drop 删除- - 删除字段aa drop 字段名mysql alter table demo drop aa;Query OK, 0 rows affected (0.06 sec)Records: 0 Duplicates: 0 Warnings: 0mysql desc demo;- 修改字段属性 - 改不改名mysql alter table demo modify sex tinyint default 3;Query OK, 9 rows affected (0.07 sec)Records: 9 Duplicates: 0 Warnings: 0mysql- change 还可以修改字段的名 change 旧名 新名 属性、约束mysql alter table demo change sex xingbie tinyint default 3;Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0mysql- 使用change修改字段属性时,给两个相同的名mysql alter table demo change qq qq tinyint;Query OK, 9 rows affected (0.07 sec)Records: 9 Duplicates: 0 Warnings: 0- 显示创建数据库的语句mysql show create database demoaG* 1. row * Database: demoaCreate Database: CREATE DATABASE demoa /*!40100 DEFAULT CHARACTER SET latin1 */1 row in set (0.00 sec)mysql- 修改数据库的默认字符集mysql alter database demoa default charset=utf8;Query OK, 1 row affected (0.00 sec)mysql - 修改表的默认字符集alter table demoa(没有这个表) default charset=utf8;- 查看当前数据库下所有的表mysql show tables;+-+| Tables_in_lamp134 |+-+| demo | stu |+-+2 rows in set (0.00 sec)mysql- 添加一条数据 id 自增、主键, 对应位置写 nullmysql insert into stu(id,name,sex,age) value(null,zhangsan,1,20);Query OK, 1 row affected (0.00 sec)mysql select * from stu;- 当不写表的字段名时,采取插入数据的方式和表的结构一一对应的mysql insert into stu value(null,admin,2,30);Query OK, 1 row affected (0.00 sec)mysql- 该位置为1条命令4行显示 插入多条记录mysql insert into stu(id,name,age,sex) value(null,admin1,2,30), - (null,admin2,2,13), - (null,admin3,3,12), - (null,admin4,1,18);Query OK, 4 rows affected (0.00 sec)Records: 4 Duplicates: 0 Warnings: 0mysql- 用户的权限- 数据库只允许在本机使用root权限登陆- mysql -h localhost(主机地址) -u root(用户) -p (密码) - 创建数据库用户 在什么地方(允许用户在那个ip地址上登录) 用什么用户名、什么密码登陆- 授权时,你的权限一定要高- 查询当前登陆数据库的用户mysql select user();+-+| user() |+-+| rootlocalhost |+-+1 row in set (0.00 sec)mysql- 授权 grant all(所有的权限) on 数据库.数据表 to 用户名IP地址 identified by 密码;- % 代表了任意一个地址mysql grant all on *.* to huxiaoshuai% identified by 123;Query OK, 0 rows affected (0.00 sec)mysql- 刷新权限mysql flush privileges;Query OK, 0 rows affected (0.00 sec)mysql- 修改mysql数据库中user表中相关用户的IP地址- 查询指定字段的数据mysql select host,user,password from user;+-+-+-+| host | user | password |+-+-+-+| localhost | root | | | root | | :1 | root | | localhost | | | % | huxiaoshuai | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |+-+-+-+5 rows in set (0.00 sec)mysql- 更新设置新添加的用户的host主机地址为本机mysql update user set host=localhost where user = huxiaoshuai;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql- 移除创建的权限mysql revoke create on *.* from huxiaoshuailocalhost;Query OK, 0 rows affected (0.00 sec)- 刷新权限mysql flush privileges;Query OK, 0 rows affected (0.00 sec)mysql- 删除mysql的权限用户mysql delete from user where user = huxiaoshuai;Query OK, 1 row affected (0.00 sec)mysql- 如何允许其他用户访问数据apache的配置文件http.conf中 查找 allow from 在该位置的最下面添加 Allow from all 保存-重启apache- 索引类别:主键索引、唯一索引、普通索引- 主键索引 - primary key 唯一 不能为空- 唯一索引 - unique 唯一- 普通索引 - 创建一个普通索引 alter table 表名 index 索引名(字段名)mysql alter table stu add index username(username);Query OK, 6 rows affected (0.05 sec)Records: 6 Duplicates: 0 Warnings: 0mysql- 查看stu表中的所有的索引mysql show indexes from stuG* 1. row * Table: stu Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: id Collation: A Cardinality: 6 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment:Index_comment:* 2. row * Table: stu Non_unique: 0 Key_name: name Seq_in_index: 1 Column_name: name Collation: A Cardinality: NULL Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment:Index_comment:2 rows in set (0.01 sec)mysql- 删除普通索引 alter table 表名 drop index 索引名mysql alter table stu drop index username;Query OK, 6 rows affected (0.04 sec)Records: 6 Duplicates: 0 Warnings: 0mysql- 当前位置为 name 是唯一索引名mysql alter table stu drop index name;Query OK, 6 rows affected (0.05 sec)Records: 6 Duplicates: 0 Warnings: 0mysql- (是自增的主键,不能被直接删除)- 删除主键索引 mysql alter table de drop primary key;Query OK, 0 rows affected (0.19 sec)Records: 0 Duplicates: 0 Warnings: 0mysql- * 所有字段 查询stu表中所有字段的数据mysql select * from stu;+-+-+-+-+-+| id | username | age | sex | class |+-+-+-+-+-+| 1 | 张三 | 20 | 2 | lamp134 | 2 | 付善德 | 14 | 3 | lamp134 | 3 | 姗姗 | 18 | 3 | lamp135 | 4 | 露露 | 28 | 1 | lamp136 | 5 | 冬冬 | 13 | 2 | lamp133 |+-+-+-+-+-+5 rows in set (0.04 sec)mysql- 性别,姓名,id 三个字段的数据 相邻字段之间使用,隔开mysql select id,username,sex from stu;+-+-+-+| id | username | sex |+-+-+-+| 1 | 张三 | 2 | 2 | 付善德 | 3 | 3 | 姗姗 | 3 | 4 | 露露 | 1 | 5 | 冬冬 | 2 |+-+-+-+5 rows in set (0.00 sec)mysql - 获取当前stu表中记录数据的条数mysql select count(*) from stu;+-+| count(*) |+-+| 5 |+-+1 row in set (0.0

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论