我在 SQL Server 2008 中定义了以下索引视图(您可以从 gist下载工作架构以进行测试):
CREATE VIEW dbo.balances
WITH SCHEMABINDING
AS
SELECT
user_id
, currency_id
, SUM(transaction_amount) AS balance_amount
, COUNT_BIG(*) AS transaction_count
FROM dbo.transactions
GROUP BY
user_id
, currency_id
;
GO
CREATE UNIQUE CLUSTERED INDEX UQ_balances_user_id_currency_id
ON dbo.balances (
user_id
, currency_id
);
GO
Run Code Online (Sandbox Code Playgroud)
user_id、currency_id和transaction_amount都定义为 中的NOT NULL列dbo.transactions。但是,当我查看 Management Studio 的对象资源管理器中的视图定义时,它在视图中将balance_amount和都标记transaction_count为NULL可列。
我查看了几个讨论,这是其中最相关的一个,建议对函数进行一些改组可能有助于 SQL Server 识别视图列始终为NOT NULL. 但是,在我的情况下不可能进行这种改组,因为在索引视图中不允许 …
执行插入时,空字符串将转换为 null:
insert into test (f) values ('');
Run Code Online (Sandbox Code Playgroud)
现在,有一行 f 包含空值。
但是,当我查询表时,我不能使用 '':
select * from test where f='';
no rows selected
Run Code Online (Sandbox Code Playgroud)
我可以使用空值:
select * from test where f is null;
____F_
NULL
Run Code Online (Sandbox Code Playgroud)
所以......似乎Oracle决定不能将空字符串用于插入,但在执行查询时它们仍然是空字符串。关于空字符串何时变为空值以及何时仍为空字符串的文档在哪里?
我有一个表,需要添加一个没有默认值的新列:
ALTER TABLE integrations.billables
DROP CONSTRAINT IF EXISTS cc_at_least_one_mapping_needed_billables,
ADD CONSTRAINT cc_at_least_one_mapping_needed_billables
CHECK ((("qb_id" IS NOT NULL) :: INTEGER +
("xero_id" IS NOT NULL) :: INTEGER +
("freshbooks_id" IS NOT NULL) :: INTEGER +
("unleashed_id" IS NOT NULL) :: INTEGER +
("csv_data" IS NOT NULL) :: INTEGER +
("myob_id" IS NOT NULL) :: INTEGER) > 0);
Run Code Online (Sandbox Code Playgroud)
ALTER TABLE integrations.billables
DROP COLUMN IF EXISTS myob_id,
ADD COLUMN myob_id varchar(255);
Run Code Online (Sandbox Code Playgroud)
如何为下一个值添加约束,而不是为那些已经存在的值添加约束?(否则我会得到某些行违反了错误检查约束“”)。
这与我之前的问题有关:错误:某些行违反了检查约束
postgresql null database-design postgresql-9.2 check-constraints
CREATE TABLE IF NOT EXISTS b2c_constants (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(64) NOT NULL,
is_deleted BOOL DEFAULT FALSE,
UNIQUE (name)
) ENGINE InnoDB CHARSET utf8 COLLATE utf8_unicode_ci
CREATE TABLE IF NOT EXISTS b2c_constant_bindings (
constant_id INT UNSIGNED NOT NULL,
company_id INT UNSIGNED NOT NULL,
object_id INT UNSIGNED DEFAULT NULL,
property_id INT UNSIGNED DEFAULT NULL,
value VARCHAR(255) NOT NULL,
UNIQUE (constant_id, company_id, object_id, property_id),
FOREIGN KEY (constant_id) REFERENCES b2c_constants (id) ON UPDATE RESTRICT ON DELETE CASCADE, …Run Code Online (Sandbox Code Playgroud) 不久前我创建了一个表并开始向其中添加数据。最近我添加了一个新列 ( address)NOT NULL作为新列的一部分。旧行(添加前)仍然为空,这在定义中创建了警告。但是,仍然允许带有新列的新行插入空值。
新列的添加前空值是否是允许的来源?如果是这样,有没有办法告诉 MySQL 不允许它,即使它以前是这样?
mysql> show create table my_table\G
*************************** 1. row ***************************
Table: my_table
Create Table: CREATE TABLE `my_table` (
`entry_id` int(11) NOT NULL auto_increment,
`address` varchar(512) NOT NULL,
`follow_up_to` int(11) default NULL,
PRIMARY KEY (`entry_id`),
KEY `follow_up_to` (`follow_up_to`),
CONSTRAINT `my_table_ibfk_1`
FOREIGN KEY (`follow_up_to`)
REFERENCES `my_table` (`entry_id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=535 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql>
Run Code Online (Sandbox Code Playgroud) 有没有办法编写一个插入/更新查询来检查列是否允许NULLs:如果它确实将列设置为NULL,''否则(空字符串)?
我会是这样的:
UPDATE mytable
SET field = IF(A_FUNCTION_TO_CHECK_IF_ALLOWS_NULL(), NULL, '');
Run Code Online (Sandbox Code Playgroud) 我正在尝试在格式为 PostgreSQL 查询中返回一个文本字段
'stringOne' || string_agg(field, ',') || 'stringTwo'
Run Code Online (Sandbox Code Playgroud)
对于 group 子句中的某些元素, wherefield始终为 null。我想要并期望stringOnestringTwo在这种情况下结束,但我得到NULL.
为什么会这样,我如何完成我想要做的事情?
假设我有桌子
foo bar
+----+--------+ +----+-------+--------------+
| id | name | | id | fooid | baz |
+----+--------+ +----+-------+--------------+
| 1 | FooOne | | 1 | 1 | FooOneBazOne |
| 2 | FooTwo | | 2 | 1 | FooTwoBazTwo |
+----+--------+ +----+-------+--------------+
Run Code Online (Sandbox Code Playgroud)
我运行查询
SELECT
foo.name AS foo,
'Bazzes: ' || string_agg(bar.baz, ', ') AS bazzes …Run Code Online (Sandbox Code Playgroud) Select FullName, Login
FROM [User]
ORDER BY FullName desc, login asc
Run Code Online (Sandbox Code Playgroud)
Select FullName, Login
FROM [User]
ORDER BY FullName desc, login asc
Run Code Online (Sandbox Code Playgroud)
FullName Login
----------------------------- ------------------------------
M...... H......... mh..@...com
A.... H..... ad..@...com
NULL and..@...com
NULL ben..@...com
NULL roc..@...com
Run Code Online (Sandbox Code Playgroud)
I want the full name in abc order desc and same with login, but I want all nulls to go to the bottom.
我在Oracle 官方文档中看到了这个声明:
在 Microsoft SQL Server 中,只有具有可变长度数据类型的列才能存储 NULL 值。当您创建一个允许 NULL 具有固定长度数据类型的列时,该列会自动转换为系统可变长度数据类型...
我从来没有在SQL Server 文档中读到过这个,也没有经历过这样的事情。相反:在 SQL Server 中,固定长度的数据类型(例如 int 和 float,还有 char)被大量使用并且即使在可为 NULL 的情况下也非常有效地存储。
这个 Oracle 声明背后有什么理由吗?!
我有一个parcels当前包含列owner_addr1, owner_addr2,的表owner_addr3。有时,后两个字段中的一个或两个字段为空。我想将它们组合成一个新的字段,owner_addr上面的每个字段//都在它们之间连接。
但是如果一个或多个原始列是 NULL,我不想连接//到结果列。因此,举例来说,如果owner_addr1是123 4th Avenue SE和owner_addr2和owner_addr3的NULL,然后我想要的结果列只是123 4th Avenue SE,没有123 4th Avenue SE // //(如果我只是做了这会发生CONCAT()与//空字符串之间......我只是想添加//非之间的NULL列,或将其如果只有一个非NULL列,则完全退出。
有没有一种简单的方法可以在 Postgresql 中进行这种条件连接,它会遗漏空行?或者我应该写一个python脚本来做到这一点?
null ×10
mysql ×3
postgresql ×3
concat ×2
oracle ×2
sql-server ×2
aggregate ×1
condition ×1
constraint ×1
empty-string ×1
functions ×1
order-by ×1
storage ×1
string ×1
update ×1
view ×1