mysql 从入门到放弃
下载: http://dev.mysql.com/downloads/mysql/5.1.html#downloads
配置
- 将下载的 mysql-noinstall-5.1.69-win32.zip 解压至需要安装的位置, 如: C:\Program Files;
- 在安装文件夹下找到 my-small.ini 配置文件, 将其重命名为 my.ini , 打开进行编辑, 在 [client] 与 [mysqld] 下均添加一行: default-character-set = gbk
- 打开 Windows 环境变量设置, 新建变量名 MYSQL_HOME , 变量值为 MySQL 安装目录路径, 这里为 C:\Program Files\mysql-5.1.69-win32
- 在 环境变量 的 Path 变量中添加 ;%MYSQL_HOME%\bin;
- 安装 MySQL 服务, 打开Windows命令提示符, 执行命令: mysqld –install MySQL –defaults-file=”my.ini”提示”Service successfully installed.”表示成功;
启动net start MySQL
;停止net stop MySQL
;卸载sc delete MySQL
基本组成
保存时脚本文件后缀名为.sql
。在控制台下,MySQL客户端也可以对语句进行单句的执行而不用保存为.sql
文件
- 标识符
- 关键字
- 语句
- 函数
MySQL中的数据类型
- 数字类型
- 整数:tinyint,smallint,mediumint,int,bigint;
- 浮点数:float,double,real,decimal
日期和时间
date/time/datetime/timestamp/year字符串类型
- 字符串:char/varchar
- 文本:tinytext/text/mediumtext/longtext
- 二进制:tinyblob/blob/mediumblob/longblub
使用MySQL数据库
- 登陆MySQL
cmd→mysql -h 主机名 -u用户名 - p
-h: 该命令用于指定客户端所要登陆的MySQL主机名,登陆当前机器该参数可以省略;
-u:所要登陆的用户名
-p:告诉服务器将会使用一个密码来登陆,如果所要登陆的用户名密码为空,可以忽略此选项
我们输入这个
mysql -u root -p
得到Enter password:
若存在密码则输入,否则回车。一般默认root账号是无密码的。
然后可以看到Welecome to the MySQL monitor...
可以exit或者quit退出登录(或者ctrl+C)
- 创建数据库
格式:create database 数据库名[其他选项]
我们输入(mysql中)
create database huchi character set gbk;
得到回应Query OK, 1 row affected (0.16 sec)
MySQL 语句以;作为语句的结束,若在语句皆为不添加分号时,命令会一直提示你继续输入(有特例,但最好加上;);
使用show databases;
查看已经创建的数据库
- 选择所要操作的数据库
要对一个数据库进行操作,必须先选择该数据库,否则会提示错误
ERROR 1046(3D000):No database selected
两种方式对数据库进行使用的选择
一. 在登陆数据库时指定:mysql -D huchi -u root -p
二.在登陆后使用use语句指定,命令use 数据库名;
use huchi
切换到huchi数据库.
- 创建数据库表
使用create table 表声明(列声明)
还有什么比贴代码更好玩的事
MySQL [huchi]> create table students
-> (
-> 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 "-"
-> );
Query OK, 0 rows affected (2.10 sec)
直接输入容易出错,比较麻烦
create table students2
(
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 "-"
);
以 “id int unsigned not null auto_increment primary key” 行进行介绍:
- “id” 为列的名称;
- “int” 指定该列的类型为 int(取值范围为 -8388608到8388607), 在后面我们又用 “unsigned” 加以修饰, 表示该类型为无符号型, 此时该列的取值范围为 0到16777215;
- “not null” 说明该列的值不能为空, 必须要填, 如果不指定该属性, 默认可为空;
- “auto_increment” 需在整数列中使用, 其作用是在插入数据时若该列为 NULL, MySQL将自动产生一个比现存值更大的唯一标识符值。在每张表中仅能有一个这样的值且所在列必须为索引列。
- “primary key” 表示该列是表的主键, 本列的值必须唯一, MySQL将自动索引该列。
下面的 char(8) 表示存储的字符长度为8, tinyint的取值范围为 -127到128, default 属性指定当该列值为空时的默认值。
保存到createtable.sql
文件中(推荐用sublime)
再来cmd:mysql -D huchi -u root -p < createtable.sql
//(提示: 1.如果连接远程主机请加上 -h 指令; 2. createtable.sql 文件若不在当前工作目录下需指定文件的完整路径。)
//查看表名称和表的详细信息
show tables;
describe students2;
MySQL [huchi]> describe students2
-> ;
+-------+---------------------+------+-----+---------+----------------+
| 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 | | - | |
+-------+---------------------+------+-----+---------+----------------+
5 rows in set (0.36 sec)
操作MySQL数据库
向表中插入数据
- insert语句可以用来插入:
insert [into] 表名 [(列名1, ...)] values (值1, ... )
例如:
MySQL [huchi]> insert students values(NULL, "huchi", "boy", 18, "110"); Query OK, 1 row affected (0.07 sec) MySQL [huchi]> insert into students (name,sex,age) values("lmh", "boy" , 19); Query OK, 1 row affected (0.01 sec)
- insert语句可以用来插入:
查询表中的数据
select语句:
select 列表名 from 表名称 [查询条件];
MySQL [huchi]> select name,age from students; +-------+-----+ | name | age | +-------+-----+ | huchi | 18 | | lmh | 19 | | lhd | 19 | | lxa | 19 | +-------+-----+ 4 rows in set (0.00 sec) MySQL [huchi]> select * from students; +----+-------+------+-----+-------------+ | id | name | sex | age | tel | +----+-------+------+-----+-------------+ | 1 | huchi | boy | 18 | 110 | | 2 | lmh | boy | 19 | - | | 3 | lhd | girl | 19 | - | | 4 | lxa | girl | 19 | - | +----+-------+------+-----+-------------+ 4 rows in set (0.00 sec)
按特定条件查询:
- where 关键词指定查询条件:
select 列名称 from 表名称 where 条件
MySQL [huchi]> select * from students where sex = "boy"; +----+-------+-----+-----+-------------+ | id | name | sex | age | tel | +----+-------+-----+-----+-------------+ | 1 | huchi | boy | 18 | 110 | | 2 | lmh | boy | 19 | - | +----+-------+-----+-----+-------------+ 2 rows in set (0.04 sec)
where 不仅可以支持 = ,还可以用<= 之类以及is [not] null ,in , like,or,and等等
MySQL [huchi]> select * from students where name like "hu%"; +----+-------+-----+-----+-------------+ | id | name | sex | age | tel | +----+-------+-----+-----+-------------+ | 1 | huchi | boy | 18 | 110 | +----+-------+-----+-----+-------------+ 1 row in set (0.01 sec)
- where 关键词指定查询条件:
更新表中的数据
- update :
update 表名称 set 列名称 = 新值 [where 更新条件];
MySQL [huchi]> update students set tel="119" where id = 1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 MySQL [huchi]> update students set age = age + 1; Query OK, 4 rows affected (0.02 sec) Rows matched: 4 Changed: 4 Warnings: 0
- update :
删除表中的数据
- delete 语句用于删除表中的数据:
delete from 表名称 where 删除条件;
MySQL [huchi]> delete from students; Query OK, 4 rows affected (0.00 sec) MySQL [huchi]> select * from students; Empty set (0.00 sec) MySQL [huchi]> insert students (name , sex ,age) values ("huchi", "boy", 19); Query OK, 1 row affected (0.00 sec) MySQL [huchi]> insert students (name , sex ,age) values ("lmh", "boy", 20); Query OK, 1 row affected (0.00 sec)
//真是个悲伤的故事。。。。
- delete 语句用于删除表中的数据:
创建后表的修改
alter table 语句用于创建后对表的修改
- 添加列:
alter table 表名 add 列名 列数据类型 [after 插入位置];
MySQL [huchi]> alter table students add address char(60);
Query OK, 0 rows affected (0.42 sec)
Records: 0 Duplicates: 0 Warnings: 0
MySQL [huchi]> alter table students add birthday date after age;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
MySQL [huchi]> describe students;
+----------+---------------------+------+-----+---------+----------------+
| 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 | |
| birthday | date | YES | | NULL | |
| tel | char(13) | YES | | - | |
| address | char(60) | YES | | NULL | |
+----------+---------------------+------+-----+---------+----------------+
7 rows in set (0.04 sec)
- 修改列:
alter table 表名 change 列名称 列新名称 新名称数据类型;
MySQL [huchi]> alter table students change address ad char(60) default "-";
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
MySQL [huchi]> alter table students change name name char(16) not null;
Query OK, 4 rows affected (0.07 sec)
Records: 4 Duplicates: 0 Warnings: 0
MySQL [huchi]> describe students;
+----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | char(16) | NO | | NULL | |
| sex | char(4) | NO | | NULL | |
| age | tinyint(3) unsigned | NO | | NULL | |
| birthday | date | YES | | NULL | |
| tel | char(13) | YES | | - | |
| ad | char(60) | YES | | - | |
+----------+---------------------+------+-----+---------+----------------+
7 rows in set (0.08 sec)
- 删除列:
alter table 表名 drop列名称
MySQL [huchi]> alter table students drop birthday;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
MySQL [huchi]> describe students;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | char(16) | NO | | NULL | |
| sex | char(4) | NO | | NULL | |
| age | tinyint(3) unsigned | NO | | NULL | |
| tel | char(13) | YES | | - | |
| ad | char(60) | YES | | - | |
+-------+---------------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
- 重命名表
alter table 表名 rename 新表名;
MySQL [huchi]> alter table students rename friends;
Query OK, 0 rows affected (0.01 sec)
MySQL [huchi]> show tables;
+-----------------+
| Tables_in_huchi |
+-----------------+
| friends |
| stu12 |
| students2 |
+-----------------+
3 rows in set (0.00 sec)
- 删除表:
drop table 表名;
MySQL [huchi]> drop table stu12;
Query OK, 0 rows affected (0.06 sec)
MySQL [huchi]> show tables;
+-----------------+
| Tables_in_huchi |
+-----------------+
| friends |
| students2 |
+-----------------+
2 rows in set (0.00 sec)
删除数据库。。。。。
drop database 数据库名;
少年请自制;
Extra
- 修改root 用户密码
cmd→mysqladmin -u root -p password 新密码
- 可视化管理工具
MySQL Workbench
下载;