使用workbench写SQL语句-初级

一、简单SQL语句使用
1.创建
创建数据库

create database 数据库名;

创建表

create table 表名(属性名 属性类型 primary key auto_increment,//可实现自增功能
 id char(10) unique,//值唯一的定长字符串
  name varchar(10), //最大长度为10的可变长字符串);

2.修改
修改表

alter table 原表名 rename to 新表名;//修改表名
alter table student add 新属性名 类型;//添加新属性列
alter table student modify 属性列名 新类型;//修改属性的类型
alter table student change email address char(50);//修改多个属性的类型
alter table teacher drop constraint 约束条件名;//删除约束条件
alter table teacher add constraint 约束条件名 check(Tno between 100 and 999);//添加约束条件

修改表中数据

update stu set id=6;//修改所有行的属性值
update student set Sage='20' where Sname='lihua' ;//修改某行属性值

3.删除
删除表或数据库

 drop database db2;//删除数据库
drop table user;//删除表

删除表中的数据

delete from stu;//删除表中所有数据,但表仍存在
delete from stu where name='光明';//删除表中某行数据

4.查询
查询表

show databases;//查询所有数据库
show tables;//查询所有表
select * from stu1;//查询某一个表

查询表中数据

select * from stu1 where age=22;//查询表中满足条件的数据
select * from stu where time between '2000-1-1' and '2005-1-1';//查询表中某范围的数据
select * from stu1 where age in (18,20,22);//查询表中某范围的数据
select * from stu1 where name like '%e%';//查询表中匹配字符的数据,下划线匹配单个字符,%匹配多个字符
select * from stu1 order by age asc;//按升序排列查询
select * from stu order by math desc;//按降序排列查询
select * from stu order by math desc,english asc;//前一相同后一才起作用
select * from stu group by sex;//分组查询信息
select * from stu group by sex having count(*)>2;//having-分组后过滤,可以对聚合函数进行判断
select count | max | min | avg | sum(id) from stu;//聚合函数查询,null不参与聚合函数的计算
select * from stu limit 开始索引,每页查询条数;//页码查询

注意:在进行任何有关表的操作之前,或者退出后重新进入软件并开始要对表进行操作,要首先选择要操作数据库,执行这一行代码:

use 数据库名;

二、使用workbench查询表格信息
1.快速查表按钮
2.sql查询语句查表
如果两种方式都显示不出表格信息,那可能是因为你的表格结果显示页面被输出显示页面覆盖了,别问我为啥知道,踩这样的坑谁懂……
调整显示页面即可见:
使用workbench写SQL语句-初级_第1张图片
希望能给你们避避坑!!!

你可能感兴趣的:(sql,数据库,mysql)