ddl

SQL Server(触发器)

心已入冬 提交于 2019-11-29 08:21:26
一.触发器概述 触发器是一种特殊类型的存储过程,当指定表中的数据发生变化时触发器自动生效。它与表紧密相连,可以看做是表定义的一部分。触发器不能通过名称被直接调用,更不允许设置参数。 在SQL Server中一张表可以有多个触发器。用户可以使用INSERT、UPDATE或DELETE语句对触发器进行设置,也可以对一张表上的特定操作设置多个触发器。触发器可以包含复杂的Transact-SQL语句。不论触发器所进行的操作有多复杂,触发器都只作为一个独立的单元被执行,被看作是一个事务。如果在执行触发器的过程中发生了错误,则整个事务将会自动回滚。 二.触发器优点 触发器的优点表现在以下几个方面: (1)触发器自动执行,对表中的数据进行修改后,触发器立即被激活。 (2)为了实现复杂的数据库更新操作,触发器可以调用一个或多个存储过程,甚至可以通过调用外部过程(不是数据库管理系统本身)完成相应的操作。 (3)触发器能够实现比CHECK约束更为复杂的数据完整性约束。在数据库中,为了实现数据完整性约束,可以使用CHECK约束或触发器。CHECK约束不允许引用其他表中的列来完成检查工作,而触发器可以引用其他表中的列。它更适合在大型数据库管理系统中用来约束数据的完整性。 (4)触发器可以检测数据库内的操作,从而取消了数据库未经许可的更新操作,使数据库修改、更新操作更安全,数据库的运行也更稳定。 (5

第4章 DDL数据定义

瘦欲@ 提交于 2019-11-29 07:06:38
第 4 章 DDL 数据定义 4.1 创建数据库 1 )创建一个数据库,数据库在 HDFS 上的默认存储路径是 /user/hive/warehouse/*.db 。 hive (default)> create database db_hive; 2 )避免要创建的数据库已经存在错误,增加 if not exists 判断。(标准写法) hive (default)> create database db_hive; FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Database db_hive already exists hive (default)> create database if not exists db_hive; 3 )创建一个数据库,指定数据库在 HDFS 上存放的位置 hive (default)> create database db_hive2 location '/db_hive2.db'; 4.2 查询数据库 4.2.1 显示数据库   1 .显示数据库 hive> show databases;   2 .过滤显示查询的数据库 hive> show databases like 'db_hive*'; OK db_hive

Creating a trigger to only run when a new table is being created

大城市里の小女人 提交于 2019-11-29 07:00:33
I know that I can use this to create DDL create trigger; CREATE OR REPLACE TRIGGER create_table_trigger AFTER CREATE ON SCHEMA DECLARE BEGIN END; Problem is this trigger would run on DDLs like 'Create sequence'; how can I only execute this for 'Create Table' DDLs? RichardTheKiwi CREATE OR REPLACE TRIGGER create_table_trigger AFTER CREATE ON SCHEMA BEGIN IF SYS.DICTIONARY_OBJ_TYPE = 'TABLE' THEN .... END; For a list of EVENT attributes, refer to this page http://ist.marshall.edu/ist480adbp/plsql_triggers.html (link is down) Wayback machine link to the contents of the dead link above: https:/

Mysql高手系列 - 第4天:DDL常见操作汇总

女生的网名这么多〃 提交于 2019-11-29 06:38:21
这是Mysql系列第4篇。 环境:mysql5.7.25,cmd命令中进行演示。 DDL:Data Define Language数据定义语言,主要用来对数据库、表进行一些管理操作。 如:建库、删库、建表、修改表、删除表、对列的增删改等等。 文中涉及到的语法用[]包含的内容属于可选项,下面做详细说明。 库的管理 创建库 create database [if not exists] 库名; 删除库 drop databases [if exists] 库名; 建库通用的写法 drop database if exists 旧库名; create database 新库名; 示例 mysql> show databases like 'javacode2018'; +-------------------------+ | Database (javacode2018) | +-------------------------+ | javacode2018 | +-------------------------+ 1 row in set (0.00 sec) mysql> drop database if exists javacode2018; Query OK, 0 rows affected (0.00 sec) mysql> show databases like

Superkey vs. Candidate key

*爱你&永不变心* 提交于 2019-11-29 03:42:33
What difference between Super and Candidate key in ERDB? Thanks. A superkey is a set of columns that uniquely identifies a row. A Candidate key would be a MINIMAL set of columns that uniquely identifies a row. So essentially a Superkey is a Candidate key with extra unnecessary columns in it. candidate key is a minimal superkey gbn Candidate key = minimal key to identify a row Super key = at least as wide as a candidate key For me, a super key would generally introduce ambiguities over a candidate key Let's keep it simple SuperKey - A set of keys that uniquely defines a row .So out of all the

MySQL语言分类——DDL

只愿长相守 提交于 2019-11-29 01:55:23
DDL的全称Data Definition Language,即数据定义语言   DDL的语法有:create、alter、drop、rename、truncate。对此做一个详细的解释: create (创建)   create 可以创建数据库 # 创建数据库 create database database_name; # 然后进入该数据库 use database_name;   create 可以创建表格     创建表格的语法:方括号的表示可以省略       create [temporary] table [if not exits] table_name(         column_name data_type [not null | null] [default default_value] [auto_increment] [Constraints约束条件],       );     [if not exits] 如果新建表不存在,则会创建新表;如果存在,不会报错,数据也不会被覆盖 create table if not exits table_name( id int, name varchar(20) );     [temporary] 创建临时表 用于存储临时计算的数据,生命周期只限于本次连接 create temporary table

How can I drop all indexes of a table in Postgres?

笑着哭i 提交于 2019-11-29 01:31:52
I keep having this problem: I have like 20 indexes on a table that I need to drop in order to do testing. Dropping the table doesn't drop all of this metadata. There doesn't seem to be a wildcard drop index ix_table_* or any useful command. There seem to be some bash loops around psql you can write. There must be something better! Thoughts? Erwin Brandstetter Assuming you only want to drop plain indexes: DO $$BEGIN EXECUTE ( SELECT 'DROP INDEX ' || string_agg(indexrelid::regclass::text, ', ') FROM pg_index i LEFT JOIN pg_depend d ON d.objid = i.indexrelid AND d.deptype = 'i' WHERE i.indrelid =

Renaming a column in MS SQL Server 2005

筅森魡賤 提交于 2019-11-29 01:09:41
What is the best practice when it comes to renaming a table column using SQL (MS SQL Server 2005 variant)? This assumes that there is data in the column that must be preserved. Glen You have to use a stored proc to rename a column. The following will rename your column from 'oldColumnName' to 'newColumnName' without affecting any data. EXEC sp_rename 'tableName.[oldColumnName]', 'newColumnName', 'COLUMN' Obviously you'll have to update any code / stored procs / SQL that uses the old name manually. I had the same problem today, and the solution was kill all processes on the database, cause the

How do I conditionally create a table in Sybase (TSQL)?

强颜欢笑 提交于 2019-11-28 21:33:23
OK, so Sybase (12.5.4) will let me do the following to DROP a table if it already exists: IF EXISTS ( SELECT 1 FROM sysobjects WHERE name = 'a_table' AND type = 'U' ) DROP TABLE a_table GO But if I try to do the same with table creation, I always get warned that the table already exists, because it went ahead and tried to create my table and ignored the conditional statement. Just try running the following statement twice, you'll see what I mean: IF NOT EXISTS ( SELECT 1 FROM sysobjects WHERE name = 'a_table' AND type = 'U' ) CREATE TABLE a_table ( col1 int not null, col2 int null ) GO Running

Reverse engineer DDL from JPA entities

隐身守侯 提交于 2019-11-28 17:56:27
I'm playing around with some JPA stuff, changing the mappings to see how they're supposed to be etc. It's basic experimentation. However I can't find a tool that will simply read my entities and then generate the table schema for me. I tried to find something like this in JBoss tools but nada. Eclipse integration will be a huge plus but i'll take a command line tool or an ant task. Any ideas? andri I don't think there is an universal way of doing this with JPA, you have to directly use the underlying JPA implementation to achieve this. For Hibernate , there are several possibilities: Use the