mysql.exe -h localhost -P 3306 -u root -p
use mydb;?????——???? 進(jìn)入數(shù)據(jù)庫(kù)
查看:show index from 表名\G
desc:查看表結(jié)構(gòu)
select * from 表名:查詢所有數(shù)據(jù)
交互方式
1神汹、客戶端連接認(rèn)證:連接服務(wù)器、認(rèn)證身份(mysql.exe -hPup)?
2古今、客戶端發(fā)送SQL指令
3屁魏、服務(wù)器接收SQL指令,并處理SQL指令捉腥,返回操作結(jié)果
4氓拼、客戶端接收結(jié)果,并顯示結(jié)果
5抵碟、斷開(kāi)連接
MySQL服務(wù)器內(nèi)部對(duì)象分成了四層:
系統(tǒng)(DBMS):管理數(shù)據(jù)庫(kù)的
數(shù)據(jù)庫(kù)(DB):放數(shù)據(jù)表的
數(shù)據(jù)表(Table):管理字段的
字段(Field) :存放數(shù)據(jù)的
將SQL的基本操作根據(jù)操作對(duì)象進(jìn)行分類桃漾,可分為三類: (庫(kù)操作、表操作(包含字段操作) 立磁、數(shù)據(jù)操作?)
庫(kù)操作?
? ??新增數(shù)據(jù)庫(kù):
? ? ? ? 1呈队、create database 數(shù)據(jù)庫(kù)名字 [庫(kù)選項(xiàng)];
? ? ? ? 2、庫(kù)選項(xiàng):用來(lái)約束數(shù)據(jù)庫(kù)唱歧,分為兩個(gè)選項(xiàng):
? ? ? ? ? ? ? ? 1)字符集設(shè)定:charset/character set 具體字符集(數(shù)據(jù)存儲(chǔ)的編碼格式宪摧,常用的有:GBK和UTF8)
? ? ? ? ? ? ? ? 2)校對(duì)集設(shè)定:collate 具體校對(duì)集(數(shù)據(jù)比較的規(guī)則)
? ? ? ? 3、數(shù)據(jù)庫(kù)名字不能用關(guān)鍵字(已經(jīng)被系統(tǒng)使用的字符)或者保留字(將來(lái)系統(tǒng)可能會(huì)用到的字符)
? ??創(chuàng)建結(jié)果:
????????在數(shù)據(jù)庫(kù)系統(tǒng)中颅崩,增加了對(duì)應(yīng)的數(shù)據(jù)庫(kù)信息
????????會(huì)在保存數(shù)據(jù)的文件夾下(Data目錄)几于,創(chuàng)建一個(gè)對(duì)應(yīng)數(shù)據(jù)庫(kù)名字的文件夾
????????每個(gè)數(shù)據(jù)庫(kù)下都有一個(gè)opt文件,保存了庫(kù)選項(xiàng)
表操作(包含字段操作)?
數(shù)據(jù)操作
注釋
--? 雙中劃線+空格:注釋(單行注釋)沿后,也可以使用#號(hào)
mysql.exe -h localhost -P 3306 -u root -p
-h:找到主機(jī)地址在哪
localhost:主機(jī)地址?
-P:端口(統(tǒng)一默認(rèn)3306)
-u:用戶名(默認(rèn)root)
-p:密碼
客戶端鏈接認(rèn)證
mysql.exe -h localhost -P 3306 -u root -p
查看所有數(shù)據(jù)庫(kù)
show databases沿彭;
斷開(kāi)連接
exit、quit尖滚、\q
--? 雙中劃線+空格:注釋(單行注釋)喉刘,也可以使用#號(hào)
-- 創(chuàng)建數(shù)據(jù)庫(kù)
create database mydb charset utf8;
表示該數(shù)據(jù)庫(kù)已存在瞧柔,要想繼續(xù)可另起名字或drop database `mydb`;
表示已刪除
-- 創(chuàng)建關(guān)鍵字?jǐn)?shù)據(jù)庫(kù)
create database database charset utf8;-- 報(bào)錯(cuò)
-- 使用反引號(hào)(非要使用關(guān)鍵字命名的數(shù)據(jù)庫(kù)加反引號(hào))
create database `database` charset utf8;
--創(chuàng)建中文數(shù)據(jù)庫(kù)
create database 北京 charset utf8;
-- 如果報(bào)錯(cuò)解決方案:告訴服務(wù)器當(dāng)前中文的字符集是什么
右鍵命令行窗口——>屬性——>選項(xiàng)——>當(dāng)前代碼頁(yè)(簡(jiǎn)體中文GBK)
先執(zhí)行:set names gbk;
再執(zhí)行:create database?北京 charset utf8;
查看數(shù)據(jù)庫(kù)
?? ? ? ?1、查看所有數(shù)據(jù)庫(kù):show databases;
? ? ? ? 2睦裳、查看指定部分的數(shù)據(jù)庫(kù)(模糊查詢):show databases like 'pattern';
? ? ? ? ? ? ? ?1) pattern是匹配模式
? ? ? ? ? ? ? ? 2)%表示匹配多個(gè)字符
? ? ? ? ? ? ? ? 3)_表示匹配單個(gè)字符
? ? ? ? 3造锅、查看數(shù)據(jù)庫(kù)的創(chuàng)建語(yǔ)句:show create database 數(shù)據(jù)庫(kù)名字;
-- 查看所有數(shù)據(jù)庫(kù)
show databases;
-- 創(chuàng)建數(shù)據(jù)庫(kù)
create database informationtest charset utf8;
-- 以informationtion_開(kāi)始的數(shù)據(jù)庫(kù)(_需要被轉(zhuǎn)義)
show databases like 'information_%';-- 相當(dāng)于information%
show databases like 'information\_%';
-- 查看數(shù)據(jù)庫(kù)的創(chuàng)建語(yǔ)句
show create database mybd;
show create database?`database`; -- 關(guān)鍵字需要使用反引號(hào)
更新數(shù)據(jù)庫(kù)
????????????數(shù)據(jù)庫(kù)名字不可以修改
????????????數(shù)據(jù)庫(kù)的修改僅限庫(kù)選項(xiàng),即字符集和校對(duì)集(校對(duì)集依賴字符集)
????????????alter database 數(shù)據(jù)庫(kù)名字 [庫(kù)選項(xiàng)];
????????????charset/character set [=] 字符集
????????????collate 校對(duì)集
刪除數(shù)據(jù)庫(kù)
drop database 數(shù)據(jù)庫(kù)名字;
-- 修改數(shù)據(jù)庫(kù)informationtest的字符集
alter database informationtest charset GBK;
注意:刪除數(shù)據(jù)庫(kù)有風(fēng)險(xiǎn)廉邑,需備份
-- 刪除數(shù)據(jù)庫(kù):(drop database 數(shù)據(jù)庫(kù)名字;)
drop database informationtest;
表操作
--創(chuàng)建表
如何解決沒(méi)有數(shù)據(jù)庫(kù)選擇哥蔚?
方案一:
顯式地指定表所屬的數(shù)據(jù)庫(kù):
create table 數(shù)據(jù)庫(kù)名.表名();
create table if not exists mydb.student(-- 顯示地將student表放到mybd數(shù)據(jù)庫(kù)下
name varchar(10),
gender varchar(10),
number varchar(10),
age int
)charset utf8;
方案二:
隱式地指定表所屬數(shù)據(jù)庫(kù)
進(jìn)入數(shù)據(jù)庫(kù)環(huán)境:use 數(shù)據(jù)庫(kù)名字;
--創(chuàng)建數(shù)據(jù)庫(kù)表
-- 進(jìn)入數(shù)據(jù)庫(kù)
use mydb;
-- 創(chuàng)建表
create table class(
name varchar(10),
room varchar(10)
)charset utf8;
-- 查看所有表
show tables;
-- 查看以s結(jié)尾的表(盡量不用這種方式查,索引會(huì)失效蛛蒙,效率會(huì)降低)
show tables like '%s';
-- 查看創(chuàng)建語(yǔ)句
show create table student;
show create table student\g -- \g等價(jià)于;
show create table student\G -- 將查到的結(jié)構(gòu)旋轉(zhuǎn)90度變成縱向
-- 查看表結(jié)構(gòu)
desc class;
describe class;
show columns from class;
-- 重命名表:student表—>my_student
rename table student to my_student;
-- 修改表選項(xiàng):字符集
alter table?my_student charset = GBK;
字段操作
-- 給學(xué)生表增加ID糙箍,放到第一個(gè)位置
alter table?my_student add column id int first;
-- 將學(xué)生表中的number 學(xué)號(hào)字段變成固定長(zhǎng)度,且放到第二位(id之后)
alter table my_student modify number char(10) after id;
-- 修改學(xué)生表中的gender字段為sex
alter table my_student change gender sex varchar(10);
-- 注意:如果刪除字段牵祟,這里的所有的都會(huì)清空深夯,不可逆
-- 刪除學(xué)生表中的age年齡字段
alter table?my_student drop age;
表操作
-- 刪除數(shù)據(jù)表
drop table class;
數(shù)據(jù)操作
-- 方案一:插入數(shù)據(jù)
insert into?my_student?values (1, 'bc20200001', 'charry', 'female'), -- female:女生
(2, 'bc20200002', 'marry', 'male'); -- male:男生
-- 方案二:插入數(shù)據(jù):指定字段列表
insert into?my_student (id,number,name,sex) values
(3, 'bc20200003', 'harry', 'female'),
(4, 'bc20200004', 'karry', 'male');
數(shù)據(jù)操作
-- 查看所有數(shù)據(jù)
select * from my_student;
-- 查看指定字段、指定條件的數(shù)據(jù)
select id,number,sex,name from my_student
where id=1;-- 查看滿足id為1的學(xué)生的信息
-- 更新數(shù)據(jù)(如果不寫(xiě)where name='karry'會(huì)導(dǎo)致“普天同慶”课舍,全部改變)
update?my_student set sex='female' where name='karry';
-- 注意:不可逆的塌西,謹(jǐn)慎刪除
-- 刪除數(shù)據(jù)
delete from?my_student where sex='male';
delete from?my_student where name='charry';
MYSQL數(shù)據(jù)類型(列類型)
-- 創(chuàng)建整型表
create table my_int(
int_1 tinyint,
int_2 smallint,
int_3 int,
int_4 bigint
)charset utf8;
-- 插入數(shù)據(jù)
insert into my_int
values(100,100,100,100);? ? -- 有效數(shù)據(jù)
insert into my_int
values('a','b','199','f');? ? -- 無(wú)效數(shù)據(jù):類型限定
insert into my_int
values(255,10000,100000,1000000);? ? -- 錯(cuò)誤:超出范圍
-- 給表增加一個(gè)無(wú)符號(hào)類型
alter table my_int add int_5 tinyint unsigned; -- unsigned:無(wú)符號(hào)類型
-- 插入數(shù)據(jù)
insert into my_int
values(127,10000,100000,1000000,255);
-- 指定顯示寬度為1
alter table my_int add int_6 tinyint(1) unsigned;
-- 插入數(shù)據(jù)
insert into my_int
values(127,0,0,0,255,255);
-- 顯示寬度為2,? ? 0填充(導(dǎo)致數(shù)字自動(dòng)變?yōu)闊o(wú)符號(hào))
alter table my_int add int_7 tinyint(2) zerofill;
-- 插入數(shù)據(jù)
insert into my_int
values(1,1,1,1,1,1,1);
insert into my_int
values(100,100,100,100,100,100,100);
M:代表總長(zhǎng)度? ? ? ? D:代表小數(shù)部分長(zhǎng)度
整數(shù)部分的長(zhǎng)度 = M - D
-- 浮點(diǎn)數(shù)表
create table my_float(
f1 float,
f2 float(10,2), -- 10位在精度范圍之外
f3 float(6,2) -- 6位在精度范圍之內(nèi)
)charset utf8;
-- 插入數(shù)據(jù)
insert into my_float
values(1000.10,1000.10,1000.10);
insert into my_float
values(1234567890,12345678.90,1234.56);
insert into my_float
values(3e38,3.01e7,1234.56);
insert into my_float
values(9999999999,99999999.99,9999.99); -- 后兩個(gè)是最大值
-- 超出長(zhǎng)度插入數(shù)據(jù)
insert into my_float
values(123456,1234.12345678,123.9876543); -- 小數(shù)部分可以超出長(zhǎng)度
insert into my_float
values(123456,1234.12,12345.56); -- 最后一個(gè)整數(shù)部分超出
-- 創(chuàng)建定點(diǎn)數(shù)表
create table my_decimal(
f1 float(10,2),
d1 decimal(10,2)
)charset utf8;
-- 插入數(shù)據(jù)
insert into my_decimal
values(12345678.90,12345678.90); -- 有效數(shù)據(jù)
insert into my_decimal
values(1234.123456,1234.123456); -- 小數(shù)部分可以超出長(zhǎng)度
-- 查看警告
show warnings;
-- 插入數(shù)據(jù)
insert into my_decimal
values(99999999.99,99999999.99); -- 沒(méi)有問(wèn)題
insert into my_decimal
values(99999999.99,99999999.999); -- 進(jìn)位超出范圍