SQL语句PART6
Differring Constraints:
Constraints can have the following attributes: DEFFERRABLE / NOT DEFFERRABLE, INITIALLY DEFFERRED / INITIALLY IMMEDIATE.
e.g.:
alter table dept2 add constraint dept2_id_pk primary key (department_id) deferrable initially deferred; // deferring constraint on creation.
set constraints dept2_id_pk immediate // changing a specific constraint attribute.
alter session set constraints=immediate // changing all constraints for a session.
Difference between initially deferrable and initially immediate
initially deferred: waits to check the constraints until the transaction ends.
initially immediate: checks the constraint at the end of the statement execution.
Dropping a constraint:
1. Removing the manager constraint from the EMP2 table:
alter table emp2 drop constraint emp_mgr_fk;
2. Removing the primary key constraint on the dept2 table and drop the associate foreign key constraint on the emp2.department_id column:
alter table dept2 drop primary key cascade;
Disabling Constraint
1. execute the disable clause of the alter table statement to deactivate an integrity constraint.
2. apply the cascade option to disable dependent integrity constraints.
eg: alter table emp2 disable constraint emp_dt_fk;
Enabling Constraints
1. activate an integrity constraint currently disabled in the table definition by using the enable clause.
2. a unique index is automatically created if you enable a unique key or a primary key constraint.
e.g.: alter table emp2 enable constraint emp_dt_fk;
Cascading constraints
1. used along with the drop column clauses.
2. drops all referential integrity constraints that refer to the primary key and unique keys defined on the dropped columns. 3. drops all multicolumn constraints defined on the dropped columns.
e.g.:
alter table emp2 drop column employee_id cascade constraints
alter table test1 drop (col1_pk, col2_fk, col1) cascade constraints;
e.g. foreign key example1:
create table depart(departid num
相关文档:
因为要根据很复杂的规则处理用户数据,所以这里用到数据库的游标。平时不怎么用这个,写在这里纯粹为自己备个忘。
--将学籍号重复的放入临时表 tmp_zdsoft_unitive_code(除高中学段外)
drop table tmp_zdsoft_unitive_code;
select s.id ,sch.school_code,sch.school_name,s.student_name,s.unitive_code,s.identity_car ......
using System;
using System.Text.RegularExpressions;
using Microsoft.SqlServer.Server;
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true,IsPrecise = true)]
public static bool RegExIsMatch(string pattern,string matchString)
{
......
第一题:
为管理业务培训信息,建立3个表:
S(S#,SN,SD,SA)S#,SN,SD,SA分别代表学号,学员姓名,所属单位,学员年龄
C(C#,CN)C#,CN分别代表课程编号,课程名称
SC(S#,C#,G) S#,C#,G分别代表学号,所选的课程编号,学习成绩
(1)使用标准SQL嵌套语句查询选修课程名称为’税收基础’的学员学号和姓名?
(2) ......
--1加内存表
EXEC sp_tableoption '表名','pintable', 'true'
--2卸载内存表
EXEC sp_tableoption '表名','pintable', 'false'
--2查询是否有内存表驻留
SELECT * from INFORMATION_SCHEMA.Tables
WHERE TABLE_TYPE = 'BASE TABLE'
AND OBJECTPROP ......
GRANT
Name
GRANT -- 定义访问权限
Synopsis GRANT { { SELECT | INSERT | UPDATE | DELETE | RULE | REFERENCES | TRIGGER }
[,...] | ALL [ PRIVILEGES ] }
ON [ TABLE ] tablename [, ...]
TO { username | GROUP groupname | PUBLIC } [, ...] [ WI ......