ddl

Add ON DELETE CASCADE behavior to an sqlite3 table after it has been created

让人想犯罪 __ 提交于 2019-12-17 05:14:38
问题 Is it possible to add an ON DELETE CASCADE to a table after it has been created? My schema is as follows: CREATE TABLE skills(name varchar, skill varchar, level int, foreign key(name) references runners(name), primary key(name, skill)); And I would like to cascade if the foreign key is deleted. 回答1: SQLite's ALTER TABLE command cannot do what you want. However, it is possible to bypass the SQL interpreter and change the internal table definition directly. SQLite stores table definitions as a

Add ON DELETE CASCADE behavior to an sqlite3 table after it has been created

爷,独闯天下 提交于 2019-12-17 05:14:27
问题 Is it possible to add an ON DELETE CASCADE to a table after it has been created? My schema is as follows: CREATE TABLE skills(name varchar, skill varchar, level int, foreign key(name) references runners(name), primary key(name, skill)); And I would like to cascade if the foreign key is deleted. 回答1: SQLite's ALTER TABLE command cannot do what you want. However, it is possible to bypass the SQL interpreter and change the internal table definition directly. SQLite stores table definitions as a

深入理解MDL元数据锁

爷,独闯天下 提交于 2019-12-16 09:54:12
前言: 当你在MySQL中执行一条SQL时,语句并没有在你预期的时间内执行完成,这时候我们通常会登陆到MySQL数据库上查看是不是出了什么问题,通常会使用的一个命令就是 show processlist,看看有哪些session,这些session在做什么事情。当你看到 waiting for table metadata lock 时,那就是遇到MDL元数据锁了。本篇文章将会介绍MDL锁的产生与排查过程。 1.什么是MDL锁 MDL全称为metadata lock,即元数据锁。MDL锁主要作用是维护表元数据的数据一致性,在表上有活动事务(显式或隐式)的时候,不可以对元数据进行写入操作。因此从MySQL5.5版本开始引入了MDL锁,来保护表的元数据信息,用于解决或者保证DDL操作与DML操作之间的一致性。 对于引入MDL,其主要解决了2个问题,一个是事务隔离问题,比如在可重复隔离级别下,会话A在2次查询期间,会话B对表结构做了修改,两次查询结果就会不一致,无法满足可重复读的要求;另外一个是数据复制的问题,比如会话A执行了多条更新语句期间,另外一个会话B做了表结构变更并且先提交,就会导致slave在重做时,先重做alter,再重做update时就会出现复制错误的现象。 元数据锁是server层的锁,表级锁,每执行一条DML、DDL语句时都会申请MDL锁,DML操作需要MDL读锁

change column datatype from array to integer

我的梦境 提交于 2019-12-14 04:13:11
问题 I need to change the data type of a column from _float8 to int4 ALTER TABLE "table" ALTER COLUMN "col" SET DATA TYPE int4; results in column "col" cannot be cast automatically to type integer ALTER TABLE "table" ALTER COLUMN "col" SET DATA TYPE int4 USING (col::integer); results in cannot cast type double precision[] to integer any ideas? 回答1: You have to point out which element of the array should be used in the conversion, e.g. alter table x alter column y set data type int4 using (y[1]:

Dynamically constructing MySQL Code for creating a trigger

浪尽此生 提交于 2019-12-13 19:17:55
问题 I am trying to implement audit trail/logging for a MySQL database. Now I have created an "audit" db (let's call it company_audit ) which will contain "audit" copies of certain tables in the main db. (Say for company.customers I will create company_audit.customers . The audit table will contain the same exact columns as in the original table. I want to use a trigger to insert changes in the original table into the audit table like so: DELIMITER $$ DROP TRIGGER IF EXISTS customers_history_AU$$

SQL Server Conditional Foreign Key Constraints

蹲街弑〆低调 提交于 2019-12-13 13:07:47
问题 I'm having trouble figuring out how to create a foreign key constraint. My data model is fixed and out of my control, it looks like this: CREATE TABLE Enquiry (Enquiry_Ref INTEGER PRIMARY KEY CLUSTERED, Join_Ref INTEGER, EnquiryDate, EnquiryType...) CREATE TABLE Contact (Contact_Ref INTEGER PRIMARY KEY CLUSTERED, Surname, Forenames ....) CREATE TABLE UniversalJoin (Join_Ref INTEGER, Contact_Ref INTEGER, Rel_Type INTEGER) Each Enquiry has exactly one Contact. The link between the two is the

Is there any ISO standard of how to write SQL queries in DDL?

主宰稳场 提交于 2019-12-13 08:13:42
问题 I am doing my first database project. I would like to know which of the following ways should I use to make SQL queries in DDL. #1 CREATE TABLE employees ( id INTEGER PRIMARY KEY, first_name CHAR(50) NULL, last_name CHAR(75) NOT NULL, dateofbirth DATE NULL ); #2 CREATE TABLE employees ( id INTEGER PRIMARY KEY, first_name CHAR(50) NULL, last_name CHAR(75) NOT NULL, dateofbirth DATE NULL ); #3 CREATE TABLE employees ( ID integer PRIMARY KEY, FIRST_NAME char(50) NULL, LAST_NAME char(75) NOT NULL

HSQLDB UNIQUE constraint and SQL Array Type

流过昼夜 提交于 2019-12-13 06:27:17
问题 I am developing with HSQLDB 2.2.9. HSQLDB is one of the RDBMS's out there that supports the SQL Array type and I want to use this capability to address some functionality in my database. I've been running some command line queries as I develop my database, but I am not sure how the UNIQUE constraint is handled by HSQLDB when declared for a column of type VARCHAR(24) ARRAY[]. The DDL I am using follows: CREATE CACHED TABLE Clients ( cli_id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,

Zend framerwork DB DDL update

人走茶凉 提交于 2019-12-13 04:02:01
问题 I am new to zend and i want to use the DB module for an application. I have this code $DB = new Zend\Db\Adapter\Adapter(array( 'driver' => 'Mysqli', 'database' => 'database', 'username' => 'user', 'password' => 'pass' )); use Zend\Db\Sql\Ddl; use Zend\Db\Sql\Ddl\Column; use Zend\Db\Sql\Ddl\Constraint; $table = new Ddl\CreateTable('table'); $table->setTable('table'); $table->addColumn(new Column\Integer('id',false,NULL,array('autoincrement'=>true))); $table->addColumn(new Column\Varchar('name'

Postgres 8.3 Query full function definition using a query

拥有回忆 提交于 2019-12-13 03:57:15
问题 Is it possible to be able to retrieve the full function definition (with parameters etc) using a SQL query? 回答1: This function or view (doing similar things) works with Postgres 8.3. CREATE AGGREGATE public.textcat_all( basetype = text, sfunc = textcat, stype = text, initcond = '' ); CREATE OR REPLACE FUNCTION public.getfunctionddl(functionOid oid) RETURNS text AS $BODY$ DECLARE funcschema text; funcname text = NULL; paranames text; paramodes text; paratypes text; paraintypes text; function