一拒贱、概念:
? 數(shù)據(jù): data
? 數(shù)據(jù)庫: DB
? 數(shù)據(jù)庫管理系統(tǒng):DBMS
? 數(shù)據(jù)庫系統(tǒng):DBS
? MySQL:數(shù)據(jù)庫?
? mysql:客戶端命令(用來連接服務(wù)或發(fā)送sql指令)
? SQL:結(jié)構(gòu)化查詢語言 放坏,其中MySQL支持這個。
? SQL語言分為4個部分:DDL(定義)板鬓、DML(操作)、DQL(查詢)、DCL(控制)
? MySQL->庫->表->數(shù)據(jù)
? SQL語句中的快捷鍵
? \G 格式化輸出(文本式缠诅,豎立顯示)
? \s 查看服務(wù)器端信息
? \c 結(jié)束命令輸入操作
? \q 退出當(dāng)前sql命令行模式
? \h 查看幫助
二哑蔫、連接數(shù)據(jù)庫:
? mysql -h 主機(jī)名 -u 用戶名? -p密碼? 庫名
? C:\>mysql? --采用匿名賬號和密碼登陸本機(jī)服務(wù)
? C:\>mysql -h localhost -u root -proot? --采用root賬號和root密碼登陸本機(jī)服務(wù)
? C:\>mysql -u root -p? --推薦方式默認(rèn)登陸本機(jī)
Enter password: ****
? C:\>mysql -u root -p lamp61? --直接進(jìn)入lamp61數(shù)據(jù)庫的方式登陸
三钉寝、授權(quán):
格式:grant 允許操作 on 庫名.表名 to 賬號@來源 identified by '密碼';
--實(shí)例:創(chuàng)建zhangsan賬號弧呐,密碼123,授權(quán)l(xiāng)amp61庫下所有表的增/刪/改/查數(shù)據(jù),來源地不限
mysql> grant select,insert,update,delete on lamp61.* to zhangsan@'%' identified by '123';
mysql> grant all on *.* to zhangsan@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)
四嵌纲、SQL的基本操作
mysql> show databases; --查看當(dāng)前用戶下的所有數(shù)據(jù)庫
mysql> create database [if not exists] 數(shù)據(jù)庫名; --創(chuàng)建數(shù)據(jù)庫
mysql> use test; ? ? --選擇進(jìn)入test數(shù)據(jù)庫
mysql> show create database 數(shù)據(jù)庫名\G? --查看建數(shù)據(jù)庫語句
mysql> select database();? --查看當(dāng)前所在數(shù)據(jù)庫位置
mysql> drop database [if exists] 數(shù)據(jù)庫名;? --刪除一個數(shù)據(jù)庫
mysql> show tables; --查看當(dāng)前庫下的所有表格
mysql> desc tb1;? --查看tb1的表結(jié)構(gòu)俘枫。
mysql> show create table 表名\G? --查看表的建表語句。
mysql> create table demo( --創(chuàng)建demo表格
-> name varchar(16) not null,
-> age int,
-> sex enum('w','m') not null default 'm');
Query OK, 0 rows affected (0.05 sec)
mysql> desc demo;? --查看表結(jié)構(gòu)
+-------+---------------+------+-----+---------+-------+
| Field | Type? ? ? ? ? | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| name? | varchar(16)? | NO? |? ? | NULL? ? |? ? ? |
| age? | int(11)? ? ? | YES? |? ? | NULL? ? |? ? ? |
| sex? | enum('w','m') | NO? |? ? | m? ? ? |? ? ? |
+-------+---------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql>drop table if exists mytab;? -- 嘗試刪除mytab表格
--添加一條數(shù)據(jù)
mysql> insert into demo(name,age,sex) values('zhangsan',20,'w');
Query OK, 1 row affected (0.00 sec)
mysql> insert into demo values('lisi',22,'m'); --不指定字段名來添加數(shù)據(jù)
Query OK, 1 row affected (0.00 sec)
mysql> insert into demo(name,age) values('wangwu',23); --指定部分字段名來添加數(shù)據(jù)
Query OK, 1 row affected (0.00 sec)
--批量添加數(shù)據(jù)
mysql> insert into demo(name,age,sex) values('aaa',21,'w'),("bbb",22,'m');
Query OK, 2 rows affected (0.00 sec)
Records: 2? Duplicates: 0? Warnings: 0
mysql> select * from demo; --查詢數(shù)據(jù)
mysql> update demo set age=24 where name='aaa';? --修改
Query OK, 1 row affected (0.02 sec)
Rows matched: 1? Changed: 1? Warnings: 0
mysql> delete from demo where name='bbb';? --刪除
Query OK, 1 row affected (0.00 sec)
mysql>\h? -- 快捷幫助
mysql>\c? -- 取消命令輸入
mysql>\s? -- 查看當(dāng)前數(shù)據(jù)庫的狀態(tài)
mysql>\q? -- 退出mysql命令行
五逮走、 MySQL數(shù)據(jù)庫的數(shù)據(jù)類型:
MySQL的數(shù)據(jù)類型分為四大類:數(shù)值類型鸠蚪、字串類型、日期類型师溅、NULL茅信。
5.1 數(shù)值類型:
*tinyint(1字節(jié)) 0~255? -128~127
smallint(2字節(jié))
mediumint(3字節(jié))
*int(4字節(jié))
bigint(8字節(jié))
*float(4字節(jié))? float(6,2)
*double(8字節(jié))?
decimal(自定義)字串形數(shù)值
5.2 字串類型
普通字串
*char? 定長字串? char(8)?
*varchar 可變字串 varchar(8)
二進(jìn)制類型
tinyblob
blob
mediumblob
longblob
文本類型
tinytext
*text? ? ? 常用于<textarea></textarea>
mediumtext
longtext
*enum枚舉
set集合
5.3 時間和日期類型:
date? 年月日
time? 時分秒
datetime 年月日時分秒
timestamp 時間戳
year 年
5.4 NULL值
NULL意味著“沒有值”或“未知值”
可以測試某個值是否為NULL
不能對NULL值進(jìn)行算術(shù)計算
對NULL值進(jìn)行算術(shù)運(yùn)算,其結(jié)果還是NULL
0或NULL都意味著假墓臭,其余值都意味著真
MySQL的運(yùn)算符:
算術(shù)運(yùn)算符:+ - * / %
比較運(yùn)算符:= > < >= <= <> !=
數(shù)據(jù)庫特有的比較:in蘸鲸,not in, is null,is not null,like, between and
邏輯運(yùn)算符:and or not
like: 支持特殊符號%和_ ; 其中 %表示任意數(shù)量的任意字符,_表示任意一位字符窿锉。
六酌摇、 表的字段約束:
unsigned 無符號(正數(shù))
zerofill 前導(dǎo)零填充
auto_increment? 自增
default 默認(rèn)值
not null? 非空
PRIMARY KEY 主鍵 (非null并不重復(fù))
unique 唯一性? (可以為null但不重復(fù))
index 常規(guī)索引
七、 建表語句格式:
create table 表名(
? 字段名 類型 [字段約束],
? 字段名 類型 [字段約束],
? 字段名 類型 [字段約束]
? ...
? );
mysql> create table stu(
-> id int unsigned not null auto_increment primary key,
-> name varchar(8) not null unique,
-> age tinyint unsigned,
-> sex enum('m','w') not null default 'm',
-> classid char(6)
-> );
Query OK, 0 rows affected (0.05 sec)
mysql> desc stu;
+---------+---------------------+------+-----+---------+----------------+
| Field? | Type? ? ? ? ? ? ? ? | Null | Key | Default | Extra? ? ? ? ? |
+---------+---------------------+------+-----+---------+----------------+
| id? ? ? | int(10) unsigned? ? | NO? | PRI | NULL? ? | auto_increment |
| name? ? | varchar(8)? ? ? ? ? | NO? | UNI | NULL? ? |? ? ? ? ? ? ? ? |
| age? ? | tinyint(3) unsigned | YES? |? ? | NULL? ? |? ? ? ? ? ? ? ? |
| sex? ? | enum('m','w')? ? ? | NO? |? ? | m? ? ? |? ? ? ? ? ? ? ? |
| classid | char(6)? ? ? ? ? ? | YES? |? ? | NULL? ? |? ? ? ? ? ? ? ? |
+---------+---------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> show create table stu\G? --查看建表的語句
*************************** 1. row ***************************
? Table: stu
Create Table: CREATE TABLE `stu` (
? `id` int(10) unsigned NOT NULL auto_increment,
? `name` varchar(8) NOT NULL,
? `age` tinyint(3) unsigned default NULL,
? `sex` enum('m','w') NOT NULL default 'm',
? `classid` char(6) default NULL,
? PRIMARY KEY? (`id`),
? UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql>
mysql> insert into stu(id,name,age,sex,classid) values(1,'zhangsan',20,'m','lamp
61');
Query OK, 1 row affected (0.00 sec)
mysql> insert into stu(name,age,sex,classid) values('lisi',22,'w','lamp61');
Query OK, 1 row affected (0.00 sec)
mysql> insert into stu(name,age,classid) values('wangwu',21,'lamp61');
Query OK, 1 row affected (0.00 sec)
mysql> insert into stu values(null,'qq',24,'w','lamp62');
Query OK, 1 row affected (0.00 sec)
mysql> insert into stu values(null,'aa',20,'m','lamp62'),(null,'bb',25,'m','lamp
63');
Query OK, 2 rows affected (0.00 sec)
Records: 2? Duplicates: 0? Warnings: 0
mysql> select * from stu;
+----+----------+------+-----+---------+
| id | name? ? | age? | sex | classid |
+----+----------+------+-----+---------+
|? 1 | zhangsan |? 20 | m? | lamp61? |
|? 2 | lisi? ? |? 22 | w? | lamp61? |
|? 3 | wangwu? |? 21 | m? | lamp61? |
|? 4 | qq? ? ? |? 24 | w? | lamp62? |
|? 5 | aa? ? ? |? 20 | m? | lamp62? |
|? 6 | bb? ? ? |? 25 | m? | lamp63? |
+----+----------+------+-----+---------+
6 rows in set (0.00 sec)
八嗡载、修改表結(jié)構(gòu):
-------------------------------------
? ? 格式: alter table 表名 action(更改選項);
? ? 更改選項:
? ? ? ? 1. 添加字段:alter table 表名 add 字段名信息
? ? ? ? ? ? 例如:
? ? ? ? ? ? ? ? -- 在user表的最后追加一個num字段 設(shè)置為int not null
? ? ? ? ? ? ? ? mysql> alter table user add num int not null;
? ? ? ? ? ? ? ? -- 在user表的email字段后添加一個age字段窑多,設(shè)置int not null default 20;
? ? ? ? ? ? ? ? mysql> alter table user add age int not null default 20 after email;
? ? ? ? ? ? ? ? -- 在user表的最前面添加一個aa字段設(shè)置為int類型
? ? ? ? ? ? ? ? mysql> alter table user add aa int first;
? ? ? ? 2. 刪除字段:alter table 表名 drop 被刪除的字段名
? ? ? ? ? ? 例如:-- 刪除user表的aa字段
? ? ? ? ? ? ? ? mysql> alter table user drop aa;
? ? ? ? 3. 修改字段:alter table 表名 change[modify] 被修改后的字段信息
? ? ? ? ? ? 其中:change可以修改字段名洼滚, modify 不修改
? ? ? ? ? ? 例如:
? ? ? ? ? ? -- 修改user表中age字段信息(類型)埂息,(使用modify關(guān)鍵字的目的不修改字段名)
? ? ? ? ? ? mysql> alter table user modify age tinyint unsigned not null default 20;
? ? ? ? ? ? -- 修改user表的num字段改為mm字段并添加了默認(rèn)值(使用change可改字段名)
? ? ? ? ? ? mysql> alter table user change num mm int not null default 10;
? ? ? ? 4. 添加和刪除索引
? ? ? ? ? ? -- 為user表中的name字段添加唯一性索引,索引名為uni_name;
? ? ? ? ? ? mysql> alter table user add unique uni_name(name);
? ? ? ? ? ? -- 為user表中的email字段添加普通索引遥巴,索引名為index_eamil
? ? ? ? ? ? mysql> alter table user add index index_email(email);
? ? ? ? ? ? -- 將user表中index_email的索引刪除
? ? ? ? ? ? mysql> alter table user drop index index_email;
? ? ? ? 5. 更改表名稱:
? ? ? ? ? ? ALTER TABLE 舊表名 RENAME AS 新表名
? ? ? ? 6. 更改AUTO_INCREMENT初始值:
? ? ? ? ? ? ALTER TABLE 表名稱 AUTO_INCREMENT=1
? ? ? ? 7. 更改表類型:
? ? ? ? ? ? ALTER TABLE 表名稱 ENGINE="InnoDB"
? ? MySQL數(shù)據(jù)庫中的表類型一般常用兩種:MyISAM和InnoDB
? ? 區(qū)別:MyISAM類型的數(shù)據(jù)文件有三個frm(結(jié)構(gòu))耿芹、MYD(數(shù)據(jù))、MYI(索引)
? ? ? ? ? MyISAM類型中的表數(shù)據(jù)增 刪 改速度快挪哄,不支持事務(wù)吧秕,沒有InnoDB安全。
? ? ? ? ? InnoDB類型的數(shù)據(jù)文件只有一個 .frm
? ? ? ? ? InnoDB類型的表數(shù)據(jù)增 刪 改速度沒有MyISAM的快迹炼,但支持事務(wù)砸彬,相對安全。
九斯入、數(shù)據(jù)的DML操作:添加數(shù)據(jù)砂碉,修改數(shù)據(jù),刪除數(shù)據(jù):
----------------------------------------------------------
? ? 1. 添加數(shù)據(jù)
? ? ? ? 格式: insert into 表名[(字段列表)] values(值列表...);
? ? ? ? --標(biāo)準(zhǔn)添加(指定所有字段刻两,給定所有的值)
? ? ? ? mysql> insert into stu(id,name,age,sex,classid) values(1,'zhangsan',20,'m','lamp138');
? ? ? ? Query OK, 1 row affected (0.13 sec)
? ? ? ? mysql>
? ? ? ? --指定部分字段添加值
? ? ? ? mysql> insert into stu(name,classid) value('lisi','lamp138');
? ? ? ? Query OK, 1 row affected (0.11 sec)
? ? ? ? -- 不指定字段添加值
? ? ? ? mysql> insert into stu value(null,'wangwu',21,'w','lamp138');
? ? ? ? Query OK, 1 row affected (0.22 sec)
? ? ? ? -- 批量添加值
? ? ? ? mysql> insert into stu values
? ? ? ? ? ? -> (null,'zhaoliu',25,'w','lamp94'),
? ? ? ? ? ? -> (null,'uu01',26,'m','lamp94'),
? ? ? ? ? ? -> (null,'uu02',28,'w','lamp92'),
? ? ? ? ? ? -> (null,'qq02',24,'m','lamp92'),
? ? ? ? ? ? -> (null,'uu03',32,'m','lamp138'),
? ? ? ? ? ? -> (null,'qq03',23,'w','lamp94'),
? ? ? ? ? ? -> (null,'aa',19,'m','lamp138');
? ? ? ? Query OK, 7 rows affected (0.27 sec)
? ? ? ? Records: 7? Duplicates: 0? Warnings: 0
? ? 2. 修改操作:
? ? ? ? 格式:update 表名 set 字段1=值1,字段2=值2,字段n=值n... where 條件
? ? ? ? -- 將id為11的age改為35增蹭,sex改為m值
? ? ? ? mysql> update stu set age=35,sex='m' where id=11;
? ? ? ? Query OK, 1 row affected (0.16 sec)
? ? ? ? Rows matched: 1? Changed: 1? Warnings: 0
? ? ? ? -- 將id值為12和14的數(shù)據(jù)值sex改為m,classid改為lamp92
? ? ? ? mysql> update stu set sex='m',classid='lamp92' where id=12 or id=14 --等價于下面
? ? ? ? mysql> update stu set sex='m',classid='lamp92' where id in(12,14);
? ? ? ? Query OK, 2 rows affected (0.09 sec)
? ? ? ? Rows matched: 2? Changed: 2? Warnings: 0
? ? 3. 刪除操作
? ? ? ? 格式:delete from 表名 [where 條件]
? ? ? ? -- 刪除stu表中id值為100的數(shù)據(jù)
? ? ? ? mysql> delete from stu where id=100;
? ? ? ? Query OK, 0 rows affected (0.00 sec)
? ? ? ? -- 刪除stu表中id值為20到30的數(shù)據(jù)
? ? ? ? mysql> delete from stu where id>=20 and id<=30;
? ? ? ? Query OK, 0 rows affected (0.00 sec)
? ? ? ? -- 刪除stu表中id值為20到30的數(shù)據(jù)(等級于上面寫法)
? ? ? ? mysql> delete from stu where id between 20 and 30;
? ? ? ? Query OK, 0 rows affected (0.00 sec)
? ? ? ? -- 刪除stu表中id值大于200的數(shù)據(jù)
? ? ? ? mysql> delete from stu where id>200;
? ? ? ? Query OK, 0 rows affected (0.00 sec)
十磅摹、數(shù)據(jù)的DQL操作:數(shù)據(jù)查詢
==============================================
? ? 格式:
? ? ? ? select [字段列表]|* from 表名
? ? ? ? [where 搜索條件]
? ? ? ? [group by 分組字段 [having 子條件]]
? ? ? ? [order by 排序 asc|desc]
? ? ? ? [limit 分頁參數(shù)]
? ? mysql> select * from stu;
? ? +----+----------+-----+-----+---------+
? ? | id | name? ? | age | sex | classid |
? ? +----+----------+-----+-----+---------+
? ? |? 1 | zhangsan |? 20 | m? | lamp138? |
? ? |? 2 | lisi? ? |? 20 | m? | lamp138? |
? ? |? 3 | wangwu? |? 21 | w? | lamp138? |
? ? |? 4 | zhaoliu? |? 25 | w? | lamp94? |
? ? |? 5 | uu01? ? |? 26 | m? | lamp94? |
? ? |? 6 | uu02? ? |? 28 | w? | lamp92? |
? ? |? 7 | qq02? ? |? 24 | m? | lamp92? |
? ? |? 8 | uu03? ? |? 32 | m? | lamp138? |
? ? |? 9 | qq03? ? |? 23 | w? | lamp94? |
? ? | 10 | aa? ? ? |? 19 | m? | lamp138? |
? ? | 11 | sad? ? ? |? 35 | m? | lamp94? |
? ? | 12 | tt? ? ? |? 25 | m? | lamp92? |
? ? | 13 | wer? ? ? |? 25 | w? | lamp94? |
? ? | 14 | xx? ? ? |? 25 | m? | lamp92? |
? ? | 15 | kk? ? ? |? 0 | w? | lamp94? |
? ? +----+----------+-----+-----+---------+
? ? 15 rows in set (0.00 sec)
十一滋迈、where條件查詢:
? ? 1. 查詢班級為lamp138期的學(xué)生信息
? ? mysql> select * from stu where classid='lamp138';
? ? 2. 查詢lamp138期的男生信息(sex為m)
? ? mysql> select * from stu where classid='lamp138' and sex='m';
? ? 3. 查詢id號值在10以上的學(xué)生信息
? ? mysql> select * from? stu where id>10;
? ? 4. 查詢年齡在20至25歲的學(xué)生信息
? ? mysql> select * from stu where age>=20 and age<=25;
? ? mysql> select * from stu where age between 20 and 25;
? ? 5. 查詢年齡不在20至25歲的學(xué)生信息
? ? mysql> select * from stu where age not between 20 and 25;
? ? mysql> select * from stu where age<20 or age>25;
? ? 6. 查詢id值為1,8,4,10,14的學(xué)生信息
? ? select * from stu where id in(1,8,4,10,14);
? ? mysql> select * from stu where id=1 or id=8 or id=4 or id=10 or id=14;
? ? 7. 查詢lamp138和lamp94期的女生信息
? ? mysql> select * from stu where classid in('lamp138','lamp94') and sex='w';
? ? mysql> select * from stu where (classid='lamp138' or classid='lamp94') and sex='w
十二霎奢、導(dǎo)入和導(dǎo)出:
-----------------------------------
-- 將lamp138庫導(dǎo)出
D:\>mysqldump -u root -p lamp138 >lamp138.sql
Enter password:
---- 將lamp138庫中的stu表導(dǎo)出
D:\>mysqldump -u root -p lamp138 stu >lamp138_stu.sql
Enter password:
-- 將lamp138庫導(dǎo)入
D:\>mysql -u root -p lamp138<lamp138.sql
Enter password:
-- 將lamp138庫中stu表導(dǎo)入
D:\>mysql -u root -p lamp138<lamp138_stu.sql
Enter password:
D:\>