1. 查看數據庫:show databases;
2. 創建數據庫:create database kuname;
3. 刪除數據庫:drop database kuname;
4. 選擇數據庫:use kuname;
5. 創建表:
create table biaoname(
age int,
name varchar(100),
sex char(10),
income int not null,
);
6. 查看某庫中的表:show tables;
7. 查看表結構:desc biaoname;
查看表類型: show table status;
8. 刪除表:drop table biaoname;
9. 表中插入記錄:
insert into biaoname
(ziduan1,ziduan2,ziduan3)
values
("value1","value2",number,now());
# now()是一個返回日期和時間的mysql里的函數
10. 插入多條數據:
insert into biaoname
(ziduan1,ziduan2,ziduan3)#可以省略該行,省略時,插入的值需要按順序
values
("value1","value2",number,now()),
("value1","value2",number,now()),
("value1","value2",number,now());
11. 查詢全部數據:select * from biaoname;
12. 從多個表中查詢數據:select * from biao1,biao2;
13. where子句(可以用binary 關鍵字設置 WHERE 子句的字符串是否區分大小寫):
select * from biao1
where binary biaojian1="content";
14. where子句用到的操作符:>,<,=,>=,<=,!=,多個條件可以用 and ,or連接:
select * from biao1
where tjian1="X" and tiaojian2>=4;
15. 修改或更新表中數據:where 指定條件可不寫
update biaoname set field1="X",field2="M"
where tjian="content" ;
16. 刪除數據沒有where子句將刪除表中所有的數據:
delete from biaoname where tiaojian="x" ;
17. 查詢含有指定內容的數據時用like(%),不用%時like和=相同的意思:
select * from biaoname
where ziduan like "%x";
意思:查找字段含有 "x"的記錄
18. 連接多個select語句用union:
select * from biaoname1
where ziduan="x"
union [ALL | DISTINCT]
select * from biaoname2
where ziduan="m";
DISTINCT: 可選,刪除結果集中重復的數據。默認是刪除重復項
ALL: 可選,返回所有結果集,包含重復數據。
19. 查詢數據排序order by:
select field1,field2 from biaoname
where ziduan="x"
order by field1 asc, field2 desc
asc升序排序,desc降序排序,默認升序
20. 結果集分組 group by:
select classname,avg(grade) from biaoname
group by classname;
#每個班級的平均成績
在分組統計的基礎上繼續統計 with rollup:
如:統計該年級的平均成績:
select classname,avg(grade) from biaoname
group by classname with rollup ;
#每班的平均成績之和/班級個數
由于繼續統計沒有指定列名所以默認為null,可用coalesce指定列明
select coalesce(classname,"指定的列明"),avg(grade) from biaoname
group by classname with rollup ;
#每班的平均成績之和/班級個數
21. 多個數據表中讀取數據inner join(inner可以不要),left join,right join:
join:會讀取在多個表中都存在的數據
SELECT a.runoob_id, a.runoob_author, b.runoob_count
FROM runoob_tbl a? #runoob 臨時作為a表
JOIN tcount_tbl b
ON a.runoob_author = b.runoob_author; #表示a,b匹配的字段
等價于:
SELECT a.runoob_id, a.runoob_author, b.runoob_count
FROM runoob_tbl a,tcount_tbl b
WHERE a.runoob_author = b.runoob_author; #表示a,b匹配的字段
left join:會讀取左邊數據表的全部數據,即便右邊表無對應數據
right join:會讀取右邊數據表的全部數據,即便左邊表無對應數據
SELECT a.runoob_id, a.runoob_author, b.runoob_count
FROM runoob_tbl a? #runoob 臨時作為a表
LEFT JOIN tcount_tbl b
ON a.runoob_author = b.runoob_author; #表示a,b匹配的字段
22. mysql查找數據表中某列是否為 NULL,用 IS NULL , IS NOT NULL:
select * from biaoname
where ziduan is (not) null;
23. 修改表名或字段時使用 alter命令:
# 修改表名稱
alter table biaoname rename to newname;
# 添加字段,默認為null,可以設置默認值
alter table biaoname add ziduanx int/char;
# 刪除字段
alter table biaoname drop ziduanx;
# 指定字段位置,默認放在最后,after后需要指出放在哪個字段后
alter table biaoname add ziduanx int first;
alter table biaoname add ziduanx char after ziduany;
# modify修改字段類型,把字段x修改為 char類型
alter table biaoname modify ziduanx char(10);
# change 可以修改字段名稱 類型,字段名,類型可以和原來的相同
alter table biaoname change ziduanx ziduany int;
# 修改字段時,可以為該字段的設置默認值(該字段原來沒有默認值)
alter table biaoname modify ziduanx int not null default "ellie001";
# 修改字段的默認值用alter
alter table biaoname alter ziduanx set default 1000;
# 刪除字段的默認值用drop
alter table biaoname alter ziduanx drop default;
# 修改表類型
alter table biaoname engine=myisam;