mysql> 庫(kù)(最高層)>>表(中層,存在于庫(kù)中)>>字段(下層关筒,存在于表中)>>data
庫(kù):
查看數(shù)據(jù)庫(kù):show databases;
創(chuàng)建數(shù)據(jù)庫(kù):create database 庫(kù)名;
create database 庫(kù)名 character set = utf8 collate = utf8_bin; #指定字符集
create database if not exists 庫(kù)名 character set = utf8 collate = utf8_bin; #容錯(cuò)機(jī)制
刷新數(shù)據(jù)庫(kù)及表的列表內(nèi)容:flush privileges;
刪除數(shù)據(jù)庫(kù):drop database 庫(kù)名;
drop database if exists 庫(kù)名; #容錯(cuò)機(jī)制
更改數(shù)據(jù)庫(kù)名:null
表:
查看表:show tables; (查看前進(jìn)入庫(kù):use 庫(kù)名;)
創(chuàng)建表:create table if not exists 表名 (
字段名 int not null primary key, #創(chuàng)建主鍵時(shí)只能設(shè)置一個(gè)主鍵, 不能有多個(gè)
字段名 varchar(80) not null, #設(shè)置主鍵,目的是為不重復(fù)創(chuàng)建相同的數(shù)據(jù)
字段名 bigint not null,)
engine=InnoDB charset=utf8; #指定引擎、指定字符集
刪除表:drop table 表名;
更改表名:alter table 表名 rename 新表名;
字段:
查看字段:desc 表名;
增加字段:alter table 庫(kù)名.表名 add 字段名 varchar(200) not null;
刪除字段:alter table 表名 drop 字段名;
更改字段數(shù)值類型:alter table 表名 modify 字段名 varchar(200);
更改字段名:alter table 表名 change 字段名 新字段名 varchar(100);
數(shù)據(jù):
查看數(shù)據(jù):select 字段,字段 from 表名;
查看數(shù)據(jù):select * from 庫(kù)名.表名\G; # “\G”為標(biāo)準(zhǔn)化輸出,不加會(huì)亂碼
插入數(shù)據(jù):insert into 表名 values(01,”Tom”,13); # varchar類型需要加“”
插入數(shù)據(jù):insert into 表名 (ID,Name,University,Secort,Sex) values(1,"TOM","SHANDONG","A","man");
刪除數(shù)據(jù):delete from 表名 where ID=1;
更改數(shù)據(jù):update 表名 set Name=('TOM') where ID=1;
用戶:
創(chuàng)建用戶:create user ‘name’@’x.x.x.x’ identified by “password”; (本地用戶localhost一個(gè)網(wǎng)段x.x.x.%,全網(wǎng)%)
刪除用戶:drop user 'name'@'x.x.x.x';
delect from user where user=’name’ and host=’x.x.x.x’;
授權(quán):
grant all privileges on 庫(kù)名.* to 'user'@'x.x.x.x' identified by "密碼";
grant select, delete, insert, update on cloud_class.* to 'user'@'x.x.x.x'; (存在的用戶 不用給密碼)
# 可以只授權(quán)select, delete, insert, update其中一個(gè)或多個(gè)權(quán)限
連接MySQL:
mysql -uroot -p -h指定連接到的ip -D指定連接的庫(kù)-P指定連接的端口