# *****59_連接認(rèn)證_創(chuàng)建數(shù)據(jù)庫*****
"""
-- 查看所有數(shù)據(jù)庫
show databases;
-- 退出命令
exit傲霸、quit疆瑰、\q
-- 創(chuàng)建名為xxx的數(shù)據(jù)庫 【mydatabase:數(shù)據(jù)庫名字】
create database mydatabase charset utf8;
-- 創(chuàng)建名為xxx的數(shù)據(jù)庫
create database informationtest charset utf8;
-- 創(chuàng)建關(guān)鍵字的數(shù)據(jù)庫
create database `database` charset utf8;
-- 創(chuàng)建中文名的數(shù)據(jù)庫
create database 中國 charset utf8;
-- 告訴服務(wù)器當(dāng)前中文的字符集是什么
set names gbk;
-- 查看以information_開始的數(shù)據(jù)庫(_需要被轉(zhuǎn)義)
show databases like 'information\_%';
-- 相當(dāng)于information%
show databases like 'information_%';
-- 查看數(shù)據(jù)庫的創(chuàng)建語句
show create database mydatabase;
show create database `database`;
"""
# *****60_修改_刪除數(shù)據(jù)庫*****
"""
-- 修改數(shù)據(jù)庫informationtest的字符集
alter database informationtest charset GBK;
-- 刪除數(shù)據(jù)庫
drop database informationtest;
!j甲摹乃摹!重要的數(shù)據(jù)庫,在刪除之前一定要備份好!8啤孵睬!
"""
# *****61_創(chuàng)建_查看表*****
"""
-- 創(chuàng)建一個名為student的表 【varchar(10): 10個字符串類型的xxx】
-顯示地將student表放到mydatabase數(shù)據(jù)庫里
create table if not exists mydatabase.student(
name varchar(10),
gender varchar(10),
number varchar(10),
age int
)charset utf8;
-- 創(chuàng)建數(shù)據(jù)表
-進(jìn)入數(shù)據(jù)庫
use mydatabase;
-創(chuàng)一個名為class的建表
create table class(
name varchar(10),
room varchar(10)
)charset utf8;
-- 查看所有表
show tables;
-- 查看以s結(jié)尾的表
show tables like '%s';
show tables like "%s";
-- 查看一個名為student的表創(chuàng)建語句
show create table student;
show create table student\g
-- 將查找到的結(jié)構(gòu)旋轉(zhuǎn)90度變成縱向
show create table student\G
-- 查看表結(jié)構(gòu)(表中的字段信息)
desc class;
describe class;
show columns from class;
"""
# *****62_修改_刪除表*****
"""
-- 重命名表(student表 -> my_student)
rename table student to my_student;
-- 修改表選項(字符集、校對集伶跷、存儲引擎都可以修改)
alter table my_student charset= GBK;
-- 給學(xué)生表增加ID, 放到第一個位置
alter table my_student
add column id int
first;
-- 將學(xué)生表中的number學(xué)號字段變成固定長度,且放到第二位(id之后)
//char:固定長度//
alter table my_student modify number char(10) after id;
-- 修改學(xué)生表中的gender字段為sex
alter table my_student change gender sex varchar(10);
-- 刪除字段
alter table my_student drop age;
-- 刪除數(shù)據(jù)表
drop table class;
"""
# *****63_數(shù)據(jù)的增刪改查*****
"""
-- 插入數(shù)據(jù)
insert into my_student values
(1,'bc20190001','Jim','male'),
(2,'bc20190001','Lily','female');
-- 插入數(shù)據(jù): 指定字段列表
insert into my_student(number,sex,name,id) values
('bc20190003','male','Tom',3),
('bc20190004','female','Lucy',4);
-- 查看所有數(shù)據(jù)
select * from my_student;
-- 查看指定字段掰读、指定條件的數(shù)據(jù)
-查看滿足id為1的學(xué)生信息
select id,number,sex,name from my_student where id=1;
-- 更新數(shù)據(jù)
!!!建議都有where, 否則就是更新全部!!!
update my_student set sex='female' where name='Jim';
-- 刪除數(shù)據(jù)
!!!刪除是不可逆的, 謹(jǐn)慎刪除!!!
delete from my_student where sex='male';
"""