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

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

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

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

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

語法格式

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

數(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;--報錯

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

create database? ?`database` charset? utf8;

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

create database? 唐山? charset? utf8;

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

set names gbk;

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

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

show databases;?

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

create database? informationtest? charset? utf8;

--查看一informationtest_開始的數(shù)據(jù)庫(_需要被轉(zhuǎn)義)

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

show databases like ' information\_% ';

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

show create database mydb;

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

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

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

alter database informationtest character GBK;

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

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

表的增刪改查

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

create table [if not exists] 表名(

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

……

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

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

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ù)庫表

-- 先進(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 等價于 ;

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(新的);

--修改表選項;字符集

alert table my_atudent charset = GBK;

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

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(表可以一次刪多個,刪完不能恢復(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輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 新增數(shù)據(jù)庫 數(shù)據(jù)庫名字以字母數(shù)字下劃線組成,不能以數(shù)字開頭 數(shù)據(jù)庫名字不能用關(guān)鍵字(已經(jīng)被系統(tǒng)使用的字符)或者保留...
    發(fā)報員有問題私小鵬閱讀 241評論 0 0
  • MySQL(數(shù)據(jù)庫)基本操作 新增數(shù)據(jù)庫 數(shù)據(jù)庫名字以字母數(shù)字下劃線組成,不能以數(shù)字開頭 數(shù)據(jù)庫名字不能用關(guān)鍵字(...
    Ancestor楠閱讀 318評論 0 1
  • 史上最簡單的 MySQL 教程>>>MySQL運(yùn)行機(jī)制原理&架構(gòu)>>>觸發(fā)器視圖(上)視圖(下)數(shù)據(jù)備份與還原(上...
    月亮是我踢彎得閱讀 356評論 0 1
  • MySQL數(shù)據(jù)類型: 整數(shù)型:存放整型數(shù)據(jù) tinyint:迷你整型,使用1個字節(jié)存儲,表示的狀態(tài)最多為256種(...
    wyc111閱讀 3,383評論 0 0
  • 計算機(jī)誕生后,數(shù)據(jù)開始在計算機(jī)中存儲并計算,并設(shè)計出了數(shù)據(jù)庫系統(tǒng),數(shù)據(jù)庫系統(tǒng)解決的問題:持久化存儲,優(yōu)化讀寫,保證...
    LittlePy閱讀 846評論 0 0

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