مشاركة عبر


ForeignKey Class

The ForeignKey object represents a SQL Server foreign key.

Inheritance Hierarchy

System.Object
  Microsoft.SqlServer.Management.Smo.SmoObjectBase
    Microsoft.SqlServer.Management.Smo.SqlSmoObject
      Microsoft.SqlServer.Management.Smo.NamedSmoObject
        Microsoft.SqlServer.Management.Smo.ScriptNameObjectBase
          Microsoft.SqlServer.Management.Smo.ForeignKey

Namespace:  Microsoft.SqlServer.Management.Smo
Assembly:  Microsoft.SqlServer.Smo (in Microsoft.SqlServer.Smo.dll)

Syntax

'Declaration
Public NotInheritable Class ForeignKey _
    Inherits ScriptNameObjectBase _
    Implements ICreatable, IDroppable, IMarkForDrop, IAlterable,  _
    IRenamable, IExtendedProperties, IScriptable, ISfcSupportsDesignMode
'Usage
Dim instance As ForeignKey
public sealed class ForeignKey : ScriptNameObjectBase, 
    ICreatable, IDroppable, IMarkForDrop, IAlterable, IRenamable, 
    IExtendedProperties, IScriptable, ISfcSupportsDesignMode
public ref class ForeignKey sealed : public ScriptNameObjectBase, 
    ICreatable, IDroppable, IMarkForDrop, IAlterable, IRenamable, 
    IExtendedProperties, IScriptable, ISfcSupportsDesignMode
[<SealedAttribute>]
type ForeignKey =  
    class 
        inherit ScriptNameObjectBase 
        interface ICreatable 
        interface IDroppable 
        interface IMarkForDrop 
        interface IAlterable 
        interface IRenamable 
        interface IExtendedProperties 
        interface IScriptable 
        interface ISfcSupportsDesignMode 
    end
public final class ForeignKey extends ScriptNameObjectBase implements ICreatable, IDroppable, IMarkForDrop, IAlterable, IRenamable, IExtendedProperties, IScriptable, ISfcSupportsDesignMode

The ForeignKey type exposes the following members.

Constructors

  Name Description
Public method ForeignKey() Initializes a new instance of the ForeignKey class.
Public method ForeignKey(Table, String) Initializes a new instance of the ForeignKey class on the specified table with the specified name.

Top

Properties

  Name Description
Public property Columns Represents a collection of Column objects. Each Column object represents a column included in the foreign key.
Public property CreateDate Gets the date and time when the foreign key was created.
Public property DateLastModified Gets the date and time when the foreign key was last modified.
Public property DeleteAction Gets or sets the action taken when the row that is referenced by the foreign key is deleted.
Public property ExtendedProperties Represents a collection of ExtendedProperty objects. Each ExtendedProperty object represents an extended property on the ForeignKey object.
Public property ID Gets the ID value that uniquely identifies the foreign key.
Public property IsChecked Gets or sets the Boolean property value that specifies whether the foreign key constraint was enabled without checking existing rows.
Public property IsEnabled Gets or sets the Boolean property value that specifies whether the foreign key is enabled.
Public property IsFileTableDefined Gets or sets whether the file table is defined.
Public property IsSystemNamed Gets the Boolean property value that specifies whether the foreign key is named by the system or by the user.
Public property Name Gets or sets the name of the foreign key. (Overrides NamedSmoObject.Name.)
Public property NotForReplication Gets the Boolean property value that specifies whether the foreign key is available for replication.
Public property Parent Gets or sets the Table object that is the parent of the ForeignKey object.
Public property Properties Gets a collection of Property objects that represent the object properties. (Inherited from SqlSmoObject.)
Public property ReferencedKey Gets the primary key referenced by the foreign key.
Public property ReferencedTable Gets or sets the table that contains the primary key referenced by the foreign key.
Public property ReferencedTableSchema Gets or sets the table schema that contains the primary key referenced by the foreign key.
Public property ScriptReferencedTable Gets or sets the Boolean value that specifies whether to script the table referenced by the foreign key.
Public property ScriptReferencedTableSchema Gets or sets the Boolean value that specifies whether to script the schema of the table referenced by the foreign key.
Public property State Gets the state of the referenced object. (Inherited from SmoObjectBase.)
Public property UpdateAction Gets or sets the Boolean property value that specifies whether the foreign key supports cascading updates.
Public property Urn Gets the Uniform Resource Name (URN) address value that uniquely identifies the object. (Inherited from SqlSmoObject.)
Public property UserData Gets or sets user-defined data associated with the referenced object. (Inherited from SmoObjectBase.)

Top

Methods

  Name Description
Public method Alter Updates any ForeignKey object property changes on instance of SQL Server. 
Public method Create Creates a foreign key on the instance of SQL Server as defined by the ForeignKey object.
Public method Discover Discovers a list of type Object. (Inherited from SqlSmoObject.)
Public method Drop Drops the foreign key.
Public method Equals (Inherited from Object.)
Protected method FormatSqlVariant Formats an object as SqlVariant type. (Inherited from SqlSmoObject.)
Protected method GetContextDB Gets the context database that is associated with this object. (Inherited from SqlSmoObject.)
Protected method GetDBName Gets the database name that is associated with the object. (Inherited from SqlSmoObject.)
Public method GetHashCode (Inherited from Object.)
Protected method GetPropValue Gets a property value of the SqlSmoObject object. (Inherited from SqlSmoObject.)
Protected method GetPropValueOptional Gets a property value of the SqlSmoObject object. (Inherited from SqlSmoObject.)
Protected method GetPropValueOptionalAllowNull Gets a property value of the SqlSmoObject object. (Inherited from SqlSmoObject.)
Protected method GetServerObject Gets the server of the SqlSmoObject object. (Inherited from SqlSmoObject.)
Public method GetType (Inherited from Object.)
Public method Initialize() Initializes the object and forces the properties be loaded. (Inherited from SqlSmoObject.)
Public method Initialize(Boolean) Initializes the object and forces the properties be loaded. (Inherited from SqlSmoObject.)
Protected method IsObjectInitialized Verifies whether the object has been initialized. (Inherited from SqlSmoObject.)
Protected method IsObjectInSpace Verifies whether the object is isolated or connected to the instance of SQL Server. (Inherited from SqlSmoObject.)
Public method MarkForDrop Marks the foreign key to be dropped the next time its parent Table object is altered by the Alter method.
Public method Refresh Refreshes the script name. (Inherited from ScriptNameObjectBase.)
Public method Rename Renames the foreign key to the specified new name.
Public method Script() Generates a Transact-SQL script that can be used to re-create the foreign key.
Public method Script(ScriptingOptions) Generates a Transact-SQL script that can be used to re-create the foreign key as specified by the script options. 
Protected method SetParentImpl Sets the parent of the SqlSmoObject to the newParent parameter. (Inherited from SqlSmoObject.)
Public method ToString Returns a String that represents the referenced object. (Inherited from SqlSmoObject.)
Public method Validate Validates the state of an object. (Inherited from SmoObjectBase.)

Top

Events

  Name Description
Public event PropertyChanged Represents the event that occurs when a property is changed. (Inherited from SqlSmoObject.)
Public event PropertyMetadataChanged Represents the event that occurs when property metadata changes. (Inherited from SqlSmoObject.)

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate method IAlienObject.Discover Discovers any dependencies. Do not reference this member directly in your code. It supports the SQL Server infrastructure. (Inherited from SqlSmoObject.)
Explicit interface implemetationPrivate method IAlienObject.GetDomainRoot Returns the root of the domain. (Inherited from SqlSmoObject.)
Explicit interface implemetationPrivate method IAlienObject.GetParent Gets the parent of this object. Do not reference this member directly in your code. It supports the SQL Server infrastructure. (Inherited from SqlSmoObject.)
Explicit interface implemetationPrivate method IAlienObject.GetPropertyType Gets the type of the specified property. (Inherited from SqlSmoObject.)
Explicit interface implemetationPrivate method IAlienObject.GetPropertyValue Gets the value of the specified property. (Inherited from SqlSmoObject.)
Explicit interface implemetationPrivate method IAlienObject.GetUrn Gets the Unified Resource Name (URN) of the object. Do not reference this member directly in your code. It supports the SQL Server infrastructure. (Inherited from SqlSmoObject.)
Explicit interface implemetationPrivate method IAlienObject.Resolve Gets the instance that contains the information about the object from the Unified Resource Name (URN) of the object. (Inherited from SqlSmoObject.)
Explicit interface implemetationPrivate method IAlienObject.SetObjectState Sets the object state to the specified SfcObjectState value. (Inherited from SqlSmoObject.)
Explicit interface implemetationPrivate method IAlienObject.SetPropertyValue Sets the property value. (Inherited from SqlSmoObject.)
Explicit interface implemetationPrivate method ISfcPropertyProvider.GetPropertySet Gets the interface reference to the set of properties of this object. (Inherited from SqlSmoObject.)
Explicit interface implemetationPrivate property ISfcSupportsDesignMode.IsDesignMode Gets a Boolean value that indicates whether the object supports design mode. Identified for informational purposes only. Not supported. Future compatibility is not guaranteed.

Top

Remarks

To get ForeignKey object properties, users can be a member of the public fixed server role.

To set ForeignKey object properties, users must have ALTER permission on the parent table or be a member of the db_owner fixed database role.

To create or drop a foreign key, users must have ALTER permission on the parent table or be a member of the db_owner fixed database role.

Thread Safety

Any public static (Shared in Microsoft Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe.

Examples

Creating, Altering, and Removing Foreign Keys

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Microsoft.SqlServer.Management.Smo Namespace

Other Resources

Creating, Altering, and Removing Foreign Keys

CREATE TABLE (Transact-SQL)