一、進入數據庫操作界面
1、mysql -u root -p 敲回車 ,輸入密碼 ,進入數據庫操作界面
2、show databases 查看所有的數據
3、use 數據庫名 使用數據庫
4、show tables 顯示庫中的所有表
5、建表語句
格式: create table 表名(字段名1 字符類型(字符長度),字段名2 字符類型(字符長度));
案例:create table aa(id int(10),name varchar(20));

6、查看表結構:
desc 表名
案例:

7、在navicat 中===點擊庫名====點擊查詢====新建查詢=====在新建查詢中輸入sql語句


8、插入數據:
(1)插入方式一:
格式:INSERT INTO 表名 VALUES(值1,值2);
案例:INSERT INTO aa VALUES(1,"aa");

(2)插入方式二:(插入部分字段)
格式:INSERT into 表名(字段名) VALUES(字段值)
案例:INSERT into aa(id) VALUES("4")

(3)插入的中文字符變成?號
解決方案:
在建表時的語句后面添加:
DEFAULT charset=utf8;
案例:create table cc(cid int(5),cname char(20))DEFAULT charset=utf8;

9、刪除表格
drop table 表名
案例:drop table yy ;

==============================
二、數據類型
1、數值類型
int 存儲類型
float 浮點數

2、字符類型
char
varchar

3、時間類型
date
time
datetime
year

注意字符的長度:
int(20)
varchar(20)
======================================
約束:
約束用於對表中字段進行限制,保證表中數據的正確性和唯一性
1、primary key 主鍵約束
非空,唯一,用於唯一標識的記錄,類似身份證。
一個表中只用一個主鍵

2、not null 非空約束

3、 unique 唯一索引
保證字段值具有唯一性,並且能為空,一個表中可以有多個唯一索引

4、default 默認值約束
定義:默認給字段指定默認值

5、auto_increment 自增長約束
作用:在整數類型,字段默認值從1開始自增
(1)一般和主鍵約束一起使用,主要針對id
(2)每插入一條數據,就是在字段上自動+1,

=========================================
1、新建表

表結構的操作:
2、add 添加字段
格式:ALTER TABLE 表名 add 字段名 字符類型(字符長度);
案例:ALTER TABLE student2 add dcs int(20);

3、change 修改字段
格式:ALTER TABLE 表名 change 舊字段名 新字段名 字符類型(字符長度);
案例:ALTER table student2 change dcs hzdcs int(19);

4、 drop 刪除字段
格式:ALTER table 表名 drop 字段名 ;
案例:ALTER table student2 drop hzdcs ;

5、rename 修改表名

6、modify after 字段的調換
格式:ALTER table 表格 MODIFY 變動的字段 字段類型(字段長度) after 指定字段 ;
案例:ALTER table hz MODIFY math int(10) after id ;

7、first 添加字段到第一位
格式:alter table 表名 add 表字段 字符類型(字符長度) first ;
案例:alter table hz add no int(20) first ;

=========================================
數據庫匯中:增、刪、改、查
一、查詢語句:
(1)查詢一個表中的所有數據
格式:select * from 表名 ; * 表示所有的字段
案例:select * from hz ;

(2)查詢部分字段
格式:select 字段1,字段2 from hz ;
案例:select id,name from hz ;

(3)查詢字段可以通過as 取別名
格式:
案例1( as寫,):
select id as " 編號",name as "姓名" from hz ;
案例2(可以省略 as不寫):
select id " 編號",name "姓名" from hz ;

(4)指定條件查詢內容:
where +條件
條件1:
比較運算:>,<,=,!=,<>,>=,<=
條件2:
and ,or ,between ....and ,in , is not null
案例1:= 等於
select id ,name from hz where id=1;
案例2:> 大於
select id ,name from hz where id>1;
案例3:<小於
select id ,name from hz where id<2;
案例4:<=小於等於
select id ,name from hz where id<=2;
(5)
案例5:>=大於等於
select id ,name from hz where id>=2;
(6)!=不等於
案例6:select id ,name from hz where id != 2;
(7)<>不等於
select id ,name from hz where id <> 2;
================================
(8)and 同時滿足條件
案例8; and 是同時滿足多個條件
select id ,name,math from hz where id > 2 and math>90;
(9)or 只要滿足其中一個條件 就顯示
select id ,name,math from hz where id > 6 or math>90;
(10)between 。。。and 在什么范圍之間
案例:select * from hz where id BETWEEN 3 and 6 ;
備注:包含了本身,
(11)in 在一組數據中選擇(在數據匯總匹配)
案例:select * from hz where id in (1,3,8)
(12)not in 不在一組數據中選
案例:select * from hz where id NOT in (1,3,8)
(13)is null 為空的數據
select * from hz where class is null;
(14)is not nu 不為空的數據
select * from hz where class is not null;
==========================================
order by 排序
(1)降序 (大到小)
order by desc
案例:select * from hz order by id desc ;

(2)升序(小到大)
asc 或不寫
案例:
select * from hz order by id asc ;
select * from hz order by id ;

(3)二次排序
案例:select * from hz order by math desc ,id desc;

=====================
like 模糊匹配查詢
%:表示匹配1個字符或多個字符
_ : 下滑線表示一個字符
案例1:匹配xx開頭的數據
select * from hz where math like "7%"; # 匹配7開頭的數據
案例2:匹配xx結尾數據
select * from hz where math like "%7"; #匹配7結尾的數據
案例3:匹配含有xx結尾數據
select * from hz where math like "%7%"; #匹配含有7的數據
案例4:匹配指定位數的數據
select * from hz where math like "7_"; #匹配具體位數的數據
=====================
limit 顯示指定的數據,限制;
根據索引位置來取值,從0開始,一個表第一行的索引就是0,第二行就是1
select * from hz limit 2; #表示取兩行數據, 2 表示步長
select * from hz limit 1,2#表示從索引1開始第二行,2表示步長2行
select * from hz limit 4,3 ;# 表示從索引4開始取值,第五行開始,取三行,
=====================
sql 聚合函數
max 最大數
案例1:select max(math) from hz ;
min最小數
案例2:select min(math) from hz ;
avg 平均值
案例3:
select avg(math) from hz ;
sum 求和
案例4:
select sum(math) from hz ;
count 統計
案例5:select count(math) from hz ;
distinct 去重
案例6:
select DISTINCT(math) from hz ;
==================
group by ....... having
group by 是分組,一般不會單獨使用,通常和聚合函數組合使用
案例1:分組
select sum(math),class from hz GROUP BY class ;
案例2:分組 在條件 having
(1)select sum(math) s,class from hz GROUP BY class having s>200 ;
(2)select sum(math) s,class from hz GROUP BY class having sum(math)>200 ;
注意:having 一般接在group by 后面
==================
改:
update ......set......
格式:update 表名 set 字段名=新值 where條件;
案例:update hz set id=1 where id=9;
==================
刪除:
(1)delete
格式:DELETE from 表名 where 條件;
DELETE from hz where id=1;
(2) truncate 快速刪除數據
格式:
truncate 表名 ;
案例:
truncate ff ;
(3)drop 刪除
格式:drop table 表名
案例:drop table emp ;
drop >truncate> delete
==================
單行注釋:ctrl +/
取消注釋:shift+ctrl+/
多行注釋:選中多行 ,ctrl +/
取消注釋:選中多行 shift+ctrl+/
===============================
備份:
(1)備份表結構:
格式:create table 新表名 like 舊表名;
create table emp_new like emp;

(2)備份表數據
格式:
INSERT into 新表結構 select * from 舊表有數據 ;
案例:
INSERT into emp_new select * from emp ;

(3)備份部分數據
格式:INSERT into 表名(字段1,字段2) select 字段1,字段2 from 舊表 ;
案例:INSERT into emp2(sid,name) select sid ,name from emp ;

(4)備份表結構和數據
格式:
create table 新表 as (select * from 原表);
案例:create table hh as (select * from emp);

=========================================================================
在linux 中:
備份:
格式:mysqldump -u root -p 原庫>新sql腳本名
案例:mysqldump -u root -p hz017>/home/hz17.sql
還原:

還原:
格式:mysql -u root -p 新庫<備份好的腳本
案例:mysql -u root -p new</home/hz17.sql

