Home Database Mysql Tutorial Oracle 11g Release 1 (11.1) PL/SQL_理解 Record 类型

Oracle 11g Release 1 (11.1) PL/SQL_理解 Record 类型

Jun 07, 2016 pm 05:15 PM

若创建 records,需要先定义一个 RECORD 类型,再用该类型声明变量。也可以创建或查找一个表、视图,或 PL/SQL 游标,总之是你想

本文内容

  • 定义和声明 Record
  • Record 作为子程序参数和函数返回值
  • 给 Record 赋值
  • 比较 Record
  • 把 Record 插入到数据库
  • 把 Record 更新到数据库
  • Record 插入和更新的约束
  • 把查询数据放到 Record  
  • 定义和声明 Record

    若创建 records,需要先定义一个 RECORD 类型,再用该类型声明变量。也可以创建或查找一个表、视图,或 PL/SQL 游标,总之是你想要的值,使用 %ROWTYPE 属性来创建匹配的 Record。

    你可以在任何 PL/SQL 块、子程序或包的声明部分定义 RECORD 类型。当你自定义 RECORD 类型时,不能在域上指定一个 NOT NULL 的约束,或给出它们的默认值。

    示例 1:演示声明和初始化一个简单的 Record 类型

    DECLARE TYPE DeptRecTyp IS RECORD ( deptid NUMBER(4) NOT NULL := 99, dname departments.department_name%TYPE, loc departments.location_id%TYPE, region regions%ROWTYPE ); dept_rec DeptRecTyp;BEGIN dept_rec.dname := 'PURCHASING';END;/

    示例 2:演示声明和初始化 Record 类型

    := -1, name VARCHAR2(64) NOT NULL := '[anonymous]');-- Declare record variables of the types declared rec1 rec1_t; rec2 rec2_t;-- Declare a record variable that can hold-- a row from the EMPLOYEES table.-- The fields of the record automatically match the names and-- types of the columns.-- Don't need a TYPE declaration in this case. rec3 employees%ROWTYPE;-- Or mix fields that are table columns with user-defined fields. TYPE rec4_t IS RECORD (first_name employees.first_name%TYPE, last_name employees.last_name%TYPE, rating NUMBER); rec4 rec4_t;BEGIN-- Read and write fields using dot notation rec1.field1 := 'Yesterday'; rec1.field2 := 65; rec1.field3 := TRUNC(SYSDATE-1);-- Didn't fill name field, so it takes default value DBMS_OUTPUT.PUT_LINE(rec2.name);END;/

    若在数据库存储一个 Record,你可以在 INSERT 或 UPDATE 语句指定,只要它的域与表的列匹配。

    你可以使用 %TYPE 来指定 Record 域类型对应表的列的类型。即使列类型改变了,你的代码仍然可以运行。例如,增加了 VARCHAR2 字段的长度,或 NUMBER 字段的精度。

    示例 3:演示使用 %ROWTYPE 来声明一个 Record,,来保存 department 表的信息

    DECLARE-- Best: use %ROWTYPE instead of specifying each column.-- Use %ROWTYPE instead of

    %ROWTYPE because -- you only want some columns.-- Declaring cursor doesn't run query or affect performance.CURSOR c1 ISSELECT department_id, department_name, location_id FROM departments; rec1 c1%ROWTYPE;-- Use %TYPE in field declarations to avoid problems if -- the column types change. TYPE DeptRec2 IS RECORD (dept_id departments.department_id%TYPE, dept_name departments.department_name%TYPE, dept_loc departments.location_id%TYPE); rec2 DeptRec2;-- Write each field name, specifying type directly-- (clumsy and unmaintainable for working with table data-- use only for all-PL/SQL code). TYPE DeptRec3 IS RECORD (dept_id NUMBER, dept_name VARCHAR2(14), dept_loc VARCHAR2(13)); rec3 DeptRec3;BEGINNULL;END;/

    PL/SQL 可以定义包含对象、集合和其他 Record(内置 Record)的 Record。但 Record 不能是对象类型的属性。

    若声明一个 Record,表示数据库表的一行,则无需列出列,使用 %ROWTYPE 属性。

    当表添加列后,你的代码仍然可以运行。若你想表示一个表列的一个子集,或是不同表的列,则你可以定义一个视图或声明一个游标,来选择右边的列,执行任何需要的连接,再在视图或游标上应用 %ROWTYPE。

     

    linux

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌
    Two Point Museum: All Exhibits And Where To Find Them
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

    The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

    How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

    Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

    How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

    Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

    What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

    Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

    How do you drop a table in MySQL using the DROP TABLE statement? How do you drop a table in MySQL using the DROP TABLE statement? Mar 19, 2025 pm 03:52 PM

    The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

    How do you create indexes on JSON columns? How do you create indexes on JSON columns? Mar 21, 2025 pm 12:13 PM

    The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

    How do you represent relationships using foreign keys? How do you represent relationships using foreign keys? Mar 19, 2025 pm 03:48 PM

    Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

    How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? Mar 18, 2025 pm 12:00 PM

    Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)

    See all articles