美少女Java实训笔记03

 








              sql基础      

---------------------------------------------------------

【1】、说明:创建新表

create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

【2】、根据已有的表创建新表: 

Acreate table tab_new like tab_old (使用旧表创建新表)

Bcreate table tab_new as select col1,col2… from tab_old definition only

【3】、说明:删除

DELETE FROM EMPLOYEES

 

   WHERE BRANCH_OFFICE = 'Los Angeles';

【4】、说明:插入一个列
INSERT  INTO 表名 VALUES (字段对应值1,字段对应值2,....);

列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。
[5]、说明:添加主键: 

              CREATE  TABLE  tb
              (
              id  INT  IDENTITY(1,1)  PRIMARY  KEY ,
              name  VARCHAR (20)
              )
 
[6]、说明:创建索引
create [unique] index idxname on tabname(col….) 
删除索引 drop index idxname
注:索引是不可更改的,想更改必须删除重新建。

7、更新语句
语法:
update  表名 set 字段名=更新的值 where 条件;
例如:我们将上面user表中第一条记录中的密码改成222222,语句如下:
update user set passWord='222222'where id=1;
8.1:给指定条件查询:
语法:
select *from 表名 where 条件;
例如我们要查询user表中id<4的字段值,语句如下:
select *from user where id<4;
10、distinct:

              select distinct *from user

              查询user表中的所有字段的值都一样只显示一条记录

              select distinct password from user


              查询user表中的password字段值相同的只显示一条记录

              select distinct username,passwordfrom user;

              查询user表中username和password字段值都相同的只显示一条记录。

10、条件

       10.1: is null 和is not null

       null是一种状态,不是里面的字符串为空。

通配符:

10.2,% :表示任意0个或多个字符。可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)示。

比如 SELECT * FROM [user] WHERE u_name LIKE '%三%'

10.7:排序

ORDER BY

asc desc 当对多个字段排序时,只作用前面的一个字段。

order by :排序字段可以查询字段的别名,先查后排序,where条件中不能用查询字段的别名。

SELECT *FROM emp ORDER BY deptno ASC,sal DESC;

对emp 表 中两个字段排序,先对deptno的值进行升序排列,在先对deptno字段的值排序好的基础上再对sal字段的值进行降序排列

11、模糊查询:

关键字LIKE

例如查询user表中userName字段值最后一个字符是‘东’的,语句如下:

select *from user from userName LIKE ‘%东’;

例如查询user表中userName字段值中包含'东'的,语句如下:

select *from user from userName LIKE ‘%东%’

;注:"%"表示零个或者多个字符.我这里只介绍了模糊查询中的一种通配符的使用,具体的看上面的通配符使用。



[12]、说明:几个简单的基本的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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值