-- 連接認證
mysql.exe -h localhost -P 3306 -u root -p
--? 查看所有數(shù)據(jù)庫
show databases;
--? 斷開連接
exit
quit
\q
--? 雙中劃線+空格:注釋(單行注釋),也可以使用#號
--? 創(chuàng)建數(shù)據(jù)庫
create database medb 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 names gbk;
create database 北京 charset utf8;
--? 查看所有數(shù)據(jù)庫
show databases;
--? 創(chuàng)建數(shù)據(jù)庫
create database informationtest charset utf8;
--? 查看以information_開始的數(shù)據(jù)庫(_需要被轉(zhuǎn)義)
show databases like 'information_%'; --? 相當于information%
show databases like 'information\_%';
-- 查看數(shù)據(jù)庫的創(chuàng)建語句
show create database mydb;
show create database 'database'; --? 關鍵字需要使用反引號
--? 修改數(shù)據(jù)庫informationtest的字符集
alter database informationtest charset GBK;
-- 刪除數(shù)據(jù)庫
drop database informationtest;
-- 創(chuàng)建表
create table if ont exists mydb.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 -- 將查到的結構旋轉(zhuǎn)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ù)
insert into my_student
values(1,'ba20200001','Jim','male'),
(2,'ba20200002','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
values(255,10000,100000,1000000);? --? 錯誤:超出范圍
--? 給表增加一個無符號類型
alter table my_int add int_5 tinyint 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填充
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);