語法:
一步步由淺到深,這裏用的都是mysql做的。
基礎:
連接數據庫:
mysql -h10.20.66.32 -uroot -p123456
- 1
-h後面是mysqlServer所在地址,-u後面是用戶名,-p後面是密碼。
查看數據庫
show databases;
- 1
使用數據庫
use test;
- 1
- 查看錶
show tables;
- 1
查看錶結構
desc winton
- 1
建表
create table t1(
id int not null primary key,
name char(20) not null
);
- 1
- 2
- 3
- 4
語法 create table 表名稱( 字段名 字段名類型 字段描述符,字段名 字段類型 字段描述符);
刪除表
drop table test;
- 1
語法:drop table 表名稱;
修改表
添加字段
alter table t1 add(score int not null);
- 1
語法:alter table 表明稱 add(字段名 類型 描述符);
移除字段
alter table t1 drop column score;
- 1
語法:alter table 表名 drop colunm 字段名,drop colunm 字段名;
變更字段
alter table t1 change name score int not null;
- 1
語法:alter table 表名 change 舊字段名 新字段名 新字段描述符
插入
全字段插入
insert into winton values(001,'zww'),(002,'rs');
- 1
語法:insert into 表名 values(字段1值,字段2值,……),(字段1值,字段2值,……);
個別字段插入
insert into winton(id) values(004);
- 1
查看插如後的結果,如上圖所示。
語法:insert inton 表名(字段名) values(值一),(值二);
普通查詢
單表全字段查詢
select * from t1;
- 1
語法:select * from 表名;
單表個別字段查詢
select id from t1;
- 1
語法:select 字段一,字段二 from 表名;
多表查詢
select t1.id,t1.score,winton.name from t1,winton;
- 1
語法:select 表一字段,表二字段,表三字段,…… from 表一,表二,表三,……
條件查詢
單表條件查詢
select * from t1 where socre>90;
- 1
語法:select 字段1,字段2 from 表名 where 條件;
多表條件查詢
select t1.id,t1.score,winton.name from t1,winton where t1.id=winton.id;
- 1
語法:select 表一字段,表二字段 from 表一,表二 where 條件;
嵌套查詢
select name from winton where id=(select id from t1 where score=90);
- 1
語法:select 字段一,字段二…… from 表名 where 條件(查詢);
並查詢
(select id from t1 )union(select id from winton);
- 1
交查詢
select id from t1 where id in (select id from winton);
- 1
刪除
delete from winton where id=4;
- 1
語法:delete from 表名 where 條件;
更新
update t1 set score=69 where id=2;
- 1
語法:update 表名 set 更改的字段名=值 where 條件;
常用函數
求和
select sum(score) from t1;
- 1
注:sum(字段) 對字符串和時間無效
求平均值
select avg(score) from t1;
- 1
注:avg(字段)對字符串和時間無效
計數
select count(*) from t1;
- 1
注:count(字段名)不包含NULL;
求最大值
select max(name) from winton;
- 1
注:max(colunm)返回字母序最大的,返回數值最大的
求最小值
select min(name) from winton;
- 1
注:min(colunm)返回字母序最小值,返回數值最小值
常用的修飾符
distinct 字段中值唯一
select distinct name from winton;
- 1
limit查詢結果數限制
select * from winton limit 2;
- 1
order by 排序
select * from winton order by name;
- 1
注:默認是升序
desc 降序
slelect * from winton order by name desc;
- 1
asc 升序
select * from winton order by name asc;
- 1
group by 分組
select name from winton group by name;
- 1
索引
創建普通索引
create index wintonIndex on winton (name);
- 1
語法:create index 索引名稱 on 表名 (字段一,字段二,……);
創建唯一索引
create unique index wintonIndex on winton (id);
- 1
語法:create unique index 索引名 on 表名 (字段一,字段二,……);
ps:unique index 要求列中數據唯一,不能出現重複。
移除索引
drop index wintonIndex on winton;
- 1
語法: drop index 索引名 on 表名;
結尾
恩,基本能想起來的就值麼多了,都是最基礎,最常用的一些。