db2 建立資料庫、表、儲存過程等
啟動
[[email protected] ~]# su - db2inst1
[[email protected] ~]$ 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.
[ [email protected] ~]$
建立資料庫
[[email protected] ~]$ db2 create database hibernate;
SQL1001N "hibernate" is not a valid database name. SQLSTATE=2E000
資料庫名字應該是1-8個字元
[[email protected] ~]$ db2 create database hiber;
DB20000I The CREATE DATABASE command completed successfully.
列出資料庫
[[email protected] ~]$ 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 =
[ [email protected] ~]$
連線資料庫
[[email protected] ~]$ db2 connect to hiber;
Database Connection Information
Database server = DB2/LINUX 9.7.6
SQL authorization ID = DB2INST1
Local database alias = HIBER
[[email protected] ~]$
列出表
[[email protected] ~]$ db2 list tables;
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
0 record(s) selected.
[[email protected] ~]$
建立表
[[email protected] ~]$ 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
[[email protected] ~]$
這個在oracle裡是可以執行的,但是在db2上,primary key的欄位一定要指明not null才行:
[[email protected] ~]$ 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.
[[email protected] ~]$
執行程式碼塊
db2 [email protected]表示命令以@結束
[[email protected] ~]$ db2 [email protected]
(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 [email protected]
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 [email protected]
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 [email protected]
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 [email protected]
DB20000I The UPDATE COMMAND OPTIONS command completed successfully.
db2 => list command [email protected]
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 =>
相關推薦
MongoDB非關係型資料庫的簡介、建立資料庫和表、配置使用者等
簡介: MongoDB是一個高效能,開源,無模式的文件型資料庫,是一個基於分散式檔案儲存的資料庫,是一個介於關係資料庫和非關係資料庫之間的產品,是非關係資料庫當中功能最豐富,最像關係資料庫的,是當前NoSQL資料庫產品中最熱門的一種。 mongodb的結構和普通的關係型資料
刪除指定SQL資料庫所有表和儲存過程
原理就是根據 sysobjects 系統表裡面查到每張表名,然後drop掉,同理可以一次性drop所有儲存過程 --刪所有資料表 USE [資料庫名] DECLARE @tableName VARCH
db2 建立資料庫、表、儲存過程等
啟動 [[email protected] ~]# su - db2inst1 [[email protected] ~]$ db2start SQL8007W There are "13" day(s) left in the evaluatio
EF6學習筆記一:code-first簡單建立資料庫、表的一些過程
我的EF學習筆記是按照 汪鵬(網名Jeffcky) 大俠《你必須掌握的Entity Framework 6.x與Core 2.0》來弄的。 這也是我第一篇部落格,感覺這東西不能亂寫啊,算了,幹吧。 EF我之前是做過的,但是隻是一些零碎的東西,不成系統。 EF是什麼呢?ORM框架objec
SQL Server判斷資料庫、表、儲存過程、函式是否存在
--1.判斷資料庫是否存在 IF EXISTS ( SELECT * FROM sys.databases WHERE NAME = '[資料庫名]' ) DROP DATABASE [資料庫名] --2.
mysql 資料庫、表、檢視 的建立/修改/刪除
建立,刪除資料庫 指定字符集 create database 資料庫名[default character set utf8];&n
SQL函式:判斷庫、表、儲存過程等是否存在
----------------- 庫是否存在if exists(select * from master..sysdatabases where name=N'庫名')print 'exists'elseprint 'not exists'----------------- 判斷要建立的表名是否存在if e
用python3對資料庫mysql進行建立庫、表、增刪改查操作
import pymysql import sys # python mysql 建立庫、表增刪改查標準語句 print('----------------------------') print('載入mysql模組完成') con = pymysql.connect(ho
SQL 使用者許可權(表、函式、儲存過程等)---收藏
對資料的訪問是通過SQL Server 2008的許可權層次結構進行管理的。可以通過GRANT、DENY和REVOKE語句來管理這個許可權層次結構。 ● GRANT:允許一個數據庫使用者或角色執行所授許可權指定的操作。 ● DENY:拒絕一個數據庫使用者或角色的特定許可權,並且阻止它
資料庫sql語句筆試題--包含建立資料庫、表、插入記錄、查詢等操作
資料庫筆試題,包含建立資料庫、表、插入記錄、查詢等操作。1.建立資料庫。要求用SQL語句建立滿足如下要求的資料庫:(1)資料庫名稱為School; (2)主資料檔案:邏輯名為SchoolData,檔名為“D:\School\SchoolData.mdf”,檔案初始大小為50
sqlserver 各種判斷是否存在(表名、函式、儲存過程等)
庫是否存在 if exists(select * from master..sysdatabases where name=N'庫名') print 'exists'elseprint 'not exists'--------------- -- 判斷要建立的表名是否存在 if exists (select
分享知識-快樂自己: Oracle資料庫例項、使用者、表、表空間之間關係
資料庫: Oracle資料庫是資料的物理儲存。這就包括(資料檔案ORA或者DBF、控制檔案、聯機日誌、引數檔案)。 其實Oracle資料庫的概念和其它資料庫不一樣,這裡的資料庫是一個作業系統只有一個庫。可以看作是Oracle就只有一個大資料庫。 例項: 一個Oracle例項(Oracle
查詢資料庫中當前使用者下的所有表,儲存過程,檢視,觸發器
user_objects是oracle字典表的試圖,他包含了通過DDL建立的所有物件。表,試圖,索引。。等 all_table, dba_table , user_table 同理 Select object_name From user_obj
MySQL修改資料庫、表、欄位字符集
修改資料庫字符集: 程式碼如下: ALTER DATABASE db_name DEFAULT CHARACTER SET character_name [COLLATE ...]; 把表預設的字符集和所有字元列(CHAR,VARCHAR,TEXT)改為
Oracle建立表空間、表、一級使用者授權
---恢復內容開始--- 1. 安裝: 百度一下你就知道 2. sqlplus登入/sqlplus命令登入 在安裝Oracle時,你需要記住設定的“全域性資料庫名”(預設為orcl) 和 口令,在以兩種方式登入時: 使用者名稱: sys(超級使用者==sysdba) / system(管
使用jdbc獲取資料庫全部表、表包含欄位
1. 建立資料來源Vo, 封裝資料來源相關資訊; //資料來源資訊 public class DataSource implements Serializable { //資料庫型別 private JdbcType jdbcTy
SQL 選擇資料庫以及查詢所有資料庫名、表、列名
查詢所有資料庫名 資料庫、表、列、使用者、許可權等資訊被儲存在資料庫和表中,不過內部的表一般不直接被訪問。可用MySQL的 show 命令來顯示這些資訊(MySQL從內部表中提取這些資訊)。SQL命令
Sql2012如何將遠端伺服器資料庫及表、表結構、表資料匯入本地資料庫
1、第一步,在本地資料庫中建一個與伺服器同名的資料庫 2、第二步,右鍵源資料庫,任務》匯出資料,彈出匯入匯出提示框,點下一步繼續 3、遠端資料庫操作,確認伺服器名稱(伺服器地址)、身份驗證(輸入使用者名稱、密碼)、選擇需要匯出的源資料庫,點下一步繼續 4、本地目標伺服器
10、PL/SQL儲存過程
子程式是執行特定任務的程式單元/模組。 這些子程式組合起來形成更大的程式。這種做法被稱為“模組化設計”。 子程式可以被稱為呼叫程式的另一個子程式或程式呼叫。 可以在以下幾個地方中建立一個子程式 - 在模式(schema)級別中 一個程式包中 在PL/SQL塊中 在模式
MFC獲取SqlServer資料庫所有表、欄位名、記錄資料
程式碼如下過程其實不是每一步都有,但是主要功能都在! //1、連線資料庫類 BOOL CSqlDlg::Ado(CString strConn) { ::CoInitialize(NULL); // 初始化OLE/COM庫環境 try { m_pConn.CreateInstan