MYSQL的安装(ZIP版)
1.直接百度搜索MYSQL下载关键字,使用普通下载即可;
2.解压缩文件并把应用程序文件移动到目标路径,E:\Program Files (x86)\MYSQL\mysql-5.6,把外层文件夹改为简单明了的名称;
MySQL 5.6 for Windows 解压缩版配置安装
参见 百度百科
MYSQL建用户,建库,建表,授权
MySql安装与配置方法(MySQL添加用户、删除用户与授权)
实际操练:
登入数据库
mysql -h主机名 -u 用户名 -p 密码
mysql -h localhost -u root –p
退出错误命令:
‘\c;
查看数据库和用户状态
查看既有数据库
show databases;
查看用户和所属主机信息
SELECT User, Host, Password FROM mysql.user;
用户维护
创建用户1
insert into mysql.user(Host,User,Password) values('localhost','tanzuai',password('123456'));
创建用户zhaoyi ,密码为:123456
insert into mysql.user(host,user,password) values('localhost','zhaoyi',password('123456'));
设定密码之后需要,刷新才能生效。
flush privileges ;
创建用户2:
先建立一个空数据库:mydb;
create database mydb;
格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码";
例如:给既有的mydb数据库 赋权给zhaoyi
select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14个权限,如果全部赋权则:ALL PRIVILEGES
grant ALL PRIVILEGES on mydb.* to zhaoyi@localhost identified by '123456';
查看用户权限:
show grants for 'zhaoyi'@'localhost';
权限收回:
REVOKE GRANT OPTION ON 数据库 FROM 用户名; / 回收部分权限
REVOKE ALL PRIVILEGES ON 数据库名FROM用户名; //回收特定数据库的全部权限
REVOKE ALL PRIVILEGES ON *.* FROM 用户名; //回收所有数据库的全部权限
例子:
revoke drop on mydb.* from 'zhaoyi'@'localhost';
REVOKE ALL PRIVILEGES ON mydb.* from 'zhaoyi'@'localhost';
flush privileges ;
用户删除
Delete FROM user Where User='用户名' and Host='主机名';
例如删除用户zhaoyi:
use mysql
delete from user where user='zhaoyi' and host='localhost';
等价于
delete from mysql.user where user='zhaoyi' and host='localhost';
flush privileges;
数据库操作
创建数据库
create database 数据库名 [其他选项];
例子:
create database mydb character set utf8;
删除数据库
drop database mydb;
显示数据库
Show databases;
建表
create table 表名称(列声明);
例子:
use mydb
create table mytable
(id int unsigned not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "-")engine=InnoDB default charset=utf8 auto_increment=1;
查看表
Show tables;
查看表结构:
desc mydb;
删除表:
Drop table 表名称
例子:
drop table mydb;
mysql> desc mytable;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | char(8) | NO | | NULL | |
| sex | char(4) | NO | | NULL | |
| age | tinyint(3) unsigned | NO | | NULL | |
| tel | char(13) | YES | | - | |
+-------+---------------------+------+-----+---------+----------------+
操作MySQL数据库
格式:insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);
例子:
insert into mytable values(NULL,'zhaoyi','male',20,'13838871361');
insert into mytable values(NULL,'赵毅','男',20,'13838871361');
表查询
格式:select 字段 from 表名称 [查询条件];
例子:mysql> select id,name,tel from mytable;
+----+--------+-------------+
| id | name | tel |
+----+--------+-------------+
| 1 | zhaoyi | 13838871289 |
| 2 | 赵毅 | 13838871289 |
+----+--------+-------------+
也可以使用通配符 * 查询表中所有的内容, 语句: select * from 表名称;
select 列名称 from 表名称 where 条件;
例如查询查询性别为’男’的信息
mysql> select * from mytable where sex='男';
+----+------+-----+-----+-------------+
| id | name | sex | age | tel |
+----+------+-----+-----+-------------+
| 2 | 赵毅 | 男 | 20 | 13838871361 |
+----+------+-----+-----+-------------+
表更改
update 表名称 set 列名称=新值 where 更新条件;
例如把名字叫‘赵毅’的人的电话改为:13673613589
update mytable set tel='13673613589' where name='赵毅';
select * from mytable
表删除
delete from 表名称 where 删除条件;
例如把性别为‘男’的记录删除
delete from mytable where sex='男’;
插入新字段
格式:alter table 表名 add 字段 列数据类型 [after 插入位置];
例如 在name字段后添加地址adress 字段
alter table mytable add adress char(60) null default '--' after name;
mysql> desc mytable;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | char(8) | NO | | NULL | |
| adress | char(60) | YES | | -- | |
| sex | char(4) | NO | | NULL | |
| age | tinyint(3) unsigned | NO | | NULL | |
| tel | char(13) | YES | | - | |
+--------+---------------------+------+-----+---------+----------------+
更改既有字段
格式:alter table 表名 change 列名称 列新名称 新数据类型;
例如:把name的数据类型改为char(20)
alter table mytable change name names char(20);
字段删除
alter table 表名 drop 列名称;
例如把adress字段删除
alter table mytable drop adress;
重命名表
alter table 表名 rename 新表名;
例如把表mytable命名为zytable;
alter table mytable rename zytable;
查询表字段和数据类型
select column_name from information_schema.COLUMNS where TABLE_SCHEMA='mydb' and TABLE_NAME='zytable';
select DATA_TYPE from information_schema.COLUMNS where TABLE_SCHEMA='mydb' and TABLE_NAME='zytable';
select column_name, DATA_TYPE from information_schema.COLUMNS where TABLE_SCHEMA='mydb' and TABLE_NAME='zytable';
//查询记录行从第一条到第n+1条的记录.
select * from zytable limit 0,n; //查询记录行从第一条到第n+1条的记录.
如何实现主键id 与数据表行号的对应查询:
set @mycnt = 0;select id, (@mycnt:=@mycnt+1) as rownum from zytable order by id asc/desc;
再进行计数:
set @mycnt = 0;
select count(*) from (select (@mycnt:=@mycnt+1) as rownum from zytable) as total;
咨询电话
0371-68632068