desc:查看表結(jié)構(gòu)
select *:查詢所有數(shù)據(jù)
交互方式
1、客戶端連接認(rèn)證:連接服務(wù)器、認(rèn)證身份(mysql.exe -hPup)?
2、客戶端發(fā)送SQL指令
3、服務(wù)器接收SQL指令,并處理SQL指令,返回操作結(jié)果
4、客戶端接收結(jié)果,并顯示結(jié)果
5、斷開連接
MySQL服務(wù)器內(nèi)部對象分成了四層:
系統(tǒng)(DBMS):管理數(shù)據(jù)庫的
數(shù)據(jù)庫(DB):放數(shù)據(jù)表的
數(shù)據(jù)表(Table):管理字段的
字段(Field) :存放數(shù)據(jù)的
將SQL的基本操作根據(jù)操作對象進(jìn)行分類,可分為三類: (庫操作、表操作(包含字段操作) 、數(shù)據(jù)操作?)
庫操作?
新增數(shù)據(jù)庫:
? ? ? ? 1、create database 數(shù)據(jù)庫名字 [庫選項(xiàng)];
2、庫選項(xiàng):用來約束數(shù)據(jù)庫,分為兩個選項(xiàng):
1)字符集設(shè)定:charset/character set 具體字符集(數(shù)據(jù)存儲的編碼格式,常用的有:GBK和UTF8)
2)校對集設(shè)定:collate 具體校對集(數(shù)據(jù)比較的規(guī)則)
? ? ? ? 3、數(shù)據(jù)庫名字不能用關(guān)鍵字(已經(jīng)被系統(tǒng)使用的字符)或者保留字(將來系統(tǒng)可能會用到的字符)
創(chuàng)建結(jié)果:
????????在數(shù)據(jù)庫系統(tǒng)中,增加了對應(yīng)的數(shù)據(jù)庫信息
會在保存數(shù)據(jù)的文件夾下(Data目錄),創(chuàng)建一個對應(yīng)數(shù)據(jù)庫名字的文件夾
????????每個數(shù)據(jù)庫下都有一個opt文件,保存了庫選項(xiàng)

表操作(包含字段操作)?
數(shù)據(jù)操作
注釋
--? 雙中劃線+空格:注釋(單行注釋),也可以使用#號
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ù)庫
show databases;
斷開連接
exit、quit、\q
--? 雙中劃線+空格:注釋(單行注釋),也可以使用#號
-- 創(chuàng)建數(shù)據(jù)庫
create database mydb charset utf8;
表示該數(shù)據(jù)庫已存在,要想繼續(xù)可另起名字或drop database `mydb`;

表示已刪除

-- 創(chuàng)建關(guān)鍵字?jǐn)?shù)據(jù)庫
create database database charset utf8;-- 報(bào)錯
-- 使用反引號(非要使用關(guān)鍵字命名的數(shù)據(jù)庫加反引號)
create database `database` charset utf8;
--創(chuàng)建中文數(shù)據(jù)庫
create database 北京 charset utf8;
-- 如果報(bào)錯解決方案:告訴服務(wù)器當(dāng)前中文的字符集是什么
右鍵命令行窗口——>屬性——>選項(xiàng)——>當(dāng)前代碼頁(簡體中文GBK)
先執(zhí)行:set names gbk;
再執(zhí)行:create database?北京 charset utf8;
查看數(shù)據(jù)庫
? ? ? ?1、查看所有數(shù)據(jù)庫:show databases;
? ? ? ? 2、查看指定部分的數(shù)據(jù)庫(模糊查詢):show databases like 'pattern';
? ? ? ? ? ? ? ?1) pattern是匹配模式
? ? ? ? ? ? ? ? 2)%表示匹配多個字符
? ? ? ? ? ? ? ? 3)_表示匹配單個字符
3、查看數(shù)據(jù)庫的創(chuàng)建語句:show create database 數(shù)據(jù)庫名字;
-- 查看所有數(shù)據(jù)庫
show databases;
-- 創(chuàng)建數(shù)據(jù)庫
create database informationtest charset utf8;
-- 以informationtion_開始的數(shù)據(jù)庫(_需要被轉(zhuǎn)義)
show databases like 'information_%';-- 相當(dāng)于information%
show databases like 'information\_%';

-- 查看數(shù)據(jù)庫的創(chuàng)建語句
show create database mybd;
show create database?`database`; -- 關(guān)鍵字需要使用反引號
更新數(shù)據(jù)庫
????????????數(shù)據(jù)庫名字不可以修改
????????????數(shù)據(jù)庫的修改僅限庫選項(xiàng),即字符集和校對集(校對集依賴字符集)
????????????alter database 數(shù)據(jù)庫名字 [庫選項(xiàng)];
????????????charset/character set [=] 字符集
????????????collate 校對集
刪除數(shù)據(jù)庫
drop database 數(shù)據(jù)庫名字;
-- 修改數(shù)據(jù)庫informationtest的字符集
alter database informationtest charset GBK;
注意:刪除數(shù)據(jù)庫有風(fēng)險(xiǎn),需備份
-- 刪除數(shù)據(jù)庫:(drop database 數(shù)據(jù)庫名字;)
drop database informationtest;
表操作

--創(chuàng)建表
如何解決沒有數(shù)據(jù)庫選擇?

方案一:
顯式地指定表所屬的數(shù)據(jù)庫:
create table 數(shù)據(jù)庫名.表名();
create table if not exists mydb.student(-- 顯示地將student表放到mybd數(shù)據(jù)庫下
name varchar(10),
gender varchar(10),
number varchar(10),
age int
)charset utf8;
方案二:
隱式地指定表所屬數(shù)據(jù)庫
進(jìn)入數(shù)據(jù)庫環(huán)境:use 數(shù)據(jù)庫名字;
--創(chuàng)建數(shù)據(jù)庫表
-- 進(jìn)入數(shù)據(jù)庫
use mydb;
-- 創(chuàng)建表
create table class(
name varchar(10),
room varchar(10)
)charset utf8;

-- 查看所有表
show tables;
-- 查看以s結(jié)尾的表(盡量不用這種方式查,索引會失效,效率會降低)
show tables like '%s';
-- 查看創(chuàng)建語句
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,放到第一個位置
alter table?my_student add column id int first;
-- 將學(xué)生表中的number 學(xué)號字段變成固定長度,且放到第二位(id之后)
alter table my_student modify number char(10) after id;
-- 修改學(xué)生表中的gender字段為sex
alter table my_student change gender sex varchar(10);
注意:如果刪除字段,這里的所有的都會清空,不可逆
-- 刪除學(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ù)(如果不寫where name='karry'會導(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');? ? -- 無效數(shù)據(jù):類型限定
insert into my_int
values(255,10000,100000,1000000);? ? -- 錯誤:超出范圍
-- 給表增加一個無符號類型
alter table my_int add int_5 tinyint unsigned; -- unsigned:無符號類型
-- 插入數(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ù)字自動變?yōu)闊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:代表總長度D:代表小數(shù)部分長度
整數(shù)部分的長度 = 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); -- 后兩個是最大值
-- 超出長度插入數(shù)據(jù)
insert into my_float
values(123456,1234.12345678,123.9876543); -- 小數(shù)部分可以超出長度
insert into my_float
values(123456,1234.12,12345.56); -- 最后一個整數(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ù)部分可以超出長度
-- 查看警告
show warnings;
-- 插入數(shù)據(jù)
insert into my_decimal
values(99999999.99,99999999.99); -- 沒有問題
insert into my_decimal
values(99999999.99,99999999.999); -- 進(jìn)位超出范圍