SQL Create Table Columns [duplicate] - sql

This question already has answers here:
Creating a computed column in SQL Server 2008
(2 answers)
Closed 2 years ago.
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

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
)

Related

Column exist but program don t see? [duplicate]

This question already has answers here:
SQLite Foreign Key
(5 answers)
Closed 1 year ago.
I created a 3 tables but in the last one shows errors
CREATE TABLE Student
(
St_Id char(7) PRIMARY KEY,
St_Fname varchar(15) NOT NULL,
St_Lname varchar(20) NOT NULL,
St_DOB date
)
CREATE TABLE Course
(
Course_code char(5) PRIMARY KEY,
Course_title varchar(30) NOT NULL,
Course_credit INTEGER NOT NULL
)
CREATE TABLE Registration
(
Reg_no INTEGER PRIMARY KEY AUTOINCREMENT ,
FOREIGN KEY (St_Id) REFERENCES Student(St_Id),
FOREIGN KEY (Course_code) REFERENCES Course(Course_code),
Mark_obtaines INTEGER
)
and error is
Execution finished with errors. Result: unknown column "St_Id" in
foreign key definition At line 1: CREATE TABLE Registration ( Reg_no
INTEGER PRIMARY KEY AUTOINCREMENT , FOREIGN KEY (St_Id) REFERENCES
Student(St_Id),
In order to define a foreign key, you need to define the column first -- and the types need to match the type in the referenced table:
CREATE TABLE Registration (
Reg_no INTEGER PRIMARY KEY AUTOINCREMENT,
St_Id CHAR(7),
Course_Code CHAR(5),
FOREIGN KEY (St_Id) REFERENCES Student(St_Id),
FOREIGN KEY (Course_code) REFERENCES Course(Course_code),
Mark_obtaines INTEGER
);
Then the FOREIGN KEY declaration provides more information about the column.

Why can not I use two foreign key in one table? [duplicate]

This question already has answers here:
More than one key specified in column level FOREIGN KEY constraint
(2 answers)
Closed 2 years ago.
I try to write it:
CREATE TABLE Man
(
ID NUMERIC(9) PRIMARY KEY
);
CREATE TABLE Woman
(
ID NUMERIC(9) PRIMARY KEY
);
CREATE TABLE MotherOf
(
MID NUMERIC(9),
FID NUMERIC(9)
PRIMARY KEY(MID, FID)
FOREIGN KEY (MID) REFERENCES Woman(ID)
FOREIGN KEY (FID) REFERENCES Man(ID)
)
and I get this error:
Multiple FOREIGNKEY constrains were specified for columns 'FID', table 'MotherOf'.
Why can't I do this?
Try it with it looking like:
CREATE TABLE MotherOf(
MID NUMERIC(9) FOREIGN KEY REFERENCES Woman(ID),
FID NUMERIC(9) FOREIGN KEY REFERENCES Man(ID)
PRIMARY KEY(MID, FID)
)

How to link tables in SQL

I have two tables and i want to create a third that references the other two.
stock_group has groupno as primary key
stock_group2s also has groupno as primary key
I want to create table called stock_group3s, with 5 columns:
key int AUTO INCREMENT PRIMARY KEY
groupno INT
group1 [stock_group.groupno]
group2 [stock_group2s.groupno]
name VARCHAR (30)
The two linked columns then must contain only a value that can be found in their referenced tables.
How do set this up? I am using SQL Server 2008 R2 with SQL Server 2008 Management Studio
perhaps you are looking for
create table stock_group (groupno int primary key)
create table stock_group2s(groupno int primary key)
create table stock_group3s
(
IDkey int PRIMARY KEY IDENTITY(1,1)
,groupno INT
,group1 INT
,group2 INT
,name VARCHAR (30)
)
ALTER TABLE stock_group3s WITH NOCHECK ADD CONSTRAINT FK_stock_group3s_stock_group FOREIGN KEY(group1)
REFERENCES stock_group (groupno)
ALTER TABLE stock_group3s WITH NOCHECK ADD CONSTRAINT FK_stock_group3s_stock_group2s FOREIGN KEY(group2)
REFERENCES stock_group2s (groupno)

SQL Server : set primary key without dropping table and content [duplicate]

This question already has answers here:
SQL Server add auto increment primary key to existing table
(16 answers)
Add primary key column in SQL table
(3 answers)
Closed 9 years ago.
Is it possible to set the primary key and auto increment on a SQL Server table without dropping and recreating the table, and losing all it's data?
Yes of course! You just add a new column, and it an INT IDENTITY and add a primary key constraint to it:
ALTER TABLE dbo.YourTable
ADD ID INT IDENTITY(1,1) NOT NULL
ALTER TABLE dbo.YourTable
ADD CONSTRAINT PK_YourTable PRIMARY KEY (ID)
If there is an existing primary key, you must first drop it:
IF EXISTS (SELECT * FROM sys.key_constraints
WHERE type = 'PK' AND parent_object_id = OBJECT_ID('MyTable')
AND Name = 'PK_MyTable')
ALTER TABLE MyTable DROP CONSTRAINT PK_MyTable
If you are adding a column to be used as a primary key, then you can simply add it:
ALTER TABLE MyTable ADD MyKey INT IDENTITY
Then, you can set this column as your table's primary key:
ALTER TABLE MyTable ADD CONSTRAINT PK_MyTable PRIMARY KEY(MyKey)

SQL Multiple Foreign Keys as Primary Keys

If I declare the table below does it implicitly imply that both the foreign keys make a unique primary key or do I need to do something more to make both attributes as a primary key?
CREATE TABLE Report_has_Items
(
ReportID int REFERENCES Report(ReportID) NOT NULL,
ItemID int REFERENCES Item(ItemID) NOT NULL
)
Essentially both attributes which are foreign keys from other tables, together would form a unique key.
No it doesn't. The above table has no primary key. If you want to use the fields as a primary key use:
CREATE TABLE Report_has_Items(
ReportID int REFERENCES Report(ReportID) NOT NULL,
ItemID int REFERENCES Item(ItemID) NOT NULL,
PRIMARY KEY (ReportID, ItemID)
)
or something similar depending on your sql dilect.
Let's name our constraints, eh?
CREATE TABLE dbo.Report_has_Items(
ReportID int NOT NULL,
CONSTRAINT [FK_RHI_Report] (ReportId) REFERENCES dbo.Report(ReportID),
ItemID int NOT NULL,
Constraint [FK_RHI_Item] (ItemId) REFERENCES dbo.Item(ItemID),
CONSTRAINT [PK_RHI] PRIMARY KEY (ReportID, ItemID)
)
I am not sure i understand your question completely but i assume you are trying to create a composite primary key (primary key with more than one attribute). You could do the following.
CREATE TABLE Report_has_Items(
ReportID int references Report(ReportID),
ItemID int references Item(ItemID),
PRIMARY KEY (ReportID , ItemID )
);
Note:The pair (ReportID , ItemID ) must then be unique for the table and neither value can be NULL.
Here is a very useful link for SQL Queries