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
下載;