desc:查看表結構
select *:查詢所有數(shù)據(jù)
交互方式
1、客戶端連接認證:連接服務器泡垃、認證身份(mysql.exe -hPup)?
2析珊、客戶端發(fā)送SQL指令
3将谊、服務器接收SQL指令察蹲,并處理SQL指令屁置,返回操作結果
4狮杨、客戶端接收結果,并顯示結果
5逸邦、斷開連接
MySQL服務器內(nèi)部對象分成了四層:
系統(tǒng)(DBMS):管理數(shù)據(jù)庫的
數(shù)據(jù)庫(DB):放數(shù)據(jù)表的
數(shù)據(jù)表(Table):管理字段的
字段(Field) :存放數(shù)據(jù)的
將SQL的基本操作根據(jù)操作對象進行分類渣窜,可分為三類: (庫操作器一、表操作(包含字段操作) 捐腿、數(shù)據(jù)操作?)
庫操作?
新增數(shù)據(jù)庫:
? ? ? ? 1纵朋、create database 數(shù)據(jù)庫名字 [庫選項];
2、庫選項:用來約束數(shù)據(jù)庫茄袖,分為兩個選項:
1)字符集設定:charset/character set 具體字符集(數(shù)據(jù)存儲的編碼格式操软,常用的有:GBK和UTF8)
2)校對集設定:collate 具體校對集(數(shù)據(jù)比較的規(guī)則)
? ? ? ? 3、數(shù)據(jù)庫名字不能用關鍵字(已經(jīng)被系統(tǒng)使用的字符)或者保留字(將來系統(tǒng)可能會用到的字符)
創(chuàng)建結果:
????????在數(shù)據(jù)庫系統(tǒng)中宪祥,增加了對應的數(shù)據(jù)庫信息
會在保存數(shù)據(jù)的文件夾下(Data目錄)聂薪,創(chuàng)建一個對應數(shù)據(jù)庫名字的文件夾
????????每個數(shù)據(jù)庫下都有一個opt文件,保存了庫選項
表操作(包含字段操作)?
數(shù)據(jù)操作
注釋
--? 雙中劃線+空格:注釋(單行注釋)蝗羊,也可以使用#號
mysql.exe -h localhost -P 3306 -u root -p
-h:找到主機地址在哪
localhost:主機地址?
-P:端口(統(tǒng)一默認3306)
-u:用戶名(默認root)
-p:密碼
客戶端鏈接認證
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)建關鍵字數(shù)據(jù)庫
create database database charset utf8;-- 報錯
-- 使用反引號(非要使用關鍵字命名的數(shù)據(jù)庫加反引號)
create database `database` charset utf8;
--創(chuàng)建中文數(shù)據(jù)庫
create database 北京 charset utf8;
-- 如果報錯解決方案:告訴服務器當前中文的字符集是什么
右鍵命令行窗口——>屬性——>選項——>當前代碼頁(簡體中文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ù)庫(_需要被轉義)
show databases like 'information_%';-- 相當于information%
show databases like 'information\_%';
-- 查看數(shù)據(jù)庫的創(chuàng)建語句
show create database mybd;
show create database?`database`; -- 關鍵字需要使用反引號
更新數(shù)據(jù)庫
????????????數(shù)據(jù)庫名字不可以修改
????????????數(shù)據(jù)庫的修改僅限庫選項复罐,即字符集和校對集(校對集依賴字符集)
????????????alter database 數(shù)據(jù)庫名字 [庫選項];
????????????charset/character set [=] 字符集
????????????collate 校對集
刪除數(shù)據(jù)庫
drop database 數(shù)據(jù)庫名字;
-- 修改數(shù)據(jù)庫informationtest的字符集
alter database informationtest charset GBK;
注意:刪除數(shù)據(jù)庫有風險涝登,需備份
-- 刪除數(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ù)庫
進入數(shù)據(jù)庫環(huán)境:use 數(shù)據(jù)庫名字;
--創(chuàng)建數(shù)據(jù)庫表
-- 進入數(shù)據(jù)庫
use mydb;
-- 創(chuàng)建表
create table class(
name varchar(10),
room varchar(10)
)charset utf8;
-- 查看所有表
show tables;
-- 查看以s結尾的表(盡量不用這種方式查效诅,索引會失效胀滚,效率會降低)
show tables like '%s';
-- 查看創(chuàng)建語句
show create table student;
show create table student\g -- \g等價于;
show create table student\G -- 將查到的結構旋轉90度變成縱向
-- 查看表結構
desc class;
describe class;
show columns from class;
-- 重命名表:student表—>my_student
rename table student to my_student;
-- 修改表選項:字符集
alter table?my_student charset = GBK;
字段操作
-- 給學生表增加ID,放到第一個位置
alter table?my_student add column id int first;
-- 將學生表中的number 學號字段變成固定長度乱投,且放到第二位(id之后)
alter table my_student modify number char(10) after id;
-- 修改學生表中的gender字段為sex
alter table my_student change gender sex varchar(10);
注意:如果刪除字段咽笼,這里的所有的都會清空,不可逆
-- 刪除學生表中的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的學生的信息
-- 更新數(shù)據(jù)(如果不寫where name='karry'會導致“普天同慶”剑刑,全部改變)
update?my_student set sex='female' where name='karry';
注意:不可逆的,謹慎刪除
-- 刪除數(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填充(導致數(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
-- 浮點數(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)建定點數(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); -- 進位超出范圍