1、連接認證基本語法
mysql.exe -hlocalhost -p3306 -uroot -proot
mysql -uroot -p
Enter password:****(root)
2绵估、退出
建議方式:使用sql提供的指令
Exit隆敢;//exit帶分號
\q //quit縮寫
Quit
3晴竞、創(chuàng)建數(shù)據(jù)庫
基本語法:create database 數(shù)據(jù)庫名字[庫選項]养叛;
庫選項:數(shù)據(jù)庫的相關(guān)屬性
字符集:charset字符集种呐,代表著當前數(shù)據(jù)庫下的所有表儲存的數(shù)據(jù)默認指定的字符集(如果當前不指定,那么采用DBMS默認的)
校對集:collate
Create database 數(shù)據(jù)庫名字 charset 字符集名稱弃甥;
create database mydatabase charset gbk;
4爽室、顯示所有數(shù)據(jù)庫
基本語法:showdatabases like '匹配模式';
_:匹配當前位置單個字符
%:匹配指定位置多個字符
獲取以my開頭的全部數(shù)據(jù)庫:'my%';
獲取m開頭淆攻,后面第一個字母不確定阔墩,最后為database的數(shù)據(jù)庫:'m_database';
獲取以database結(jié)尾的數(shù)據(jù)庫:'%database';
顯示所有數(shù)據(jù)庫
show databases;
查看以my開頭的數(shù)據(jù)庫
show databases like 'my%';
查看數(shù)據(jù)庫創(chuàng)建語句
show create database mydatabase;
5、修改數(shù)據(jù)庫
修改數(shù)據(jù)庫字符集(庫選項):字符集和校對集
基本語法:alter database 數(shù)據(jù)庫名字 charset 字符集;
alter database mydatabase charset gbk;
是否可以修改數(shù)據(jù)庫名字瓶珊?mysql5.5之前是可以修改的啸箫,rename命令,但是5.5之后就不可以了
6伞芹、刪除數(shù)據(jù)庫
基本語法:drop database 數(shù)據(jù)庫名稱
drop database mydatabase;
刪除雖簡單筐高,但是切記要做好安全操作,確保里面數(shù)據(jù)沒有問題了
刪除數(shù)據(jù)庫之后:對應(yīng)儲存數(shù)據(jù)的文件夾也會被刪除(opt文件也會被刪除)
7丑瞧、創(chuàng)建數(shù)據(jù)表
基本語法:create table 表名 (字段名 字段類型 [字段屬性],字段名 字段類型 [字段屬性]...)[表選項]
創(chuàng)建數(shù)據(jù)表
create table class(name varchar(10));
以上錯誤表明蜀肘,表必須放在對應(yīng)的數(shù)據(jù)庫下绊汹,有倆種方式可以將表掛入到指定的數(shù)據(jù)庫下
1、在數(shù)據(jù)表名字前面加上數(shù)據(jù)庫名字扮宠,用"."連接即可:數(shù)據(jù)庫.數(shù)據(jù)表
將數(shù)據(jù)表掛到數(shù)據(jù)庫下
create table mydatabase2. class(name varchar(10));
2西乖、在創(chuàng)建數(shù)據(jù)表之前先進入到某個具體的數(shù)據(jù)庫即可:use 數(shù)據(jù)庫名字
進入數(shù)據(jù)庫,創(chuàng)建表
use mydatabase2;
create table mydatabase2. class(name varchar(10));
表選項:與數(shù)據(jù)庫選項類似
Engine:存儲引擎坛增,mysql提供的具體存儲數(shù)據(jù)的方式获雕, 默認有一個innodb(5.5以前默認的是myisam)
Charset:字符集,只對當前自己表有效(級別比數(shù)據(jù)庫高)
Collate:校對集
使用表選項
create table student(name varchar(10))charset utf8;
8收捣、復(fù)制已有表結(jié)構(gòu)
從已經(jīng)存在的表復(fù)制一份(只復(fù)制結(jié)構(gòu)届案,如果表中有數(shù)據(jù)不復(fù)制)
基本語法:create table 新表名 like 表名;//只要使用數(shù)據(jù)庫.表名罢艾,就可以在任何數(shù)據(jù)庫下訪問其他 數(shù)據(jù)庫的表名
在test數(shù)據(jù)庫下創(chuàng)建一個與teacher一樣的表
use test;
create table teacher like mydatabase2.teacher;
9楣颠、查看所有表
查看所有表
show tables;
10尽纽、匹配顯示表
基本語法:show tables like '匹配模式';
查看匹配數(shù)據(jù)表
show tables like 'c%';
11童漩、顯示表結(jié)構(gòu)
本質(zhì)含義:顯示表中所包含的字段信息(名字弄贿,類型,屬性等)
Describe 表名
Desc 表名
show columns from 表名
顯示表結(jié)構(gòu)
describe class;
desc teacher;
show columns from student;
12矫膨、顯示表創(chuàng)建語句
查看數(shù)據(jù)表創(chuàng)建時的語句:此語句看到的結(jié)果已經(jīng)不是用戶之前輸入的
基本語法:show create table 表名差凹;
show create table student;
mysql中有多種語句結(jié)束符
與\g所表示的效果是一樣的,都是字段在上橫排著侧馅,下面跟對應(yīng)的數(shù)據(jù)危尿,\G 字段在左側(cè)豎著,數(shù)據(jù)在右側(cè)豎著
show create table student\G
13施禾、設(shè)置表屬性
表屬性指的就是表選項:engine脚线,charest和collate
基本語法:alter table 表名 表選項 [=] 值
alter table student charset gbk;
注:如果數(shù)據(jù)庫已經(jīng)確定了,里面有很多數(shù)據(jù)了弥搞,不要輕易修改表選項()字符集影響不大
修改表結(jié)構(gòu)
14邮绿、修改表名
基本語法:rename table 舊表名 to 新表名
數(shù)據(jù)庫中數(shù)據(jù)表通常有前綴,取數(shù)據(jù)庫的前倆個字母加上下劃線
rename table student to my_student;
15攀例、修改表選項
基本語法:alter table 表名 表選項 [=] 新值
alter table student charset gbk;
16船逮、新增字段
基本語法:alter table 表名 add [column] 新字段名 列類型 [列屬性] [位置first/after字段名]
給學(xué)生表添加一個age字段
alter table my_student add column age int;
字段位置:字段想要存放的位置
First:在某某之前(最前面),第一個 字段
After:字段名:放在某個具體的字段之后(默認的)
添加字段:放在第一個字段
alter table my_student add id int first
17粤铭、修改字段名
基本語法:alter table 表名 change 舊字段名 新字段名 字段類型 [列屬性] [新位置]
修改字段名
alter table my_student change age nj int;
18挖胃、修改字段類型(屬性)
基本語法:alter table 表名 modify 字段名 新類型 [新屬性] [新位置]
修改字段類型
alter table my_student modify name varchar(20);
19、刪除字段
基本語法:alter table 表名 drop 字段名
刪除字段
alter table my_student drop nj;
刪除表結(jié)構(gòu)
基本語法:drop table 表名梆惯,[表名2....]可以同時刪除多個數(shù)據(jù)表
刪除表名
drop table class;
批量刪除表名
drop table teacher,my_student;
一酱鸭、數(shù)據(jù)基礎(chǔ)操作-插入操作
本質(zhì)含義:將數(shù)據(jù)以sql的形式存儲到指定的數(shù)據(jù)表(字段)里面
基本語法:Insert into 表名 [(字段列表)] value(對應(yīng)字段列表),向表中所有字段插入數(shù)據(jù)
insert into my_student (name,age) values("jake",30);
注:后面(values中)對應(yīng)的值列表只需要與前面的字段列表相對應(yīng)即可(不一定與表結(jié)構(gòu)完全一致)
insert into my_student (age,name) values(49,"tom");
注:字段列表并不一定非要有所有的表中字段
insert into my_student (name) values("han");
基本語法:向表中所有字段插入數(shù)據(jù) //值列表必須與字段列表一致
Insert into 表名 value (對應(yīng)表結(jié)構(gòu))
insert into my_student values("lilei",28);
二垛吗、數(shù)據(jù)基礎(chǔ)操作-查詢操作
1凹髓、查詢表中全部數(shù)據(jù)
基本語法:select * from 表名;//表示匹配所有的字段*
獲取所有數(shù)據(jù)
select * from my_teacher怯屉;
2蔚舀、查詢表中部分字段
基本語法:select 字段列表 from 表名 //字段列表使用“,”隔開
獲取指定字段
select name from my_teacher;
3锨络、簡單條件查詢數(shù)據(jù)
基本語法:select 字段列表 / from 表名 where 字段名 = 值 //mysql中沒有==符號*
獲取年齡為30歲人的名字
select name from my_teacher where age = 30;
三赌躺、數(shù)據(jù)基礎(chǔ)操作-刪除操作
基本語法:delect from 表名 [where 條件];//如果沒有where條件羡儿,意味著系統(tǒng)會自動刪除該表所有數(shù)據(jù)(慎用)
刪除年齡為30歲的老師
delete from my_teacher where age = 30;
四礼患、數(shù)據(jù)基礎(chǔ)操作-更新操作
更新:將數(shù)據(jù)進行修改(通常是修改部分字段數(shù)據(jù))
基本語法:updata 表名 set 字段名 = 新值 [where 條件];如果沒有where條件,那么所有的表中對應(yīng)的那個字段都會被修改成統(tǒng)一值
更新年齡Han
updata my_teacher set age = 28 where name = 'Han';