新增數(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ù)庫的修改僅限庫選項(xiàng)
-- 修改數(shù)據(jù)庫informationtest 的字符集
alter database informationtest character GBK;
-- 刪除數(shù)據(jù)庫
drop database informationtest;
表的增刪改差
-- 新增數(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;
--修改表選項(xiàng)隙袁;字符集
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
-- 插入數(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';
-- 新增數(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;