I want to copy a column generated by identity function, identity(1,1), to another table. However, after the process, the whole new column only shows 1.
Is there a way to fix that? thank you
CREATE TABLE Dim_Route(RtID INT Identity(1,1) Primary Key, Itinerary varchar(50) NOT NULL, )
CREATE TABLE Dim_FlightSchedule(FSID INT Identity(1,1) Primary Key, RTID INT, Constraint fk_Route Foreign KEY (RTID) REFERENCES Dim_Route(RtID), )
insert Dim_Route (Itinerary)
values
('B'),
('C'),
('D'),
('E'),
('F')
insert Dim_FlightSchedule (RTID)
select RTID from Dim_Route
Here is my code:
The original table:CREATE TABLE Dim_Route(RtID INT Identity(1,1) Primary Key,
Itinerary varchar(50) NOT NULL,
)
The destination table:CREATE TABLE Dim_FlightSchedule(FSID INT Identity(1,1) Primary Key,
RTID INT,
Constraint fk_Route Foreign KEY (RTID) REFERENCES
Dim_Route(RtID),
)
UPDATE Dim_FlightSchedule
SET Dim_FlightSchedule.RTID = Dim_Route.RTID
FROM Dim_Route
The RTID in destination table are all 1.
I don't know where I got wrong. Thank you
Related
Table 1:
CREATE TABLE News
(
News_Id int PRIMARY KEY IDENTITY(1,1),
Title nvarchar(200) NOT NULL,
Author nvarchar(50),
Calender DATETIME NOT NULL,
Contents nvarchar(20) NOT NULL, --store link to text file
[Type_Id] int ,
FOREIGN KEY ([Type_Id]) REFERENCES [Types]([Type_Id])
)
Table 2:
CREATE TABLE [Types]
(
[Type_Id] int,
Name nvarchar(50),
PRIMARY KEY ([Type_Id])
)
I get this error:
FK_News references invalid table Types
Can you help me?
You have to create Type table first and then News tables, as you are referencing foreign key from Type table to News table.
Table1:
CREATE TABLE [Types](
[Type_Id] int,
Name nvarchar(50),
PRIMARY KEY ([Type_Id])
)
Table2:
CREATE TABLE News(
News_Id int PRIMARY KEY IDENTITY(1,1),
Title nvarchar(200) NOT NULL,
Author nvarchar(50),
Calender DATETIME NOT NULL,
Contents nvarchar(20) NOT NULL, --store link to text file
[Type_Id] int ,
FOREIGN KEY ([Type_Id]) REFERENCES [Types]([Type_Id])
)
Since you have created News table first, drop it and recreate it. Or create both the tables without constraints and then alter both the tables with primary key ad foreign key receptively
Since the Types table seems to be the primary one, you should create it first:
CREATE TABLE [Types](
[Type_Id] int,
Name nvarchar(50),
PRIMARY KEY ([Type_Id])
);
Then, create the News table, which has foreign keys referring to Types above:
CREATE TABLE News(
News_Id int PRIMARY KEY IDENTITY(1,1),
Title nvarchar(200) NOT NULL,
Author nvarchar(50),
Calender DATETIME NOT NULL,
Contents nvarchar(20) NOT NULL, --store link to text file
[Type_Id] int ,
FOREIGN KEY ([Type_Id]) REFERENCES [Types]([Type_Id])
);
It isn't possible to point a foreign key to a table which does not yet exist.
I'm currently working through an assignment that is asking me to create a SQL database using SQL Server Express. This is what it's asking for:
CREATE DATABASE USING SQL
And this is the code I have tried running;
CREATE DATABASE db_Library
Go
USE db_Library
CREATE TABLE tbl_library_branch
(
library_branch_branch_id INT PRIMARY KEY NOT NULL IDENTITY (1,1),
library_branch_branch_name VARCHAR(50) NOT NULL,
library_branch_address VARCHAR(50) NOT NULL
);
CREATE TABLE tbl_publisher
(
library_publisher_publisher_name VARCHAR(50) PRIMARY KEY NOT NULL,
library_publisher_address VARCHAR(50) NOT NULL,
library_publisher_phone INT NOT NULL
);
CREATE TABLE tbl_books
(
library_books_book_id INT PRIMARY KEY NOT NULL IDENTITY (1,1),
library_books_title VARCHAR(50) NOT NULL,
library_books_publisher_name VARCHAR(50) NOT NULL CONSTRAINT fk_library_books_publisher_name FOREIGN KEY REFERENCES tbl_publisher(library_publisher_publisher_name) ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE tbl_book_authors
(
library_book_authors_book_id INT NOT NULL CONSTRAINT fk_library_book_authors_book_id FOREIGN KEY REFERENCES tbl_books(library_books_book_id) ON UPDATE CASCADE ON DELETE CASCADE,
library_book_authors_author_name VARCHAR(50) NOT NULL
);
CREATE TABLE tbl_book_copies
(
library_book_copies_book_id INT NOT NULL CONSTRAINT fk_library_book_copies_book_id FOREIGN KEY REFERENCES tbl_books(library_books_title),
library_book_copies_branch_id INT NOT NULL,
library_book_copies_number_of_copies INT NOT NULL
);
CREATE TABLE tbl_book_loans
(
library_book_loans_book_id INT NOT NULL,
library_book_loans_branch_id INT NOT NULL,
library_book_loans_card_no INT NOT NULL,
library_book_loans_date_out INT NOT NULL,
library_book_loans_date_due INT NOT NULL
);
CREATE TABLE tbl_borrower
(
library_borrower_card_no INT PRIMARY KEY NOT NULL IDENTITY (1,1),
library_borrower_name VARCHAR(50) NOT NULL,
library_borrower_address VARCHAR(50) NOT NULL,
library_borrower_phone VARCHAR(50) NOT NULL
);
Using the library "Books" as an example it looks like I need to have BoodID as a primary key and Title as a primary key, but you can't have more than one primary key per table..
Then I have to take BookID from the Book_Copies table and the Book_Loans table connect to the primary key of Title in Books?
I'm beyond stuck at this point and would appreciate any resources you think could help.
There is no need to be so verbose. I think you want something more like this (for two tables):
CREATE TABLE tbl_publisher (
publisher_id int IDENTITY(1, 1) PRIMARY KEY,
publisher_name VARCHAR(50) NOT NULL UNIQUE,
address VARCHAR(50) NOT NULL,
phone INT NOT NULL
);
CREATE TABLE books (
book_id INT IDENTITY (1,1) PRIMARY KEY,
title VARCHAR(50) NOT NULL UNIQUE,
publisher_id INT NOT NULL CONSTRAINT fk_books_publisher__id FOREIGN KEY REFERENCES tbl_publisher(publisher_id) ON UPDATE CASCADE ON DELETE CASCADE
);
Notes:
Stick with synthetic primary keys for all the tables. That is, identity columns.
Other columns can be declared to be unique. That is fine.
I type quite fast. And yet I would quickly wary of typing library_book_ over and over. Such repetition just makes it harder to write and read queries.
CREATE TABLE TBLTeams
(
teamnm varchar(30) NOT NULL,
teamid varchar(10),
rondenm varchar(10)
primary key(teamnm)
)
CREATE TABLE TBLWedstrijd
(
thuisteamnm varchar(30) NOT NULL,
uitteamnm varchar(30) NOT NULL,
wedstrijdid varchar(10)
primary key(wedstrijdid)
)
alter table TBLWedstrijd add foreign key (thuisteamnm) references TBLTeams(teamnm)
alter table TBLWedstrijd add foreign key (uitteamnm) references TBLTeams(teamnm)
CREATE TABLE TBLUitslag
(
thuisteamnm varchar(30) NOT NULL,
uitteamnm varchar(30) NOT NULL,
wedstrijdid varchar(10) NOT NULL,
uitteampunt int,
thuisteampunt int
primary key(thuisteamnm)
)
alter table TBLUitslag add foreign key (wedstrijdid) references TBLWedstrijd(wedstrijdid)
If I update teamnm in TBLTeams how can I update reference in TBLWedstrijd uitteamnm. I know its a subquery but I really dont know how.
You're going about it a bit wrong. If you're going to change the teamname, you shouldn't use it as a key (Since that will break all references).
You'll want to reference their IDs from the TBLWedstrijd instead of using their name.
So your statements will become:
CREATE TABLE TBLWedstrijd
(
wedstrijdid varchar(10),
thuisteamid varchar(10) NOT NULL,
uitteamid varchar(10) NOT NULL,
primary key(wedstrijdid)
)
alter table TBLWedstrijd add foreign key (thuisteamid) references TBLTeams(teamid)
alter table TBLWedstrijd add foreign key (uitteamid) references TBLTeams(teamid)
The same is valid for your table TBLUitslag, you should only reference TBLWedstrijd, since that one already holds the info about which teams are playing. Not to mention, the score is pretty much part of a game anyway so it doesn't make a lot of sense to seperate those into 2 tables. So that becomes:
CREATE TABLE TBLWedstrijd
(
wedstrijdid varchar(10),
thuisteamid varchar(10) NOT NULL,
uitteamid varchar(10) NOT NULL,
uitteampunt int,
thuisteampunt int
primary key(wedstrijdid)
)
This is called "table normalisation" if you wanted to google some stuff for more info about it.
In previous versions of Visual studio there was a way to build DB using diagrams.
In 2012 they took out that possibility.
I need to create script which creates table for category tree.
CREATE TABLE [dbo].[Categories]
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
[Name] NVARCHAR(50) NOT NULL,
[ParentId] INT NOT NULL DEFAULT 0,
[FK_Parent] int FOREIGN KEY ([ParentId]) REFERENCES [Categories]([Id]) NOT NULL
)
If I create table like this, I cannot add top level records.
Define top-level records as having a NULL FK_Parent. Just change your definition to allow nulls.
CREATE TABLE [dbo].[Categories]
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
[Name] NVARCHAR(50) NOT NULL,
[ParentId] INT NOT NULL ,
---llow NULL FK_Parent for top-level
CONSTRAINT [FK_Parent] FOREIGN KEY ([ParentId]) REFERENCES [Categories]([Id])
)
CREATE DATABASE agom COLLATE Arabic_CI_AS
CREATE TABLE Branches
(
ID INT PRIMARY KEY NOT NULL IDENTITY,
NAME VARCHAR(255) NOT NULL
)
CREATE TABLE agom.Brands
(
ID INT PRIMARY KEY NOT NULL IDENTITY,
NAME VARCHAR(255) NOT NULL
)
CREATE TABLE agom.Work_Order
(
NUMBER INT NOT NULL,
BRANCHID INT NOT NULL,
BRANDID INT NOT NULL,
WDATE DATE NOT NULL,
REPAIRSTATUS VARCHAR(255),
REPAIRCOST VARCHAR(255),
REMARK VARCHAR(500),
PRIMARY KEY (NUMBER,BRANCHID,BRANDID)
)
CREATE TABLE agom.Profiles
(
ID INT PRIMARY KEY NOT NULL IDENTITY,
USERNAME VARCHAR(25) NOT NULL,
PASS VARCHAR(25) NOT NULL
)
ALTER TABLE agom.Work_Order
ADD CONSTRAINT branchfk
FOREIGN KEY (BRANCHID) REFERENCES Branches(ID)
ALTER TABLE agom.Work_Order
ADD CONSTRAINT brandfk
FOREIGN KEY (BRANDID) REFERENCES Brands(ID)
I get an error that cannot create table
I try to write database name with the table name db.tablename but it's not working
I need to create the database then create the tables and its constraints but I don't know where is the error.I am a sql noob
It's never Database.Table.
It's either Table, or Schema.Table, or Database.Schema.Table, or Server.Database.Schema.Table.
You probably just want to insert USE agom right after create database, and then only refer to tables by name.
Alternatively, you can refer to your tables as agom.dbo.Work_Order, dbo being the default database schema.
See Using Identifiers As Object Names for general reference.