美少女Java实训笔记03

来源:互联网 发布:合理避税 知乎 编辑:程序博客网 时间:2024/04/27 16:19

 








              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