鏈接認證
mysql.exe -h localhost -p 3306 d-u root -p
查看所有數(shù)據(jù)庫
show databases;
斷開連接
exit
quit
\q
雙中劃線+空格:注釋(單行注釋),也可以使用#號
創(chuàng)建數(shù)據(jù)庫
create database mydb charset utf8;
創(chuàng)建關鍵字數(shù)據(jù)庫
create database database charset utf8;? --報錯
使用反引號
create database 'database' charset utf8;
創(chuàng)建中文數(shù)據(jù)庫
create database 北京 charset utf8;
如果報錯解決方案:告訴服務器當前中文的字符集是什么
set name gbk;
create database 北京 charset utf8;
查看所有數(shù)據(jù)庫
show databases;
創(chuàng)建數(shù)據(jù)庫
create database informationtest charset utf8;
查看以information_開始的數(shù)據(jù)庫(_需要被轉義)
show databases like 'information_%';--相當于information_%
show databases like 'information\_%';
查看數(shù)據(jù)庫的創(chuàng)建語句
show create database mydb;
show create database 'database';--關鍵字需要使用反引號
修改數(shù)據(jù)庫informationteest的字符集
alter database informationteest;
刪除數(shù)據(jù)庫
drop database informationteest;
創(chuàng)建表
create table if not exists student(--顯示地講student表放到mydb數(shù)據(jù)庫下
name varchar(10),
gender varchar(10),
number varchar(10),
age int
)charset utf8;
創(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_stuber 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ù)
insert into my_student
valuse(1,'bc20200001','Jim','male'),
(2,'bc20200002','Lily','female');
插入數(shù)據(jù):指定字段列表
insert into my_student(number,sex,name,id) values
('bc20200003','male','Tom',3)
('bc20200004','female','Lucy',4)
查看所有數(shù)據(jù)
select * from my_student;
查看指定字段、指定條件的數(shù)據(jù)
select id,number,sex,name from my_student
where id=1;--查看滿足id為1的學生的信息
更新數(shù)據(jù)
update my_student set sex='female' where name='Jim';
刪除數(shù)據(jù)
delete from my_student where sex='male';
創(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
valuse(255,10000,100000,1000000);-- 錯誤:超出范圍
給表增加一個屋符號類型
alter table my_int add int_5 tinyint unsigend;? -- 無符號類型
插入數(shù)據(jù)
insert into my_int
values(127,0,0,0,255,255);
顯示寬度為2,0填充
alter table my_int add int_7 tinyint(2) zerofill;
插入數(shù)據(jù)
insret into my_int
values(1,1,1,1,1,1,1,1,11,);
insert into my_int
values(100,100,100,100,100,100);
浮點數(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);
insert into my_float
values(123456789,12345678.90,1234.22);--無效數(shù)據(jù):類型限定
insert into my_float
valuse(3e38,3.0le7,1234.567);
insert into my_float
valuse(99999999999,999999999999.99,999.99);--后倆個死最大值
超出長度插入數(shù)據(jù)
insert into my_float
values(123456,1234.1123456789,123.9876543);--最后一個整數(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,123456678.90); -- 有效數(shù)據(jù)
insert into my_decimal
values(1234.123456,1234.123456);-- 小數(shù)部分可以超出長度
查看警告
show warnings;
插入數(shù)據(jù)
iunsert into my_decimal
values(9999999.99,9999999.99);--沒有問題
insert into my_decimal
values(999999999.99,99999999.999);-- 進位超出范圍