MySQL常用語法

顯示有哪些數(shù)據(jù)庫:show databases;

創(chuàng)建數(shù)據(jù)庫:create database database1;

刪除數(shù)據(jù)庫:drop database database1;

使用數(shù)據(jù)庫:use database1;

查看表:show tables;

新建表:create table table1(

id int(10) not null primary key auto increment ,

a varchar(20),

b int(10),

c varchar(10) default ‘男’,

d int(10)

);

查看表結(jié)構(gòu):desc student;

刪除表:drop table student;

刪除主鍵:alter table table1 drop primary key;

增加主鍵:alter table table1 add primary key(id);

刪除字段:alter table table1 drop classid;

增加字段:alter table table1 add classid int(10);

增加記錄:insert into table1(a,b,c,d) values(‘哈哈’,1,’你好’,2);

刪除:delete from table1 where b = 1;

修改:update table1 set a = ‘哈哈哈’ where b=1;

通配符 %:多個(gè)任意的字符 _:一個(gè)字符

update table1 set b = 2 where name like ‘%i%’;

查詢:

select * from table1;

select id,name from table1;

select * from table1 where b like ‘%i%’;

排序 默認(rèn)升序asc 降序desc

select * from table1 order by a asc;

select * from table1 order by a desc;

select * from table1 order by a desc,b asc;

組函數(shù) min max count avg

select count(*) from table1;

select min(a) from table1;

select avg(a) from table1;

select max(a) from?table1;

分組:select classid,avg(b) from table1 group by classid having avg(b)>5;

字句作為查詢的條件:select * from table1 where age = (select min(b) from table1);

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊13 分享