首页 > 数据库 >db2 创建数据库、表、存储过程等

db2 创建数据库、表、存储过程等

时间:2023-04-26 19:37:15浏览次数:35  
标签:存储 OFF 数据库 command user cont db2 id


启动

[root@localhost ~]# su - db2inst1
[db2inst1@localhost ~]$ db2start

SQL8007W  There are "13" day(s) left in the evaluation period for the product 
"DB2 Connect Server". For evaluation license terms and conditions, refer to 
the License Agreement document located in the license directory in the 
installation path of this product. If you have licensed this product, ensure 
the license key is properly registered. You can register the license via the 
License Center or db2licm command line utility. The license key can be 
obtained from your licensed product CD.
SQL1063N  DB2START processing was successful.
[db2inst1@localhost ~]$



创建数据库

[db2inst1@localhost ~]$ db2 create database hibernate;
SQL1001N  "hibernate" is not a valid database name.  SQLSTATE=2E000



数据库名字应该是1-8个字符

[db2inst1@localhost ~]$ db2 create database hiber;
DB20000I  The CREATE DATABASE command completed successfully.



列出数据库

[db2inst1@localhost ~]$ db2 list db directory

 System Database Directory

 Number of entries in the directory = 3

Database 1 entry:

 Database alias                       = TESTDB
 Database name                        = TESTDB
 Local database directory             = /home/db2inst1-m
 Database release level               = d.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =

Database 2 entry:

 Database alias                       = HIBER
 Database name                        = HIBER
 Local database directory             = /home/db2inst1-m
 Database release level               = d.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =

Database 3 entry:

 Database alias                       = SAMPLE
 Database name                        = SAMPLE
 Local database directory             = /home/db2inst1-m
 Database release level               = d.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =

[db2inst1@localhost ~]$



连接数据库

[db2inst1@localhost ~]$ db2 connect to hiber;

   Database Connection Information

 Database server        = DB2/LINUX 9.7.6
 SQL authorization ID   = DB2INST1
 Local database alias   = HIBER

[db2inst1@localhost ~]$



列出表

[db2inst1@localhost ~]$ db2 list tables;

Table/View                      Schema          Type  Creation time             
------------------------------- --------------- ----- --------------------------

  0 record(s) selected.

[db2inst1@localhost ~]$



创建表

[db2inst1@localhost ~]$ db2 "create table tb_user(id integer primary key, user_name varchar(10) not null, user_age integer not null)"
DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0542N  The column named "ID" cannot be a column of a primary key or unique 
key constraint because it can contain null values.  SQLSTATE=42831
[db2inst1@localhost ~]$



这个在oracle里是可以执行的,但是在db2上,primary key的字段一定要指明not null才行:

[db2inst1@localhost ~]$ db2 "create table tb_user(id integer not null primary key, user_name varchar(10) not null, user_age integer not null)"
DB20000I  The SQL command completed successfully.
[db2inst1@localhost ~]$




执行代码块

db2 -td@表示命令以@结束

[db2inst1@localhost ~]$ db2 -td@
(c) Copyright IBM Corporation 1993,2007
Command Line Processor for DB2 Client 9.7.6

You can issue database manager commands and SQL statements from the command 
prompt. For example:
    db2 => connect to sample
    db2 => bind sample.bnd

For general help, type: ?.
For command help, type: ? command, where command can be
the first few keywords of a database manager command. For example:
 ? CATALOG DATABASE for help on the CATALOG DATABASE command
 ? CATALOG          for help on all of the CATALOG commands.

To exit db2 interactive mode, type QUIT at the command prompt. Outside 
interactive mode, all commands must be prefixed with 'db2'.
To list the current command option settings, type LIST COMMAND OPTIONS.

For more detailed help, refer to the Online Reference Manual.

db2 => begin atomic 
db2 (cont.) =>  declare v_id int;
db2 (cont.) =>  declare v_name varchar(10);
db2 (cont.) =>  declare v_age int;
db2 (cont.) =>  set v_id=1;
db2 (cont.) =>  while (v_id<=100) do
db2 (cont.) =>    insert into tb_user values(v_id, 'user_' || v_id, v_id);
db2 (cont.) =>    set v_id=v_id+1;
db2 (cont.) =>  end while;
db2 (cont.) => end @
DB20000I  The SQL command completed successfully.
db2 =>



取得前N条数据

db2 => select * from tb_user fetch first 10 rows only@

ID          USER_NAME  USER_AGE   
----------- ---------- -----------
          1 user_1               1
          2 user_2               2
          3 user_3               3
          4 user_4               4
          5 user_5               5
          6 user_6               6
          7 user_7               7
          8 user_8               8
          9 user_9               9
         10 user_10             10

  10 record(s) selected.

db2 =>



清空表truncate


db2 => truncate table tb_user immediate@
DB20000I  The SQL command completed successfully.



这跟oracle不同


存储过程

创建

db2 => create procedure pro_insert_user(in row_count integer)
db2 (cont.) => begin 
db2 (cont.) =>  declare v_id int;
db2 (cont.) =>  declare v_name varchar(10);
db2 (cont.) =>  declare v_age int;
db2 (cont.) =>  set v_id=1;
db2 (cont.) =>  while (v_id<=row_count) do
db2 (cont.) =>    insert into tb_user values(v_id, 'user_' || v_id, v_id);
db2 (cont.) =>    set v_id=v_id+1;
db2 (cont.) =>  end while;
db2 (cont.) => end @
DB20000I  The SQL command completed successfully.



调用


db2 => call pro_insert_user(10)@

  Return Status = 0




控制语句if else


db2 => create procedure pro_insert_user(in row_count integer)
db2 (cont.) => begin
db2 (cont.) =>  declare v_id int;
db2 (cont.) =>  declare v_name varchar(10);
db2 (cont.) =>  declare v_age int;
db2 (cont.) =>  declare v_count int;
db2 (cont.) =>  declare v_cnt int;
db2 (cont.) =>  set v_count = 1;
db2 (cont.) =>  set v_cnt = 0;
db2 (cont.) =>  select count(id) into v_cnt from tb_user;
db2 (cont.) =>  if (v_cnt >= 1) then
db2 (cont.) =>    select max(id)+1 into v_id from tb_user;
db2 (cont.) =>  else
db2 (cont.) =>    set v_id = 1;
db2 (cont.) =>  end if;
db2 (cont.) =>  while (v_count<=row_count) do
db2 (cont.) =>      select cast(rand()*100 as int) into v_age from sysibm.sysdummy1;
db2 (cont.) =>      insert into tb_user values(v_id, 'user_' || v_id, v_age);
db2 (cont.) =>      set v_id=v_id+1;
db2 (cont.) =>      set v_count=v_count+1;
db2 (cont.) =>    end while;
db2 (cont.) => end @
DB20000I  The SQL command completed successfully.
db2 =>





事务控制


默认是自动提交的,无须commit;


db2 => list command options@

     Command Line Processor Option Settings

 Backend process wait time (seconds)        (DB2BQTIME) = 1
 No. of retries to connect to backend        (DB2BQTRY) = 60
 Request queue wait time (seconds)          (DB2RQTIME) = 5
 Input queue wait time (seconds)            (DB2IQTIME) = 5
 Command options                           (DB2OPTIONS) = 

 Option  Description                               Current Setting
 ------  ----------------------------------------  ---------------
   -a    Display SQLCA                             OFF
   -c    Auto-Commit                               ON
   -d    Retrieve and display XML declarations     OFF
   -e    Display SQLCODE/SQLSTATE                  OFF
   -f    Read from input file                      OFF
   -i    Display XML data with indentation         OFF
   -l    Log commands in history file              OFF
   -m    Display the number of rows affected       OFF
   -n    Remove new line character                 OFF
   -o    Display output                            ON
   -p    Display interactive input prompt          ON
   -q    Preserve whitespaces & linefeeds          OFF
   -r    Save output to report file                OFF
   -s    Stop execution on command error           OFF
   -t    Set statement termination character       ON
   -v    Echo current command                      OFF
   -w    Display FETCH/SELECT warning messages     ON
   -x    Suppress printing of column headings      OFF
   -z    Save all output to output file            OFF

db2 =>



不要自动提交


db2 => update command options using c off@
DB20000I  The UPDATE COMMAND OPTIONS command completed successfully.
db2 => list command options@

     Command Line Processor Option Settings

 Backend process wait time (seconds)        (DB2BQTIME) = 1
 No. of retries to connect to backend        (DB2BQTRY) = 60
 Request queue wait time (seconds)          (DB2RQTIME) = 5
 Input queue wait time (seconds)            (DB2IQTIME) = 5
 Command options                           (DB2OPTIONS) = 

 Option  Description                               Current Setting
 ------  ----------------------------------------  ---------------
   -a    Display SQLCA                             OFF
   -c    Auto-Commit                               OFF
   -d    Retrieve and display XML declarations     OFF
   -e    Display SQLCODE/SQLSTATE                  OFF
   -f    Read from input file                      OFF
   -i    Display XML data with indentation         OFF
   -l    Log commands in history file              OFF
   -m    Display the number of rows affected       OFF
   -n    Remove new line character                 OFF
   -o    Display output                            ON
   -p    Display interactive input prompt          ON
   -q    Preserve whitespaces & linefeeds          OFF
   -r    Save output to report file                OFF
   -s    Stop execution on command error           OFF
   -t    Set statement termination character       ON
   -v    Echo current command                      OFF
   -w    Display FETCH/SELECT warning messages     ON
   -x    Suppress printing of column headings      OFF
   -z    Save all output to output file            OFF

db2 =>




标签:存储,OFF,数据库,command,user,cont,db2,id
From: https://blog.51cto.com/u_1002776/6228658

相关文章

  • 对数据库中存储的程序进行现代化改造,以使用 Amazon Aurora PostgreSQL 联合查询、pg_c
    作为数据库迁移和现代化的一部分,您可以继续使用存储的程序和调度作业,将远程实例中的数据整合到集中式数据存储中。 AmazonSchemaConversionTool(AmazonSCT)可帮助您将传统的Oracle和SQLServer函数转换为其等效的开源函数。但是,如何继续使用存储的程序从远程数据库中提取数......
  • Django 查询数据库不释放内存的情况
    查询结果未及时清空如果查询结果较大,可能会占用很多内存。在使用完查询结果后,应该及时清空,以释放占用的内存。可以通过将查询结果赋值给一个变量,然后使用del关键字删除变量来清空查询结果。例如:result=MyModel.objects.all()#使用查询结果...#清空查询结果delresul......
  • 数据库查询与前台请求后台接口返回结果id的值不一致
    数据库查询与前台请求后台接口返回结果id的值不一致主要是主键ID类似于时间戳的那种形式,前端获取接口返回后,最后几位变成了0了,网上搜了下,还有不少人也出现过这种情况。解决办法就是后台返回这个id,改成字符串类型,原因可能是int或者long在前端长度不够导致   ......
  • java连接jdbc-mssql数据库
    packagecom.swift.erp;importjava.sql.SQLException;importjava.sql.Statement;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;publicclassAPP{//引入sqljdbc.jarpublicstaticvoidmain(String[]args......
  • mysql主从,django使用多数据库做读写分离
    mysql主从mysql主从搭建的目的1.读写分离2.单个实例并发量低,提高并发量3.只在主库写,读数据都去从库mysql主从原理步骤一:主库db的更新事件(update,insert,delete)被写道binlog步骤二:从库发起连接,连接到主库步骤三:此时主库创建一个binlogdumpthread线程,把binlog的内容发送到......
  • java连接数据库的5种方式
    方式一直接导入第三方库驱动类这种加载方式在jdbc入门时已经用过,这个driver属于第三方库,。为静态加载,灵活性差,依赖性抢方式二使用反射机制获取方式一和方式二代码packagecom.hsp.edu;importcom.mysql.cj.jdbc.Driver;importjava.lang.reflect.Constructor;import......
  • 第四章 存储器管理 4.5 基本分段存储管理方式
    一、程序的分段:方便编程,分段共享,分段保护,动态链接,动态增长 二、分段系统的基本原理  1.分段  作业地址空间按逻辑信息的完整性被划分为若干个段;  每段有段名(或段号),每段从0开始编址;  段内的地址空间是连续的。   2.段表——实现从逻辑段到物理内存区......
  • 通过晶体数据库下载cif文件(转摘)
    1.7个晶体数据库下载cif文件的介绍——摘自:https://mbd.baidu.com/ug_share/mbox/4a83aa9e65/share?product=smartapp&tk=b345140e38a063d8d34015a16117c5d1&share_url=https%3A%2F%2F7dapi7.smartapps.baidu.com%2Fpages%2Farticle%2Farticle%3F_swebfr%3D1%26eid%3Dc1a3101e0346......
  • Mysql数据库
    数据库1.什么是数据库数据库(DataBase,简称DB)概念:长期存放在计算机内,有组织,可共享的大量数据的集合,是一个数据“仓库”作用:保存,并能安全管理数据(如:增删改查等),减少冗余…数据库分类关系型数据库(SQL)MySQL,Oracle,SQLServer,SQLite,DB2,......
  • Pinecone矢量数据库介绍
    Pinecone是一个矢量数据库,使开发人员只需使用API即可轻松地向其应用程序添加矢量搜索功能。介绍复杂数据正以惊人的速度增长。这些是非结构化形式的数据,包括Web上的文档、图像、视频和纯文本。许多组织将从存储和分析复杂数据中受益,但对于考虑结构化数据构建的传统数据库......