0

I want a column in sql to be the sum of 2 columns in the same table. For example: columns : score, assists, rebound While creating this table ı want score = assist + rebound value.

CREATE TABLE macDetay (
macID INT , oyuncuID INT , 
CONSTRAINT PKDETAY PRIMARY KEY (macID, oyuncuID),
CONSTRAINT FK1 FOREIGN KEY(macID) REFERENCES Mac(macID), 
CONSTRAINT FK2 FOREIGN KEY(oyuncuID) REFERENCES Oyuncu(oyuncuID),
macSkor INT, asistSayisi INT,reboundSayisi INT, 
CONSTRAINT skor CHECK (macSkor = asistSayisi+ReboundSayisi))

I'm forcing to enter the sum in my way but I want it to be automatic

Anirban Saha
  • 1,350
  • 2
  • 10
  • 38
  • 1
    You can use the computed column in a table as shown [here](https://learn.microsoft.com/en-us/sql/relational-databases/tables/specify-computed-columns-in-a-table?view=sql-server-ver15) – Suraj Kumar Jan 24 '21 at 09:23

1 Answers1

1

You can add computed column in your statement. It's simple. computedColumnName as (col1 + col2)

CREATE TABLE macDetay (
    macID INT
    ,oyuncuID INT
    ,CONSTRAINT PKDETAY PRIMARY KEY (
        macID
        ,oyuncuID
        )
    ,CONSTRAINT FK1 FOREIGN KEY (macID) REFERENCES Mac(macID)
    ,CONSTRAINT FK2 FOREIGN KEY (oyuncuID) REFERENCES Oyuncu(oyuncuID)
    ,macSkor AS (asistSayisi + ReboundSayisi)
    ,asistSayisi INT
    ,reboundSayisi INT
    )
Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42