表操作
查看當(dāng)前數(shù)據(jù)庫(kù)中的所有表
show tables;
創(chuàng)建表
create table 表名(列+類(lèi)型+約束,每一列用逗號(hào)分隔)
例如: create table zr1(id int auto_increment primary key,name varchar(10) not null)
創(chuàng)建一個(gè)叫zr1,有兩個(gè)列的表格,id列為數(shù)字類(lèi)型,自動(dòng)增長(zhǎng),主鍵,名字列為字符串類(lèi)型,限制為10個(gè)字符串,不能為空
修改表
alter table 表名 add(添加)或者change(修改)或者刪除 列名 類(lèi)型; 例如
alter table students add birthday datatime; 往students里添加時(shí)間類(lèi)型,名字為生日的表頭
刪除表
drop table 表名;
查看表結(jié)構(gòu)
desc 表名;
更改表名稱(chēng)
rename table 原表名 to 新表明;
查看表的創(chuàng)建語(yǔ)句
show create table 表名;
增加 修改 刪除
全列插入
insert into 表名 values()
缺省插入
insert into 表名(列1)values(值,...)
同時(shí)插入多條數(shù)據(jù)
insert into 表名 values(),(),();
insert into 表名(列1,..) values(值1戴差,...),(值1,....);
修改數(shù)據(jù)
update 表名 set 列名=修改的值 where 條件 id=1 或者 名字=?
刪除數(shù)據(jù)
delete from 表名 where 條件(就是想要?jiǎng)h除的行衅码,比如說(shuō)id=1)
表格數(shù)據(jù)查詢(xún)操作
查詢(xún)
select * from 表名 查詢(xún)?nèi)?br>
select * from 表名 where id=1,或者name="哈哈"(條件);
select distinct 想要查詢(xún)的東西 from 表名; 消除重復(fù)行
比較查詢(xún)
select * from 表名 where id<=4; 查詢(xún)id小于等于4的
select * from 表名 where name!="黃蓉" 查詢(xún)不等于黃蓉的
select * from 表名 where isdelete=0; 查詢(xún)沒(méi)被刪除的
運(yùn)算符
or或者 and并且 not不哎媚,沒(méi)有
select * from 表名 where id>3 and name="哈" 查詢(xún)編號(hào)大于三叫哈的同學(xué)
模糊查詢(xún)
select * from 表名 where name like '%黃%'; 查詢(xún)帶黃字的數(shù)據(jù)蒙挑,_代表一個(gè)字符,%代表多個(gè)字符
范圍查詢(xún)
select * from 表名 where id in(1,3,8); in表示在一個(gè)非連續(xù)的范圍內(nèi)查詢(xún)1或3或8的學(xué)生
select * from 表名 where id between 3 and 8; 查詢(xún)3到8的學(xué)生
空判斷
select * from 表名 where 表頭名 is null; 查詢(xún)表頭名為空的學(xué)生
is not null 就是不為空
聚合
select count(*) from 表名; 計(jì)算總行數(shù)
count計(jì)算總行數(shù) max 最大值 min 最小值 sum 求和 avg 平均值
分組
select 列1 列2 或者聚合 from 表名 group by 列1 列2 having(加條件)
前后兩個(gè)列必須相等
having 運(yùn)算跟where相同 但是原理不同
where 是對(duì)原始數(shù)據(jù)篩選
having 是對(duì)group by 的結(jié)果篩選
排序
order by name(列1) asc/desc, age(列2) asc/desc...
默認(rèn)排序是升序幼东,從小到大
asc 從小到大
desc 從大到小
分行
limit 開(kāi)始索引确镊,分幾行
一般都是在最后寫(xiě)