T-SQL开发

Jun 07, 2016 pm 05:37 PM
unique index

之所以把约束和索引放到一起来看,主要是因为主键约束和唯一键约束,它们会自动创建一个对应的索引,先分别看下数据库中的几个约束。一约束在关系型数据库里,通

之所以把约束和索引放到一起来看,主要是因为主键约束和唯一键约束,它们会自动创建一个对应的索引,先分别看下数据库中的几个约束。

一 约束

在关系型数据库里,通常有5种约束,示例如下:

use tempdb go create table s ( sid varchar(20), sname varchar(20), ssex varchar(2) check(ssex='男' or ssex='女') default '男', sage int check(sage between 0 and 100), sclass varchar(20) unique, constraint PK_s primary key (sid,sclass) ) create table t ( teacher varchar(20) primary key, sid varchar(20) not null, sclass varchar(20) not null, num int, foreign key(sid,sclass) references s(sid,sclass) )


单独定义在某一列上的约束被称为列级约束,定义在多列上的约束则称为表级约束。


1.主键约束

在表中的一列或者多列上,定义主键来唯一标识表中的数据行,也就是数据库设计3范式里的第2范式;


主键约束要求键值唯一且不能为空:primary key = unique constraint + not null constraint


2.唯一键约束

唯一约束和主键约束的区别就是:允许NULL,SQL Server 中唯一键列,仅可以有一行为NULL,ORACLE中可以有多行列值为NULL。


一个表只能有一个主键,但可以有多个唯一键:unique index = unique constraint


在一个允许为NULL的列上,想要保证非NULL值的唯一性,该怎么办?

从SQL Server 2008开始,可以用筛选索引(filtered index)

use tempdb GO create table tb5 ( id int null ) create unique nonclustered index un_ix_01 on tb5(id) where id is not null GO


3.外键约束

表中的一列或者多列,引用其他表的主键或者唯一键。外键定义如下:

use tempdb GO --drop table tb1,tb2 create table tb1 ( col1 int Primary key, col2 int ) insert into tb1 values (2,2),(3,2),(4,2),(5,2) GO create table tb2 ( col3 int primary key, col4 int constraint FK_tb2 foreign key references tb1(col1) ) GO select * from tb1 select * from tb2 select object_name(constraint_object_id) constraint_name, object_name(parent_object_id) parent_object_name, col_name(parent_object_id,parent_column_id) parent_object_column_name, object_name(referenced_object_id) referenced_object_name, col_name(referenced_object_id,referenced_column_id) referenced_object_column_name from sys.foreign_key_columns where referenced_object_id = object_id('tb1')


外键开发维护过程中,常见的问题及解决方法:

(1) 不能将主表中主键/唯一键的部分列作为外键,必须是全部列一起引用

create table tb3 ( c1 int, c2 int, c3 int, constraint PK_tb3 primary key (c1,c2) ); create table tb4 ( c4 int constraint FK_tb4 foreign key references tb3(c1), c5 int, c6 int ); /* Msg 1776, Level 16, State 0, Line 1 There are no primary or candidate keys in the referenced table 'tb3' that match the referencing column list in the foreign key 'FK_tb4'. Msg 1750, Level 16, State 0, Line 1 Could not create constraint. See previous errors. */


(2) 从表插入数据出错

insert into tb2 values (1,1) /* Msg 547, Level 16, State 0, Line 1 The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tb2". The conflict occurred in database "tempdb", table "dbo.tb1", column 'col1'. */ --从表在参照主表中的数据,可以先禁用外键(只是暂停约束检查) alter table tb2 NOCHECK constraint FK_tb2 alter table tb2 NOCHECK constraint ALL --从表插入数据后,再启用外键 insert into tb2 values (1,1),(3,3),(4,4) alter table tb2 CHECK constraint FK_tb2


(3) 主表删除/更新数据出错

--先删除从表tb2的数据或禁用外键,才能删除主表tb1中的值,否则报错如下 --未被引用的行可被直接删除 insert into tb2 values (2,2) delete from tb1 GO /* Msg 547, Level 16, State 0, Line 3 The DELETE statement conflicted with the REFERENCE constraint "FK_tb2". The conflict occurred in database "tempdb", table "dbo.tb2", column 'col4'. */


(4) 清空/删除主表出错

--清空主表时,即便禁用外键,但外键关系依然存在,所以任然无法truncate truncate table tb1 /* Msg 4712, Level 16, State 1, Line 2 Cannot truncate table 'tb1' because it is being referenced by a FOREIGN KEY constraint. */ --删除主表也不行 drop table tb1 /* Msg 3726, Level 16, State 1, Line 2 Could not drop object 'tb1' because it is referenced by a FOREIGN KEY constraint. */ --先truncate从表,再truncate主表也不行 truncate table tb2 truncate table tb1 --唯一的办法删掉外键,truncate将不受控制 alter table tb2 drop constraint FK_tb2 truncate table tb1 --最后再加上外键,注意with nocheck选项,因为主从表里数据不一致了,所以不检查约束,否则外键加不上 alter table tb2 WITH NOCHECK add constraint FK_tb2 foreign key(col4) references tb1(col1)


最后,虽然一个表上可以创建多个外键,但通常出于性能考虑,不推荐使用外键,数据参照完整性可以在程序里完成;


4.CHECK约束

可定义表达式以检查列值,通常出于性能考虑,不推荐使用。


5.NULL 约束

用于控制列是否允许为NULL。使用NULL时有几个注意点:

(1) SQL SERVER中聚合函数是会忽略NULL值的;

(2) 字符型的字段,如果not null,那这个字段不能为null值,但可以为'',这是空串,和null是不一样的;

(3) NULL值无法直接参与比较/运算;

declare @c varchar(100) set @c = null if @c'abc' or @c = 'abc' print 'null' else print 'I donot know' GO declare @i int set @i = null print @i + 1

在开发过程中,NULL会带来3值逻辑,不推荐使用,对于可能为NULL的值可用默认值等来代替。


6.DEFAULT约束

从系统视图来看,default也是被SQL Server当成约束来管理的。

select * from sys.default_constraints


(1) 常量/表达式/标量函数(系统,自定义、CLR函数)/NULL都可以被设置为默认值;

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Explain the role of InnoDB redo logs and undo logs. Explain the role of InnoDB redo logs and undo logs. Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

See all articles