MySQL(數(shù)據(jù)庫)基本操作

MySQL(數(shù)據(jù)庫)基本操作

新增數(shù)據(jù)庫

數(shù)據(jù)庫名字以字母數(shù)字下劃線組成,不能以數(shù)字開頭

數(shù)據(jù)庫名字不能用關(guān)鍵字(已經(jīng)被系統(tǒng)使用的字符)或者保留字(將來系統(tǒng)可能會(huì)用到的字符)

語法格式

--雙中劃線+空格(單行注釋),也可以使用#號

數(shù)據(jù)的增刪改查

--創(chuàng)建數(shù)據(jù)庫

create database? mydb? ?charset? utf8;? #創(chuàng)建名為mydb的數(shù)據(jù)庫

--創(chuàng)建關(guān)鍵字?jǐn)?shù)據(jù)庫

create database? ?database ?charset? utf8;--報(bào)錯(cuò)

-- 使用反引號(`? `)可以用關(guān)鍵字命名

create database? ?`database` charset? utf8;

--創(chuàng)建中文數(shù)據(jù)庫

create database? 唐山? charset? utf8;

--如果報(bào)錯(cuò)解決方案;告訴服務(wù)器當(dāng)前中文的字符集是什么

set names gbk;

在執(zhí)行? ?create database? 唐山? charset? utf8;

--查看所有數(shù)據(jù)庫

show databases;?

--創(chuàng)建數(shù)據(jù)庫

create database? informationtest? charset? utf8;

-- 查看指定部分的數(shù)據(jù)庫

--查看一informationtest_開始的數(shù)據(jù)庫(_需要被轉(zhuǎn)義,%匹配多個(gè)字符集? _匹配單個(gè)字符集)

show databases like ' information_% ';? --相當(dāng)于informationt%

show databases like ' information\_% ';(_需要被轉(zhuǎn)義)

-- 查看數(shù)據(jù)庫的創(chuàng)建語句

show create database mydb;

show create database `database`;-- 關(guān)鍵字需要使用反引號

-- 數(shù)據(jù)庫的修改 數(shù)據(jù)庫名字不可以修改 數(shù)據(jù)庫的修改僅限庫選項(xiàng)

-- 修改數(shù)據(jù)庫informationtest 的字符集

alter database informationtest charset GBK;

-- 刪除數(shù)據(jù)庫

drop database informationtest; (一次只能刪一個(gè))

表的增刪改查

-- 新增數(shù)據(jù)表?

create table [if not exists] 表名(

字段名字 數(shù)據(jù)類型,

……

字段名字 數(shù)據(jù)類型

) [表選項(xiàng)];(中括號里的可寫可不寫)

create table if not exists mydb.student(

-- 顯示地將student表放到mydb數(shù)據(jù)庫下

name varchar(10),

gender varchar(10),

number varchar(10),

age int

)charset utf8;

-- 創(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? -- \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);字符集

alert table my_atudent charset = GBK;

--給學(xué)生表增加ID,放到第一個(gè)位置

alert table my_student

?add column id int??

first;? #以分號;定位位置

--將學(xué)生表中的number學(xué)號字段變成固定長度,且放倒第二位(id)之后

alert table my_student modify number char(10) after id;

--修改學(xué)生表中的gender字段為sex

alert?table my_student? change? gender sex vachar(10)

--刪除學(xué)生表中的age年齡字段

alert table my_student drop age;


-- 刪除數(shù)據(jù)表

drop table class(表可以一次刪多個(gè),刪完不能恢復(fù),要備份)

-- 插入數(shù)據(jù)

insert into my_student

value(1,'bc20200001','Jim','male'),

(2,'bc20200002','Lily','female');

--? 插入數(shù)據(jù):指定的字段列表

insert into my_student(number,sex,name,id) values

('bc20200003','male','syh',3),

('bc20200004','female','zyn',4);

--查看所有數(shù)據(jù)

select * from my_student;

-- 查看指定字段、指定條件的數(shù)據(jù)

select id,number,sex,name from my_student

where id=1; -- 查看滿足id為1的學(xué)生信息

-- 更新數(shù)據(jù)

update my_student set sex='female' where name='Jim';

-- 刪除數(shù)據(jù)

delete from my_student where sex='male';

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

友情鏈接更多精彩內(nèi)容