创建表
create table 表名(
primary key();
);
desc 表名;//查看表的结构
alter table 表名 modify 表中的字段 改成什么;//修改表的结构中的某一字段
alter table user change 字段名 改成什么 数据类型 是否为空;//修改表中的字段名
alter table user add password2 char(32) not null comment '用户密码' after user_name;//zai user_name 的前面添加一个password2的字段(after 是表示添加在谁的后面,如果不写就表示添加在最后)
alter table user drop password;//删除字段
alter table user rename to user2;//修改表名。可以不用加to
create table user2(
id int unsigned not null auto_increment primary key,
user_name varchar(20) not null comment '用户名',
password char(32) not null,
email varchar(50) not null,
mobile char(11) not null,
fee decimal(10,2) default 0.00 not null,
age tinyint not null
);
//插入某一行
方式一
insert into (属性名1,属性名2, 。。。) values (值1, 值2, 。。。);
//方式二(需要包含所有的属性才会插入成功,少一个都不行)
insert into values (值1, 值2, 。。。);
删除
delete from 表名 where 条件;
//修改属性当中的值
update 表名 set 属性= 修改后的值 where 条件;
查询重复的内容(属性的内容是重复的,查询出来以后显示的是重复的内容)
select distinct(属性名字) from 表名
连接字段(连接函数)
select concat(user_name, email) from user;
select concat(属性1, 属性2) from 表名;(返回的是两个属性的值拼接在一起的值)
select concat(user_name, email) as user_name_email from user;(as 后面是连接后的重命名)
select concat_ws('=',user_name, email) as user_name_email from user;(加上分隔符,写在属性的前面)
//模糊查询('%u'左模糊,‘u%右边模糊’,'%u%'中间含有u的全模糊)
select user_name from user where user_name like '%u%';(查询该字段里面有u的user_name)
//排序
select * from user order by id desc;
如果不加desc就是升序排列
select 属性名 from 表名 order by 属性;
//计算表中的数据有多少条
select count(*) from user;
select count(属性名) from 表名;
//求和
select sum(age) as 显示的名字 from user where id > 2;
select sum(属性名) from 表名;
算平均数
avg(属性名)
最大值
max(属性名)
最小值
min(属性名)
create table 表名(
primary key();
);
desc 表名;//查看表的结构
alter table 表名 modify 表中的字段 改成什么;//修改表的结构中的某一字段
alter table user change 字段名 改成什么 数据类型 是否为空;//修改表中的字段名
alter table user add password2 char(32) not null comment '用户密码' after user_name;//zai user_name 的前面添加一个password2的字段(after 是表示添加在谁的后面,如果不写就表示添加在最后)
alter table user drop password;//删除字段
alter table user rename to user2;//修改表名。可以不用加to
create table user2(
id int unsigned not null auto_increment primary key,
user_name varchar(20) not null comment '用户名',
password char(32) not null,
email varchar(50) not null,
mobile char(11) not null,
fee decimal(10,2) default 0.00 not null,
age tinyint not null
);
//插入某一行
方式一
insert into (属性名1,属性名2, 。。。) values (值1, 值2, 。。。);
//方式二(需要包含所有的属性才会插入成功,少一个都不行)
insert into values (值1, 值2, 。。。);
删除
delete from 表名 where 条件;
//修改属性当中的值
update 表名 set 属性= 修改后的值 where 条件;
查询重复的内容(属性的内容是重复的,查询出来以后显示的是重复的内容)
select distinct(属性名字) from 表名
连接字段(连接函数)
select concat(user_name, email) from user;
select concat(属性1, 属性2) from 表名;(返回的是两个属性的值拼接在一起的值)
select concat(user_name, email) as user_name_email from user;(as 后面是连接后的重命名)
select concat_ws('=',user_name, email) as user_name_email from user;(加上分隔符,写在属性的前面)
//模糊查询('%u'左模糊,‘u%右边模糊’,'%u%'中间含有u的全模糊)
select user_name from user where user_name like '%u%';(查询该字段里面有u的user_name)
//排序
select * from user order by id desc;
如果不加desc就是升序排列
select 属性名 from 表名 order by 属性;
//计算表中的数据有多少条
select count(*) from user;
select count(属性名) from 表名;
//求和
select sum(age) as 显示的名字 from user where id > 2;
select sum(属性名) from 表名;
算平均数
avg(属性名)
最大值
max(属性名)
最小值
min(属性名)