【MySQL】使用SQL语句进行数据简单操作

1.创建数据库

CRATE DATABASE  mydemo; //创建数据库,库名为mydemo

2.创建表

create table score( //创建一个表 表名为score
id int primary key auto_increment,    
//变量名id 数据类型int(整数型) 主键 primary key 自增auto_increment
uname varchar(50) not null,  not null//非空,不为空
upwd varchar(50) not null,//``普通单词,避免出现关键字
gendar varchar(10)
);
 
**//执行建表语句选择当前数据库**
**建表语法**

3.插入数据

// id=用户编号,// uname=用户名字

// upwd=密码//gendar =性别

// score = 分数// registdata = 注册日期

insert into 表名(列名1,列名2,列名3....) values//值 (值1,值2,值3...)
**/*id设为主键,要求插入数据不能为空值,不能重复**
insert into score(id,uname,upwd,gendar) values (1000,'zs','11111','男')
/*性别列为空,所以插入数据不能外传*/
insert into score(uname,upwd) values ('zs','11111','男')
**/*id设置为自增,插入数据可以不用管,mysql会自动进行插入,并将值自动递增**
insert into score(uname,upwd) values ('zs','11111');
**/*插入数据有日期类型,, (五个参数给对应的五个值)..字符串使用但单引号 日期 now属于当前时间**
insert into score(uname,upwd,gendar,score,registdate) values ('zs','11111','男',‘9.8’,now());

4.修改数据

/修改score表中所有分为6分, set=修改

update score  SET  score =9.9

/修改 zs3 分数为9.8

update score  SET  score =9.8 where uname = 'zs3';

/同时修改多个值; 修改zs2密码为22222,分数为10

update score set upwd='22222',score=10 where uname='zs2'

5.删除数据

1)–删除所有数据 [慎用]

delete from 表名;

如 delete from score;

2)–删除符合条件(where 列名= ? 那个列名)的数据 where属于条件

delete from score where id=1001  按条件列名去删除

delete from score where uname = 'zs4'

3)–清空表

truncate table score;

6.查询数据

1)查询所有数据

//查询score表 *号表示表里所有的列(建议不要使用 *号,用具体的列名)

select * from score;

2) 查询所有数据 ,指明所需要查询的列,查询所有数据的姓名分数

select uname,score from score;

3)单个条件查询:查询姓名为zs1的数据

select * from score where uname='zs1'

4)多个条件查询:查询性别为男并分数大于6的数据 ((and=和 or=或))

select * from score where gendar='男' and score>6;

5) 排序查询:根据注册时间倒叙排序 默认升序 asc 倒叙是desc

// 根据什么来排序= order by desc=倒序

//查询分数表的注册时间列变为倒叙排序

select * from score order by  registdate desc;  

6)模糊查询 : 查询姓名中包含x的数据

%=任意个数字符的占位 ,_下划线 是一个字符的占位 like约等于=的意思 模糊查询**

select * from score where uname like '%x%';

7)查询一个表的总数 总条数

select count (*) from score;

8)分页查询

select * from score limit 起始位置从0开始,查询的数据条数;

(第一页)

select * from score limit 0,2;

(第二页)

select * from score limit 2,2;

7.简单的基本的SQL语句

选择:select * from table1 where 范围


插入:insert into table1(field1,field2) values(value1,value2)


删除:delete from table1 where 范围


更新:update table1 set field1=value1 where 范围


查找:select * from table1 where field1 like ’%value1%’ ---like 模糊查询


排序:select * from table1 order by field1,field2 [desc]


总数:select count as totalcount from table1


求和:select sum(field1) as sumvalue from table1


平均:select avg(field1) as avgvalue from table1


最大:select max(field1) as maxvalue from table1


最小:select min(field1) as minvalue from table1








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值