MySQL基础语法一

1、创建数据库

#创建数据库 ruozedata
create database ruozedata;

2、创建用户并刷新用户权限

grant all privileges on ruozedata.* to ruoze@'%' identified by '123456';
flush privileges;

3、基本权限查看

#查看数据库
show create database ruozedata;
#查看表结构
show create table stuinfo;
#查看用户权限
show grants for ruoze@'%';

4、MySQL字段类型

数字类型:int 整数,long 长整数、float 单精度 、double 双精度 、decimal 小数值
字符串’abc’:char 定长字符串 0-255字节、varchar 变长字符串 0-65535字节、text
日期和时间:
date YYYY-MM-DD 2019-09-10
time HH:MM:SS 10:10:10
datetime 2019-09-10 10:10:10
timestamp 2019-09-10 10:10:10

5、增删改查insert、update、delete、select

insert into ruozedata(id,name,age) values(1,‘rz’,18)
update ruozedata set age=22 where id=1;
delete from ruozedata where id=1;
select * from ruozedata;

#举例增删改查的实际应用
create table ruozedata(
id int ,
name varchar(200),
age int
)

insert into ruozedata(id,name,age) values(1,'rz',18)
update ruozedata set age=22 where id=1;
delete from ruozedata where id=1;
select * from ruozedata;

6、语法
DDL: 数据定义语言 create drop alter
DML: 数据操作语言 insert update delete select
DCL: 数据控制语言 grant

#删除表
drop table ruozedata;
#创建表
create table 表名()...
#执行结果
select * from ruozedata;
#约束
 default
#1/主键 primary key 简写pk  一张表就只能一个主键==非空约束+唯一
#2/主键是唯一约束的变态升级
#3/主键或唯一约束可以多个字段设置,根据数据的特性
ALTER TABLE ruozedata.studentinfo ADD CONSTRAINT studentinfo_un UNIQUE KEY (num,name,...) ;数据不能存在多条重复
#多列
update ruozedata.studentinfo set name='huhu2',age=19 where id=3;
# id一定要自增长 非业务字段。
#创建人/创建时间/修改人/修改时间,任何的数据设计时都需要进行时间、人的记录。
#字段命名统一风格,常用的就是英文单词加上下划线组合,字母通常是3/4个,如果已建表看表的规则。
#不要将汉语拼音全拼或者缩写作为字段
#不要将中文作为字段名称
--------------------------------------
示例:
create table ruozedata(
id int auto_increment primary key,
stu_number int,
stu_name varchar(200),
stu_age int ,
createtime timestamp default current_timestamp, 
#createtime创建时间  timestamp字段类型  default current_timestamp默认值当前时间
cretae_user varchar(100),
update_time timestamp default current_timestamp on update current_timestamp,
#on update current_timestamp当表的数据发生变化,字段会自己做时间更新
update_user varchar(100)
)

关于where条件
生产上 update 切记是否要加where条件
生产上 delete 切记是否要加where条件
update ruozedata.studentinfo set age=28 where num=1;
update ruozedata.studentinfo set age=19;

update不加where的情况
1 1 jepson 19 2019-06-26 22:14:19 2019-06-26 22:24:17
2 2 ruoze 19 2019-06-26 22:20:10 2019-06-26 22:24:17
3 3 huhu 19 2019-06-26 22:20:10 r 2019-06-26 22:24:17

(1)插入数据

insert into 表名(字段名1,字段名2,……)
values(值1,值2,……)
示例:
insert into ruozedata.stuinfo(num,name,age) 
values(1,'ruoze',12,……);

(2)更改数据

update 表名 set 字段名=新值 
示例:
update stuinfo set age=15 where id=1;

(3)查询数据

# > < =
select * from ruozedata.studentinfo where id>=1; 
select * from ruozedata.studentinfo where name='ruoze1'; 
select * from ruozedata.studentinfo where name!='ruoze1'; 

查询年龄26岁或名称ruoze1的数据,or表示或的意思
查询年龄26岁且名称ruoze1的数据,and表示或的意思
查询ruoze1和jepson之间年龄为26的
select * from ruozedata.studentinfo where age=26 or name=‘ruoze1’;
select * from ruozedata.studentinfo where age=26 and name=‘ruoze1’;
select * from ruozedata.studentinfo where age=26 and (name=‘ruoze1’ or name=‘jepson’);
模糊查询
查询含有r字母的
select * from ruozedata.studentinfo
where name like ‘%r%’;
查询首字母为ruo的
select * from ruozedata.studentinfo
where name like ‘ruo%’;
查询最后字母
select * from ruozedata.studentinfo
where name like ‘%1’;
查询第三个字母为o,前两个字母用占位符__
select * from ruozedata.studentinfo
where name like ‘__o%’;

(4)删除数据

delete from ruozedata where id=1;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值