庫的相關(guān)內(nèi)容
create database 'name' charset utf8
創(chuàng)建一個(gè)數(shù)據(jù)庫并設(shè)置字符集是utf8
create database if not exists 'name'
如果不存在就創(chuàng)建衷模,存在就忽略
show databases
顯示已存在的所有數(shù)據(jù)庫
show databases like 'db_%'
查看數(shù)據(jù)庫名字以db_開頭的數(shù)據(jù)庫
use 'database_name'
選擇數(shù)據(jù)庫
alter database 'database_name' default charset gbk default collate gbk_chinses_ci
修改數(shù)據(jù)庫字符集
drop database if exists 'database_name'
如果存在就刪除數(shù)據(jù)庫擒贸,不存在就忽略
show engines
顯示所有引擎
MySQL數(shù)據(jù)類型
數(shù)值類型:int
日期時(shí)間類型:date
字符串類型:varchar
浮點(diǎn)類型:float
表的相關(guān)內(nèi)容
創(chuàng)建表
create table table-name(
id int not null auto_increment primary key comment 'id'
name varchar(10) not null comment '名字',
email varchar(20) not null comment '電子郵件'
);
查看所有表
show tables
查看表結(jié)構(gòu)
desc table_name
查看表狀態(tài)
show table status
刪除一個(gè)表
drop table table_name
添加一個(gè)字段
alter table table_name add 字段名 int(10) not null
刪除一個(gè)字段
alter table table_name drop column 字段
修改字段類型
alter table table_name modify 字段 varchar(15) null
更新表中的數(shù)據(jù)
update table_name set 字段 = ' '
刪除表的所有行
delete from table_name
刪除表并重新創(chuàng)建表結(jié)構(gòu)
truncate table_name
插入數(shù)據(jù)
insert into table_name values ( )
檢索表中的所有數(shù)據(jù)
select * from table_name
檢索表中的數(shù)據(jù)并最多顯示5行
select * from table_name limit 5
去重
select distinct 字段 from table_name
排序
select * from table_name order by 字段
分組
select * from table_name group by 字段
判斷條件
select * from table_name where xxxxxx
自聯(lián)結(jié)
select p1.scores,p1.id from scores as p1 惩激,scores as p2 where p1.stu_id = p2.stu_id and p2.id = 2;
左外聯(lián)結(jié)
select students.name,scores.score from students left outer join scores on students.stu_id = scores.stu_id and scores.score < 60;
內(nèi)聯(lián)結(jié)
select students.name,scores.score from students inner join scores on students.stu_id = scores.stu_id and scores.score < 60; 內(nèi)聯(lián)結(jié)
子查詢
select name from students where stu_id in (select stu_id from scores where score < 60);
視圖
reate or replace view 視圖名 as