心專才能繡得花,心靜才能織得麻。書山有路勤爲徑,學海無涯苦做舟。mysql
create database (數據庫名); #建立數據庫
show databases; #查看已擁有的數據庫
use (數據庫名) #使用該數據庫
show tables; #查看該數據庫下已擁有的表
drop database (數據庫名); #刪除該數據庫sql
create table (表名)(列名稱1 數據類型 約束,列名稱2 數據類型 約束,列名稱3 數據類型 約束,....); #建立表數據庫
show create table (表名) #查看錶的基本信息
desc (表名) #查看錶的基本結構日誌
drop table (表名); #刪除該表(可經過日誌恢復)
truncate table(表名); #刪除該表(不可經過日誌恢復)排序
alter table Student modify Sname varchar(25); #這裏修改了Student表Sname的數據類型it
alter table Student add Sclass varchar(20); #這裏添加了一個Scalss字段table
alter table Student drop column(可加可不加) Sclass; #這裏把新加的字段進行刪除class
alter table Student change Sclass Stel varchar(20); #這裏把Sclass更名爲Steldate
alter table Student rename SC; #這裏把表名Student改成Scselect
alter table user add age varchar(20) first; #插在第一行
alter table user add sex varchar(20) after (你要插在誰後面);
insert into Student(Sno,Sname,Sage,Ssex) values("10001","張三",19,"男"); #方法一
insert into Student values("10002","李四",22,"男"); #方法二
insert into Student(Sno,Sname,Sage,Ssex) values("10003","王五",20,"男"),("10004","趙六",21,"男"); #方法三(多條一塊兒添加)
update Student set Sage = 21 where Sno="10001" #把學號爲10001的學生年齡改成21
delete from Student where Sno = '10001' #把學號爲10001的學生的記錄刪除
delete from Student #刪除表中的全部記錄
select name from user;#在user表裏面查詢name
select name,age from user;#在user表裏面查詢name,age
select * from user;#查詢user表裏面的全部信息
select distinct age from user;#去除重複值
select * from user where id=2;#查詢id爲2的信息
select * from user where age between 30 and 40;#查詢年齡在30到40歲人的全部信息
select *from user where age =30 or age =40;#查詢年齡爲30或者40歲的人的全部信息
select *from user where age !=30 ;#查詢年齡不等於30歲的人的全部信息 '<>'也爲不等於
select *from user where age in(30,40) ;#查詢年齡爲30或者40歲的人的全部信息
select *from user where name like '劉%';#查詢名字姓氏爲劉的全部人的信息
select *from user where name like '%偉%';#查詢全部名字帶偉的人的信息
select * from user order by age;#按年齡排序(升序)或者在後面加asc
select * from user order by age desc;#按年齡排序(降序)
select * from user limit 5,3;#取前5條記錄後面的3條記錄的信息
select * from user limit 5;#取前5條記錄的信息
select * from user group by age;#查詢去除重複的年齡的全部信息(分組)
select count<*> from user; #查詢一共有幾條數據
select name as na from user;#給name取別名na並查詢他的全部信息
select sname ,cname from student inner join cs on student.id = cs.id;#在兩張表之間查詢(內聯)
select student.sname from student left join cs on student.id = cs.id;#左連接 以左邊爲基準
select student.sname from student right join cs on student.id = cs.id; #右鏈接 以右邊爲基準
select *from student where id in (select id from cs);#嵌套查詢
select *from student where id in (1,2,3);#和上面結果同樣