DataType数据库字段类型
数据中字段有很多类型以下只是常用的。
类型 | |
---|---|
int | 整数 |
varchar | 字符串 |
tinyint | 数值布尔 |
表的特殊标识
当创建表的时候会有一写特殊标识可供选择:
标识符 | |
---|---|
PK | 主键唯一标识符 |
UQ (Unique) | 值唯一,不能出现重复 |
AI (auto increment) | 自动增长 |
NN (not null) | 值不允许为空 |
SQL语法
select语句
-- 从一个表中查询所有数据
# select * from 表名称
-- 从指定的表里查询指定的列名
#select 列名称 from 表名称
-- 通过星号获取users表中所有的数据
select * from users
-- 从users表中获取 username 和 password 的数据
select username,password from users
insert into语句
向表中插入数据。
-- 向表中插入数据
# insert into 表名(列1,列2...) values (值1,值2...)
-- 向user表中插入 username为tony stark,password为 098123的用户
insert into users(username,password) values ('tony stark','098123');
update语句
用于更新数据
-- 语法解读:
-- 1.用update 指定更新那个表中的数据
-- 2.用set 指定列对应的值
-- 3.用where 指定更新条件
# update 表名 set 列名=新值 where 条件
-- 把用户id为1的密码修改为 666666
update users set password='ls666666' where id=1;
-- 把用户id为2的用户密码和状态更新为 admin123 和 1
update users set password='admin123',status=1 where id=2;
delete from语句
用于删除数据
-- 用于删除
delete from 表名 where 条件
-- 删除users中id为5的用户
delete from users where id=5;
where 子句
where作用限定选择的标准
# 查询语句 中的 where
select 列名称 from 表名称 where 列 运算符 值
# 更新语句中的where
update 表名 set 列=新值 where 列 运算符 值
# 删除语句中的 where 条件
delete from 表名 where 列 运算符 值
where子句中的运算符
= 等于
<> != 不等于
‘>’ 大于
‘<’ 小于
‘<=’ 小于等于
‘>=’ 大于等于
between 在某个范围
like 搜索某种模式
演示where字句的使用
-- where字句的使用
-- 查询状态等于1的用户
select * from users where status=1