mysql数据库且字 语句是什么,MySQL(数据库)基本操作
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)鍵字數(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;
-- 查看指定部分的數(shù)據(jù)庫
--查看一informationtest_開始的數(shù)據(jù)庫(_需要被轉(zhuǎn)義,%匹配多個字符集? _匹配單個字符集)
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ù)庫的修改僅限庫選項
-- 修改數(shù)據(jù)庫informationtest 的字符集
alter database informationtest charset GBK;
-- 刪除數(shù)據(jù)庫
drop database informationtest; (一次只能刪一個)
表的增刪改查
-- 新增數(shù)據(jù)表create table [if not exists] 表名(
字段名字 數(shù)據(jù)類型,
……
字段名字 數(shù)據(jù)類型
) [表選項];(中括號里的可寫可不寫)
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ù)庫表
-- 先進入數(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';
總結(jié)
以上是生活随笔為你收集整理的mysql数据库且字 语句是什么,MySQL(数据库)基本操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微脉java面试,微脉医疗开放平台
- 下一篇: sql server配置连接oracle