SQL语句1

建立书写sql语法的编辑器

sql语法是来执行运行navicat的数据库  删减 新增 

如何建立:在需要被执行的数据库的查询入口 就可以进行书写sql语法了

 

常用语法

注释 ctrl / 

取消注释 ctrl shift /

创建表  create table (表的名字必须是英文或者拼音

             字段名 类型 约束,

             字段名......)

注 id这段书写顺序是不能乱的先是id 类型 无符号 主键 自动递增

creata table studeate(
id int unsigned primary kry auto_increment name varchar (10),
age int Unsigned,
height declmal(5,2) )

 

增加数据

下面展示给所有字段设置数据,给指定的字段设置数据,一条语法里设置多个字段数据,三条语法

注:数值需要与前面的字段相匹配,不能不按顺序

        遇上varchar的类型的字段,都需要加上单引号

insert into root values(数值1,'数值2',数值3....)
insert into root(name,age) values(数值1,'数值2',数值3....)
insert into root values(数值1,'数值2',数值3....),(数值1,数值2,数值3....)

 

增加字段

alter table 表名 add 字段名 类型

 

删除表

1 表不存在的话就会报错

2 表不存在的话也不会报错

drop table 表名
drop table if 表名

 

 

修改与删除数据

下面语法意为修改root表的id为2的名字改为垃圾,id改为18

update root set name='垃圾',id=18 where id=2
delete from root where id=18

上面语法意为删除root表的id为18的所有字段数据

 

 

去重

去掉该字段所有数值的重复数值,保留唯一

关于去重可以灵活使用,可以用在所有的数据去重也可以用在个别字段的去重

select distinct 字段名 from 表名

 

查询数据

第一个意为name为xx的所有数据显示出来,去掉字段,则该表所有数据出现

第二个意为查询root表的字段为age和name的字段的所有数据,如果要去重在select后面加上distinct就可

第三个意为查询root表的名字叫纷纷的年龄,如果需要查询多个纷纷的其他数据,后面加上字段或者就写个*

改查询出来的数据里的字段名的格式为:select 旧字段名 as 新字段名(可中文),旧字段名 as 新字段名(可中文)from 表名

改查询出来的数据里的表名的格式为:

select*from root name=xx
select age,name from root
select age from root where name='纷纷'

 

模糊查询

*like 可灵活使用

*%表示任意多个字符

*_表示一个任意字符

第一个为查询孙开头任意一个字符的信息

第二个为查询孙开头任意多个字符的信息

第三个为查询带孙字的多个信息

select*from root where name like '孙_'
select*from root where name like '孙%'
select*from root where name like '%孙%'

 

范围查询

小值必须在前面

第一条是比较运算的方式

第二条是更加简洁的方式

select*from root where age>=18 and age<=20
select*from root where age between 18 and 20

 

空判断

注:null与空字符串有数据但没显示,是不同的

第一条为没填写年龄的所有信息

第二条为写了年龄的所有信息,如果有写过但删除了这个也会显示出来,这就是与null的区别

select*from root where age is null
select*from root where age is not null

 

 

比较运算

*小于   <     

*小于等于<= 

*大于 > 

*大于等于  >=   

*等于   =   

*不等于!=或者<>

第一个意为查询该表的年龄小于54的数据

第二个意思为查询该表名字不是桂花的所有数据

select*from root where age<54

select*from root where name!='桂花'或者
select*from root where name<>'桂花'
 

 

逻辑运算

*and

*not

*or   这个也可以用in来替代 例如where name in('数值1','数值2','数值3')

第一个为查询名字为桂花且年龄12的信息

第二个为查询名字为桂花或者年龄12的满足一个条件的全部信息

第三个为查询不是叫桂花的信息

select*from root where name='桂花' and age=12
select*from root where name='桂花' or age=12
select*from root where not name='桂花'

 

逻辑删除

例如客户需要注销,后台会标记该数据被删除,客户重新注册的时候,数据可以找回,不能使用delete否则找不回数据

1   首先先添加个字段isdelete int      2  再设置数值为0或者1,0代表未删除,1代表已删除 

UPDATE root SET isdelete=0  设置全部为0
UPDATE root set isdelete=1 where id=3  再设置需要被删除的id为1

 

 

 

posted @ 2020-11-03 22:55  旺仔姐姐啊  阅读(112)  评论(0编辑  收藏  举报