2018-11-21_SQL学习笔记

distinct选择不同的内容

select distinct id from tablename;查询不重复的id

select count(distinct id) from tablename 查询不重复的id的个数

SQL在文本只周围使用单引号(大部分也支持双引号)

数值字段不实用引号

select id from tablename where name like 'guo*'  查询名字是guo开头的id

select * from tablename where not id = '001' 查询id不为001的数据

select  * from tablename where id='001' and (name='guo' or name = 'chen') 以后通过括号进行套用

order by 关键字默认按照升序排列

insert into table (column,column1,column4) values(value,value1,value4)  可以只插入部分值

insert into table values (value,value1,value2....) 插入所有的列,而且要一一对应

NULL代表的是一种状态,不是一个值,代表这个地方是空的,什么都没有

当我们给表中的几个字段插入数据以后,剩下的没有插入数据的字段是自动插入NULL

判断是否为null的办法,   is null  或者 is not  null

在我们创建表的时候,可以设置某些字段是not null,就是必须插入值

在这里要提醒一下  not null 和 primary key 都必须插入值 否则报错

一般情况下 查询出来的null显示的是空白  不是NULL

update table set column1 = value1,volumn3 = value3 where condition

注意一定要使用where或者其他限制条件,否则所有的数据都会被update

注意点:主键不能直接update,因为主键在生成的时候已经校验过重复了,不允许在修改的时候出现重复

delete from table 删除表中所有数据

delete * from table 也是删除所有数据

select * from table limit 5   选择前五条记录

select top 5 * from table   选择前五条记录  

oracle中使用 rownum <= 3  Oracle中查询5条数据

select top 50 percent * from table 查询50%的数据

where name like 'A%'  name是A开头的数据

where name like '%B'  name是B结尾的数据

where name like '%guo%'  在任何位置包含guo的值

where name like '_r%'  name的第二个字段是r的值

where name like 'a_%_%'  代表a开头的最少三个字段的值

where name like '[a-c]%' a或b或c开头的值

where name like '%[!bmp]' 不是b或m或p结尾的值

select * from customers where county in ('germany',;Japan;,'Tokoy');

select * from custumoers where county in (select county from user)

between 可以比较数字,文本或日期

where money between 10 and 20

where name between 'guo' and 'chen' name是guo或者chen的人

between在不同的数据库中不同,不一定代表

Oracle中 number表示数字类型,varchar和varchar2表示字符串类型

Mysql中Int Integer表示不带小数的数字,float doublenumeric表示小数

切记,不同的数据库的数据类型是有差异的,一定要根据实际情况来使用

alter table t00_user add  (columnname,datatype) 为user表增加一个int类型的id列

alter table t00_user drop column columnname 去掉t00_user表中的某个列

select username as name from t_user    使用as的时候后面的值不需要用引号

只有创建unique index的时候才会限制重复

视图的有效期:只要引用的原表存在就会一直保存

临时表在回话结束的时候系统就会自动删除

inner join (内连接,就是两个表的公共部分) 表示至少两个表中要有一个匹配,则返回行

left join 即使右表中没有匹配,也返回左边所有行

full join 只要其中一个表中存在匹配,则返回行

full outer join 返回两个表中的所有数据

union 是列出两个表中不重复的数据

union是列出两个表中的所有数据

select * into newtable from table where 0=1;  创建一个同样的新表

select * into newtable from table;

insert into newtable select * from talbe;

alter table table_name drop index index_name;删除掉目标表的指定索引

drop index index_name 删除索引

truncate table table_name  清空表中的数据

drop table table_name  删除掉整张表

create table chen (id int not auto_increment ,name varchar(255) not null,ip varchar(255),primary key (id))

每当用户查询view中的数据时,数据库会默认从表中查看最新数据

猜你喜欢

转载自blog.csdn.net/g418572664/article/details/84322039
今日推荐