mysq基础操作

时间:2022-04-07 23:56:37

mysq基础操作

创建表:

create table customer(mid char(5) primary key,name varchar(20),birth date,sex char(1) DEFAULT '0');

显示表的结构;

desc customer;

插入:

insert customer values('N001','小小','1980-12-12',1);

检索;

select* from customer;

创建表的时候指定字符集:

create table customer(mid char(5) primary key,name varchar(20),birth date,sex char(1) DEFAULT '0')

charset=utf8;

显示所有的表;

show tables;

显示表的结构 desc customer;

删除表:drop table customer;

自增序列 auto_increment

create table goods(id int auto_increment primary key,name varchar(30));
insert into goods(name) value('桃子');

insert into customer(mid,name,sex)values('H0001','李佳',0);
insert into customer(mid,name,birth) values('G0001','杜依依','1974-09-18');
insert into customer(mid,name,sex)values('G0001','李玉',0);

更新存在的记录:

mysq基础操作

update customer set name='李玉枝',birth='1997-12-24' where mid='N001';

update customer set sex='0';

删除表中的记录;

delete from customer where mid='N002';

清除所有的数据;

truncate table customer;

数据检索select;

1.推荐明确指定列名

select mid,name from customer;

2.条件检索

select name,birth from customer where birth>='1980/01/01';

3.模糊检索

select name,birth from customer where name like '李%';

select name,birth from customer where name like '李_';

4.null条件

select name,birth from customer where birth  is NUll;

5.多个组合表达

select name,birth from customer where sex='0' and birth is not null;

select name,birth from customer where sex='0' or birth<'2000/3/3';

运算符优先级:not <and<or

6.结果排序; asc升序 desc 降序

select name,birth from customer order by sex asc,birth desc;

7.去得指定件数的记录 (eg:第5名到第九名的数据)

select name,birth from customer order by birth desc limit 2;

原则上limit语句要和order by语句同时使用,没同时使用则取出随机结果

8.数据分组

以特定字对记录进行整理称为分组化,group by 通常与统计函数进行使用

avg 平均数

count 件数

max 最大值

min 最小值

sum 合计值

统计男生女生多少人:

select sex ,count(mid) from customer group by sex;

9.列的别名

select sex ,count(mid) as cnt from customer group by sex;

count 统计的是非null的记录