Named Default constraint for Identity - sql

Let's say I have following table creation script. I would like to have a named default constraint for the EmployeeID column to use Identity (1,1)
CREATE TABLE dbo.Employee
(
EmployeeID INT NOT NULL ,
LastName VARCHAR(100) NOT NULL,
FirstName VARCHAR(100) NOT NULL,
CONSTRAINT PK_Employee PRIMARY KEY NONCLUSTERED
(
EmployeeID ASC
)
)
GO
ALTER TABLE Employee ADD CONSTRAINT UDF_Employee_EmployeeID DEFAULT IDENTITY(1,1) FOR EmployeeID
GO
I am getting following error
Incorrect syntax near the keyword 'IDENTITY'
What am I doing wrong here?

This should do it:
CREATE TABLE dbo.Employee
(
EmployeeID INT NOT NULL IDENTITY(1,1),
LastName VARCHAR(100) NOT NULL,
FirstName VARCHAR(100) NOT NULL,
CONSTRAINT PK_Employee
PRIMARY KEY NONCLUSTERED (EmployeeID ASC)
)
DEFAULT IDENTITY is not proper syntax and since IDENTITY is a column property, not a constraint, you can't add it using ALTER (or at all after column creation).

Related

ALTER TABLE statement conflicted with the FOREIGN KEY constraint

When creating the foreign key I came across this error. Below is my code.
create table tblPerson(
ID int not null primary key,
Fullname varchar(50) not null,
Email varchar(50) not null,
GenderId int
)
create table tblGender (
ID int not null primary key,
Gender varchar(50) not null
)
alter table tblPerson add constraint tblPerson_GenderId_FK
foreign key (GenderId) references tblGender(ID)
You want to identify any "do not align" rows....
I have made the below.
You won't be able to add the FK constraint, if any rows come back from the SELECT query.
I have also removed the hungarian notation for "tbl". I would advise against it.
create table dbo.Person(
ID int not null primary key,
Fullname varchar(50) not null,
Email varchar(50) not null,
GenderId int )
create table dbo.Gender (
ID int not null primary key,
Gender varchar(50) not null
)
/* any rows below? your FK creation will fail */
Select *, p.GenderId as 'HoustonWeHaveAProblemValue' from dbo.Person p Where Not Exists (Select 1 from dbo.Gender g where g.ID = p.GenderId)
alter table dbo.Person add constraint Person_GenderId_FK
foreign key (GenderId) references dbo.Gender(ID)
Hi please use the following code to achieve your goal :
1-First create tblGender:
create table tblGender (
ID int not null primary key,
Gender varchar(50) not null
)
2-Then create table tblPerson with the relationship between 2 tables since the beginning:
create table tblPerson(
ID int not null primary key,
Fullname varchar(50) not null,
Email varchar(50) not null,
GenderId int references tblGender(ID)
)
works fine.

Creating new cluster table on file group

I keep getting an error message incorrect syntax near ON. Can someone please help me figuring out why?
CREATE TABLE [CustomerService.Contacts]
(
--A
ContactID int IDENTITY(1,1),
CONSTRAINT PK_ContactID PRIMARY KEY CLUSTERED (ContactID),
--B
CustomerID int not null,
CONSTRAINT FK_CustomerID FOREIGN KEY (CustomerID)
REFERENCES Sales.Customer (CustomerID),
--C
RepID int not null,
CONSTRAINT FK_RepID FOREIGN KEY (RepID)
REFERENCES CustomerService.Reps (RepID),
--D
ContactDateTime date not null,
--E
ContactMethod varchar(5) DEFAULT 'Other' not null,
CHECK (ContactMethod IN ('Email', 'Phone', 'Chat', 'Other')),
--F
ContactPhone varchar(14) null,
--G
ContactEmail varchar(50) null,
--H
ContactDetail varchar(MAX) not null,
ON AD_CustomerService;
GO
ALTER TABLE CustomerService.Contacts
REBUILD PARTITION = ALL WITH (DATA_COMPRESSION = PAGE);
GO
You never closed the parenthesis you opened after the first line:
CREATE TABLE [CustomerService.Contacts]
(
so you are probably missing a close parenthesis before the ON keyword:
...
ContactDetail varchar(MAX) not null,
) -- <---this was missing
ON AD_CustomerService;
GO
ALTER TABLE CustomerService.Contacts
REBUILD PARTITION = ALL WITH (DATA_COMPRESSION = PAGE);
GO

Foreign key references invalid column error

I'm trying to run this query but SQL -erver stopped me with this error:
Foreign key 'FK__food__groupid__3F115E1A' references invalid column 'groupid' in referenced table 'sub'.
This is my query:
create table menu
(
valedid int primary key not null,
name nvarchar(50) not null,
)
create table sub
(
qroupid int primary key not null,
groupname nvarchar(50) not null,
valedid int not null,
foreign key(valedid) references menu (valedid),
)
create table food
(
foodid int primary key not null,
radif int identity(1,1) not null,
qeymat int not null,
name nvarchar(100) not null,
groupid int not null,
foreign key(groupid) references sub(groupid),
)
You create the column with name qroupid and not groupid in table sub

SQL Server 2012 : Create Table

CREATE TABLE Schedule
(
Section DATETIME NOT NULL PRIMARY KEY(CourseID, Section, EmployeeID),
CourseID VARCHAR(10) REFERENCES Course(CourseID) NOT NULL,
EmployeeID VARCHAR(20) NOT NULL REFERENCES Employee(EmployeeID),
StartTime TIME NULL,
Days DATE NULL,
Length TIME NULL
)
CREATE TABLE Enrollment
(
StudentID INT Primary key (StudentID, CourseID, Section) NOT NULL,
CourseID VARCHAR(10) REFERENCES Course(CourseID) NOT NULL,
Section DATETIME NOT NULL REFERENCES Schedule(Section)
)
2nd table did not get created, where did I go wrong?
Its because you made the primary key in the Schedule table a natural/combined key.
Try creating a stand alone column for this purpose instead. I've included an example below that shows the differences.
CREATE TABLE DBO.PK_TEST (
Col_A INT NOT NULL
,Col_B INT NOT NULL
,Primary Key(Col_A,Col_B)
)
Create table DBO.PK_TEST_2 (
Col_A int NOT NULL
,Col_B int NOT NULL
,Col_C as (cast(Col_A as nvarchar)
+ cast(Col_B as nvarchar)) PERSISTED NOT NULL
,primary key(Col_C)
)

Retrieve Data from Different Tables (SQL Server)

I have 4 tables in a SQL Server database with following schema:
Attendance
CREATE TABLE [dbo].[Attendance] (
[AttendanceId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
[CourseId] UNIQUEIDENTIFIER NOT NULL,
[StudentId] UNIQUEIDENTIFIER NOT NULL,
[SubjectId] UNIQUEIDENTIFIER NOT NULL,
[Semester] INT NOT NULL,
[Month] NVARCHAR (50) NOT NULL,
[Count] INT NOT NULL,
CONSTRAINT [PK_Attendance] PRIMARY KEY NONCLUSTERED ([AttendanceId] ASC),
CONSTRAINT [FK_Attendance_Student] FOREIGN KEY ([StudentId]) REFERENCES [dbo].[Student] ([StudentId]) );
Course
CREATE TABLE [dbo].[Course] (
[CourseId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
CONSTRAINT [PK_Course] PRIMARY KEY NONCLUSTERED ([CourseId] ASC)
);
Student
CREATE TABLE [dbo].[Student] (
[StudentId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
[CourseId] UNIQUEIDENTIFIER NOT NULL,
[Name] NVARCHAR (100) NOT NULL,
[RollNo] INT NOT NULL,
[Semester] INT NOT NULL,
CONSTRAINT [PK_Student] PRIMARY KEY NONCLUSTERED ([StudentId] ASC),
CONSTRAINT [FK_Student_Course] FOREIGN KEY ([CourseId]) REFERENCES [dbo].[Course] ([CourseId])
);
Subject
CREATE TABLE [dbo].[Subject] (
[SubjectId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
[CourseId] UNIQUEIDENTIFIER NOT NULL,
[Name] NVARCHAR (100) NOT NULL,
[Semester] INT NOT NULL,
CONSTRAINT [PK_Subject] PRIMARY KEY NONCLUSTERED ([SubjectId] ASC),
CONSTRAINT [FK_Subject_Course] FOREIGN KEY ([CourseId]) REFERENCES [dbo].[Course] ([CourseId])
);
I need to create a attendance report in the following format:
Course Name | Student Name | Subject Name | Semester | Month | Count
Please tell me what SQL Query I need to use and if there's any change in schema required then suggest the same.
I'm looking forward to have your replies.
Thanks,
You need to use a JOIN in your query so that it only returns rows which match a [StudentId] from the Attendance table.
e.g.
SELECT c.CourseName, s.StudentName, u.SubjectName, u.Semester, a.Month, a.Count
FROM Student s
JOIN Attendance a ON s.StudentId = a.StudentId
JOIN Course c ON a.CourseId = c.CourseId
JOIN Subject u ON c.CourseId = u.CourseId
Something along these lines will only return rows which specifically match