1、create database 新庫名 新建庫

2、查看所有的倉庫

3、use 庫名 使用庫

4、show tables 查看庫中所有表

5、create table 表名(字段名 1 字符類型(字符長度),字段名2 字符類型(字符長度));
案例:create table hz015(id int(10),name char(20));

6、格式:INSERT into 表名 VALUES(值1,值2); 插入數據
方法一:
案例:INSERT into hz015 VALUES(1,"xiaozhu");
方法二:
格式:INSERT into 表名(字段1) VALUES(值1); 插入部分數據
INSERT into hz015 (id)VALUES(4);

7、desc 表名 查看 表結構

1、建庫
2、使用庫
3、建表
4、插入數據
5、對數據增刪改查
================================
建表:
create table 表名( 字段名1,數據類型(數據的長度)約束;字段名2,數據類型(數據的長度)約束 )
================================
字符類型:
一個漢字占多少 長度和編碼有段 (gbk 和 utf8 這些都是字符集格式,)
utf-8: 一個漢字等於 3個字節
gbk :一個漢字等於2個字節
常用的類型:
(1)數值類型
int 字節:4 大整數值
bigint 字節:8 極大整數值
float 字節:4 浮點數
(2)字符類型
char 0-255 定長字符串
varchar 0-65535 字節 變長字符串
(3)時間類型
date yyyy--mm-dd 年月日 字節3
time hh:mm:ss 時分秒 字節:3
date time 日期+時間 字節:8
year 年 字節:1
================================
約束:
實際上就是表中的限制條件
作用:表在設計的時候加入 約束的目的是為了保證表中的記錄完整和有效
約束種類:
1、非空約束 (not null) 非空 用於保證字的值不能為空
2、唯一約束 (unique) 簡稱:(uni) 保證字段值具有唯一性,一個表可以有多個,能為空
3、主鍵約束 (primary key)簡稱 :pri 保證字段值具有唯一性 、一個表中只用一個,不能為空
4、默認值 (default )
5、auto_incrment 自增長
備注:與主鍵約束一起使用
6、空值插入使用:null
7、保證中文顯示,在建表語句后加上: default charset=utf8
案例1:
create table aa(id int(10) PRIMARY key ,age int(10) default 18 ,sfz int(40) UNIQUE )DEFAULT CHARSET=utf8 ;
select * from aa ;
desc aa ;
INSERT into aa(id,sfz) VALUES(1,1232)
案例2:
SELECT * from dt;
create table dt( name char(10), n year not null ,cj_data char(10));
INSERT into dt VALUES("tian",1995,11112222)
================================
刪除表:
格式:drop table 表名;
案例:drop table bb ;
================================
表結構:
(1)添加表字段 add
格式:alter table 表名 add 字段名 字段類型(字符長度);
案例:alter table da add age int(10);

(2)修改表字段 change
格式:alter table 表名 change 原字段名 新字段名 新字符類型(新字符類型長度 );
案例:alter table da change age sex char(10 );
(3)刪除表字段 drop
格式:alter table 表名 DROP 刪除的字段名 ;
案例:alter table da DROP name ;
(4)調換表字段的順序
a、first 字段放在第一位
格式:ALTER table 表名 add 新字段名 字符類型(字符長度) first ;
案例:ALTER table da add id int(10) first ;
b、modify after (在什么后面)
格式:ALTER table 表名 modify 字符類型(字符長度) after 原字段 ;
案例:ALTER table da modify age int(10) after id ;
================================
查詢語句:
select 語句
(1)查詢一個表中所有的數據
格式:SELECT * from 表名;
案例:SELECT * from hz015 ;
(2)查詢一個表中部分字段的數據
格式:SELECT 字段名1,字段名2 from 表名 ;
案例:SELECT id from hz015 ;
備注:用,逗號分隔
(3)取別名 as 查詢語句

(4)指定條件查詢內容
格式:SELECT * from 表名 where 條件 ;
案例:SELECT * from hz015 where id=1 ;
條件:比較運算符,and,or ,in ,not in ,between and ,is null ,is not null
where 條件使用注意:
比較運算符:(=,>,<,>=,<=,!=(不等於),<>(不等於))
SELECT * from hz015 where id=1 ;
SELECT * from hz015 where id>1 ;
SELECT * from hz015 where id<3 ;
SELECT * from hz015 where id<=3 ;
SELECT * from hz015 where id>=3 ;
SELECT * from hz015 where id<>3 ;
SELECT * from hz015 where id!=3 ;
and 條件 同時滿足所有條件, 比如 :條件1和條件2 同時滿足
格式:select * from表名 where 條件1 and 條件2 ;
案例:select * from emp where dept2=101 and incoming>3000 ;

or 條件 滿足所有條件中的其中一個條件, 比如 :條件1和條件2 滿足其中一個或都滿足
格式:
select * from 表名 where 條件1 or 2條件 ;
案例:select * from emp where dept2=101 or incoming>5000 ;
between and 在什么范圍內
格式:SELECT * from 表名 where 條件 between 開始值 and 結束值;
案例:SELECT * from hz015 where id between 2 and 4;
in 在一組數據中選(或匹配其中的數據)
格式:select * from 表名 where 字段 in (匹配的值)
案例:select * from hz015 where id in (1,3,10,11)
not int 匹配不在一組數據中的數據
格式:select * from 表名 where 字段 not in (不匹配的值)
案例:select * from hz015 where id not in (1,3,10,11)
is null 為空
格式:select * from 表名 where 字段名 is null ;
案例:select * from hz015 where name is null ;
is not null 不為空
格式:select * from 表名 where 字段名 is not null ;
案例:select * from hz015 where name is not null ;
==================================================
order by 排序
升序:asc
默認:asc 可以不寫
格式:select * from 表名 ORder by 字段名 ;
案例:select * from emp ORder by incoming ;
格式二:select * from 表名 ORder by 字段名 asc ;
案例:select * from emp ORder by incoming asc ;
降序:desc
格式:select * from 表名 ORDER BY 字段名 desc ;
案例:select * from hz015 ORDER BY id desc ;
二次排序
格式:select * from 表名 ORder by 字段1 desc , 字段2 desc ;
案例:select * from emp ORder by dept2 desc ,incoming desc ;
==================================================
like 模糊匹配查詢
% :表示匹配0個字符或多個字符
—:表示一個字符
案例1:select * from emp where sid like "15%"; 表示匹配15開頭的數據
案例2:select * from emp where sid like "%6%"; 表示匹配含有6的數據
案例3:select * from emp where sid like "%4"; 表示匹配4結尾的數據
案例4:select * from emp where sid like "___4"; 表示匹配具體的字符數結尾,或開頭
==================================================
limit 顯示行數,限制作用
limit m,n m表示是下標, n 表示步長
表中下標是從0開始,
案例1:select * from emp limit 0,3; 0 表示索引第一行,3表示一共顯示3行
案例2:select * from emp limit 3,2; 3表示第四行,2 表現顯示兩行
==================================================
group by 分組
having 接條件,和我們where 一樣 ,
group by 分組后查詢出來結果,在根據條件接having
案例:select dept2,sum(incoming) from emp group by dept2 ;
案例2:select dept2,sum(incoming) s from emp group by dept2 HAVING s>10000 ;
==================================================
函數:
max 最大值
min 最小值
avg 平均值
count 統計
sum 求和
distinct 去重
select max(incoming) as "最高工資" from emp ; 最高工資
select min(incoming) from emp ; #最少工資
select avg(incoming) from emp ; #平均工資
select count(incoming) from emp ;#發工資的人數
select sum(incoming) from emp ; #工資總額
select DISTINCT(dept2) from emp ; # 去重
==================================================
update set 改
格式 :update 表名 set 修改的字段=字段新值 where 條件
案例:update hz015 set name="xiaozhang" where id=4 ;
案例:update hz015 set name="xiaozhang" where id=4 or id=1 ;
==================================================
drop 刪除數據庫和刪除表 速度快
truncate 快速刪除
delete 刪除
drop > treuncate>delete
===============================
delete
(1)DELETE from 表名 刪除整個表的數據,表還存在 ;
案例:DELETE from hz015 ;
(2)DELETE from 表名 where 條件 刪除指定的內容
案例:DELETE from hz015 where id=4 ;
===============================
drop
(1)drop 庫名 刪除庫
案例:drop database kk ;
(2)刪除表
格式:drop table 表名 ;
案例:drop table hz015 ;
===============================
