随便写的sql语句

随便写的简单的sql语句,贴上来便于以后回顾

mysql -uroot -p; #输入密码,登录数据库

show databases; #查看所有数据库

create database zqs_db character set utf8; #创建数据库并设置编码为utf8

use zqs_db; #选择数据库

show tables; #查看当前数据库中所有的数据表


#创建数据表
create table student(
	id int unsigned not null auto_increment primary key,
	name char(8) not null,
	sex enum('男','女') not null default '男',
	age tinyint unsigned not null,
	tek char(13) not null default '-'
	#foreign key(classid) references class(id);#创建外键
);


#插入数据(全部字段)
insert into student values(null,'张三','男',21,15212344321);
#插入部分数据
insert into student (name) values('周一');


#查询数据
select * from student where age>18 and sex='男';


#删除数据 
delete from student where age<18 and tel = '-';


#添加字段
alter table student add address char(60) not null after id;
#删除字段
alter table student drop address;
#修改字段名称
alter table student change tek tel char(13) not null default '-';


#explain select * from student;
#show status;
#show status like 'uptime'; #当前mysql 运行时间
#show status like 'connections'; #查看数据库的链接数量
#show status like 'slow_queries'; #显示慢查询
#set long_query_time=2;#即修改慢查询时间为2秒 
#show variables like 'long_query_time';
#create index name_index  on student(name);#创建普通索引
#explain select name from student; 
#create unique index id_un on student(id); #创建为一索引
#explain select * from student where id>1;
#create fulltext index address_ft on student(name);#创建全文索引
#explain select * from student where match(name)against('周一');


#show index from student; #查看指定表的全部索引
#show keys from student; #查看指定表的全部索引
select id,sex,avg(age) as age_avg,sum(age) as age_sum,count(*) as count
from student
where id>1  #where 必须出现在group by前
group by sex
having age_sum >100; #having 必须出现在 group by 后




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值