Add column as Primary Key

2

I ran the following script:

CREATE TABLE [dbo].[horario](
    [Ano] [int] NOT NULL,
    [CodigoTurma] [varchar](5) NOT NULL,
    [Ordem] [int] NOT NULL,
    [Professor_Id] [int] NOT NULL,
    [Matriz_Semestre] [int] NOT NULL,
    [Matriz_Curso_Codigo] [varchar](5) NOT NULL,
    [Matriz_Disciplina_Codigo] [varchar](5) NOT NULL,
 CONSTRAINT [primary key1] PRIMARY KEY CLUSTERED 
(
    [Ano] ASC,
    [Professor_Id] ASC,
    [Matriz_Semestre] ASC,
    [Matriz_Curso_Codigo] ASC,
    [Matriz_Disciplina_Codigo] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

I can not remove the created table, so to add a new column I ran the following command:

ALTER TABLE horario ADD Dia [int] NOT NULL

The problem is that I need this new column to also be part of the primary key set. Can anyone help me?

For some reason the bank looked like this:

    
asked by anonymous 09.06.2015 / 05:37

1 answer

3

Just drop the primary key and create it again:

ALTER TABLE horario
DROP CONSTRAINT [primary key1]
GO

ALTER TABLE horario
ADD CONSTRAINT [primary key1] PRIMARY KEY CLUSTERED
(
    [Ano] ASC,
    [Professor_Id] ASC,
    [Matriz_Semestre] ASC,
    [Matriz_Curso_Codigo] ASC,
    [Matriz_Disciplina_Codigo] ASC,
    [Dia] ASC
)
GO

To check the primary key name, use:

SELECT * from 
    INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab
WHERE 
    Tab.Table_Name = 'horario'
    AND Constraint_Type = 'PRIMARY KEY'
    
09.06.2015 / 05:49