完全转载自:http://blog.sina.com.cn/s/blog_67aaf4440100v01p.html

--创建数据库

create database Etp;

--连接数据库

connect to Etp;

--断开连接

disconnect Etp;

--查看当前数据库下有哪些表

list tables;

--建表

create table studentInfo(

 stuno char(5) not null,

 stuname varchar(8),

 stubirth date

);

--查看表结构

describe table studentinfo;

--新增表字段

alter table studentinfo add stutel int;

alter table studentinfo add abc int;

--修改字段类型

alter table studentinfo alter column stutel set data type
char(11);

--删除字段

alter table studentinfo drop column abc;

--增加一个非空约束

alter table studentinfo alter column stuname set not null;

--重构表

reorg table studentinfo;

--增加一个唯一约束

alter table studentinfo alter column stutel set not null;

alter table studentinfo add constraint un_stutel
unique(stutel);

--添加检查约束

alter table studentinfo add column stuAge int;

alter table studentinfo add constraint ch_stuAge check(stuAge
> 0 and stuAge <150);

--添加主键约束

alter table studentinfo add constraint pk_stuno primary
key(stuno);

--删除表

drop table studentinfo;

--创建表的同时添加约束方式1

create table studentinfo(

 stuNo int not null,

 stuName varchar(8) not null,

 stuAge int,

 stuTel char(8),

 constraint pk_stuNo primary key(stuNo),

 constraint un_stuName unique(stuName),

 constraint ch_stuAge check(stuAge
>=0 and stuAge <150)

);

--创建表的同时添加约束方式2

create table studentinfo(

 stuNo int not null primary key,

 stuName varchar(8) not null unique,

 stuAge int check(stuAge >=0 and
stuAge <150),

 stuTel char(8)

);

--添加主外键

--新增班级表

create table classInfo(

 classId int not null primary key,

 className varchar(20)

);

--建表的同时添加外键

create table studentinfo(

 stuNo int not null,

 stuName varchar(8) not null,

 stuBirth date not null,

 stuAge int,

 stuTel char(8),

 fclassId int,

 stuBirth date not null,

 constraint pk_stuNo primary key(stuNo),

 constraint un_stuName unique(stuName),

 constraint ch_stuAge check(stuAge
>=0 and stuAge <150),

 constraint fk_fcalssId foreign key(fclassid)
references classInfo(classId)

);

-- 自增

create table studentinfo(

 stuNo int not null generated always as
identity(start with 1 ,increment by 1),

 stuName varchar(8) not null,

 stuAge int,

 stuTel char(8),

 fclassId int,

 stuBirth date not null,

 constraint pk_stuNo primary key(stuNo),

 constraint un_stuName unique(stuName),

 constraint ch_stuAge check(stuAge
>=0 and stuAge <150),

 constraint fk_fcalssId foreign key(fclassid)
references classInfo(classId)

);

--先建表再添加外键

alter table studentinfo add constraint fk_classId foreign
key(fclassid) references classInfo(classId);

--从系统表中查询约束名

select constname, tabname, refkeyname, reftabname, colcount,
deleterule, updaterule from syscat.references;

--插入

insert into classinfo values(1,'ETP-1');

insert into studentInfo
values(1,'xy',20,'12345',1,'1990-02-28');

--不是全部插入则需要写列名

insert into studentinfo(stuNo,stuName,stuTel)
values(2,'wj','111');

-- 有自增长的列要写清楚列名

insert into studentinfo(stuName,stuAge,stuTel,fclassid,stuBirth)
values('xy',20,'12345',1,'1990-02-28');

insert into studentinfo(stuName,stuAge,stuTel,fclassid,stuBirth)
values('tom',22,'12345',2,'1990-02-28');

--更新

update studentinfo set stuBirth = '1990-02-21' where
stuName='xy';

update studentinfo set stuBirth = '1990-02-21',stuAge = 21 where
stuName='xy';

--删除

deleted from studentinfo where stuName='xy';

--查询

select * from studentinfo where stuName='xy';

select stuName,stuAge from studentinfo;

--别名查询

select stuName as 姓名,stuAge as 年龄 from studentinfo;

select s.stuName as 姓名,s.stuAge as 年龄 from studentinfo s;

--运算查询

select s.stuName as 姓名,s.stuAge+5 as 年龄 from studentinfo s;

--串联运算查询

select stuName||stuAge from studentinfo;

--and 和 or

select s.stuName as 姓名,s.stuAge+5 as 年龄 from studentinfo s where
s.stuName='xy' and s.stuAge=20;

select s.stuName as 姓名,s.stuAge+5 as 年龄 from studentinfo s where
s.stuName='xy' or s.stuAge=20;

--null

select * from studentinfo where stuAge is
null; 

select * from studentinfo where stuAge is not
null;

--between and 包括边界 相当于>=和<=s

select s.stuName as 姓名,s.stuAge+5 as 年龄 from studentinfo s where
s.stuAge between 10 and 20

--in

select * from studentinfo  where stuName in
('xy','wj');

select * from studentinfo  where stuName not in
('xy','wj');

--模糊查询 like%,%表示多个字符

select * from studentinfo where stuName like 'x%'

--模糊查询 like_ , _表示单个字段

select * from studentinfo where stuName like 'x_';

--排序 order by

select * from studnetinfo order by fclassid desc;

select * from studnetinfo order by fclassid asc;

--distinct去掉重复

select distinct stuAge as 年龄 from studentinfo;

--group by,使用的时候,select 后面只能加2种字段: 1.group by 后面出现的,2.聚合函数

select fclassId as 班级号,count(stuName) as 学生个数 from studentinfo
group by fclassid;

-having 在分组的基础上过滤,出现顺序where-group by-having

select fclassId as 班级号,count(stuName) as 学生个数 from studentinfo
group by fclassid having count(StuName)>=2

DB2中SQL基本语句的操作的更多相关文章

  1. db2 中 SQL判断物理表是否存在、修改表名

    1.db2 中 SQL判断物理表是否存在 SELECT * FROM SYSIBM.SYSTABLES WHERE TID <> 0 AND Name = 'TABLE_NAME' AND ...

  2. SQL万能语句-经典操作

    一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...

  3. oracle中sql查询语句的执行顺序

    查询语句的处理过程主要包含3个阶段:编译.执行.提取数据(sql查询语句的处理主要是由用户进程和服务器进程完成的,其他进程辅助配合) 一.编译parse 在进行编译时服务器进程会将sql语句的正文放入 ...

  4. Java连接MySQL数据库实现用户名密码的验证方法 Java语句中sql查询语句'' ""作用

    //方法一,可以验证登录,但方法不实用.package com.swift; import java.sql.Connection; import java.sql.DriverManager; im ...

  5. PHP中SQL查询语句的id=%d解释

    在SQL语句中有一些写的是这样的: 'SELECT id FROM dbname WHERE xx_id = %d;', $bl['student_id'] 其中的“xx_id = %d”,这里的%d ...

  6. 嵌套SQL语句訪问DB2中SQLCA的调用技巧

    在IBM的关系型数据库产品DB2中,使用SQL Communication Area(SQLCA)将程序中嵌套的SQL语句执行情况返回给程序. 在程序中有针对性地对SQLCA实施调用,可对程序中各类S ...

  7. EF Core中执行Sql语句查询操作之FromSql,ExecuteSqlCommand,SqlQuery

    一.目前EF Core的版本为V2.1 相比较EF Core v1.0 目前已经增加了不少功能. EF Core除了常用的增删改模型操作,Sql语句在不少项目中是不能避免的. 在EF Core中上下文 ...

  8. DB2常用sql语句

    转 DB2 提供了关连式资料库的查询语言sql(structured query language),是一种非常口语化.既易学又易懂的语法.此一语言几乎是每个资料库系统都必须提供的,用以表示关连式的操 ...

  9. 关于SQL中的Update语句

    今天在SQL数据库操作时需要将一张表中的数据Update到另一张表中去, 可是用我以往的写法确怎么也不能成功.代码如下: update table1 a set a.Col1=b.Col2 from ...

随机推荐

  1. RDS 事务型数据库sql

    -- 替换json中数据 select SUBSTRING_INDEX(SUBSTRING_INDEX('[{"channelCode":"MOBIL",&qu ...

  2. 联赛模拟测试17 A. 简单的区间 启发式合并

    题目描述 分析 我们要找的是一段区间的和减去该区间的最大值能否被 \(k\) 整除 那么对于一段区间,我们可以先找出区间中的最大值 然后枚举最大值左边的后缀与最大值右边的前缀之和是否能被 \(k\) ...

  3. msyql查看连接数

    连接数 SHOW FULL PROCESSLIST 1.  查看允许的最大并发连接数 SHOW VARIABLES LIKE 'max_connections'; 2.  修改最大连接数 方法1:临时 ...

  4. Ubuntu服务安装

    一.ifconfig命令安装 sudo apt install net-tools 二.ssh服务安装 sudo apt-get install openssh-server netstat -ltn ...

  5. 第二十七章 Linux系统管理之定时任务

    一.定时任务概述 1.含义:设定某个日期或时间周期性执行指令. 2.crond # 守护进程 分钟级别 rond是Linux系统中用来定期执行命令或脚本的一种服务软件,一般情况下,我们安装完CentO ...

  6. Jmeter入门(4)- 注意事项

    一.中文乱码问题的解决方法 1. 将HTTP请求的内容编码改成UTF-8 2. 修改配置文件jmeter.properties 将jmeter安装目录的bin目录下的jmeter.properties ...

  7. 【设计模式】第一篇:概述、耦合、UML、七大原则,详细分析总结(基于Java)

    迷茫了一周,一段时间重复的 CRUD ,着实让我有点烦闷,最近打算将这些技术栈系列的文章先暂时搁置一下,开启一个新的篇章<设计模式>,毕竟前面写了不少 "武功招式" 的 ...

  8. 痞子衡嵌入式:基于恩智浦i.MXRT1010的MP3音乐播放器(RT-Mp3Player)设计

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是基于i.MXRT1011的MP3播放器参考设计. i.MXRT1011是恩智浦i.MXRT四位数系列的入门型号,虽然是入门级,可也是50 ...

  9. 数据结构(C++)——链栈

    结点结构 typedef char ElemType; typedef struct LkStackNode{ ElemType data; LkStackNode *next; }*Stack,SN ...

  10. D. Bash and a Tough Math Puzzle 解析(線段樹、數論)

    Codeforce 914 D. Bash and a Tough Math Puzzle 解析(線段樹.數論) 今天我們來看看CF914D 題目連結 題目 給你一個長度為\(n\)的數列\(a\), ...