Creating new cluster table on file group - sql

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

Related

there is already an object named '' in the database

This is my code:
CREATE TABLE supplier -- creating table supplier
(
supplierID INT NOT NULL IDENTITY,
supplierName VARCHAR(30) NOT NULL,
suppplierNo VARCHAR(10) NOT NULL,
supplierEmail VARCHAR(30) NOT NULL,
CONSTRAINT PK_supplierID PRIMARY KEY(supplierID)
)
GO
I get the the error:
Msg 2714, Level 16, State 6, Line 34
There is already an object named 'supplier' in the database.
Any help? Thanks!
Please try this code.
IF EXISTS(SELECT 1 FROM sys.tables WHERE name = 'supplier')
DROP TABLE dbo.supplier;
CREATE TABLE dbo.supplier
(
supplierID INT NOT NULL IDENTITY,
supplierName VARCHAR(30) NOT NULL,
suppplierNo VARCHAR(10) NOT NULL,
supplierEmail VARCHAR(30) NOT NULL,
CONSTRAINT PK_supplierID PRIMARY KEY(supplierID)
)
GO
You need to check if the table exists first
IF OBJECT_ID('dbo.supplier', 'U') IS NOT NULL
DROP TABLE dbo.supplier;
CREATE TABLE dbo.supplier
(
supplierID INT NOT NULL IDENTITY,
supplierName VARCHAR(30) NOT NULL,
suppplierNo VARCHAR(10) NOT NULL,
supplierEmail VARCHAR(30) NOT NULL,
CONSTRAINT PK_supplierID PRIMARY KEY(supplierID)
)
GO
If you are using 2016+ you can use
DROP TABLE IF EXISTS dbo.supplier;
CREATE TABLE dbo.supplier
(
supplierID INT NOT NULL IDENTITY,
supplierName VARCHAR(30) NOT NULL,
suppplierNo VARCHAR(10) NOT NULL,
supplierEmail VARCHAR(30) NOT NULL,
CONSTRAINT PK_supplierID PRIMARY KEY(supplierID)
)
GO

Named Default constraint for Identity

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).

Error in creating tables in SQL

I'm trying to create a few tables in a new database, but when I try to create them it creates a couple of errors. I'm currently using Microsoft SQL Server Management Studio.
The errors seem to be in the end of the code where I'm trying to add constraints for foreign keys. Any help would be greatly appreciated thank you.
Here's the code, it's meant to generate 3 tables with 1 table containing 2 foreign keys to the other tables with matching column names.
CREATE TABLE Customers
(
CustomerID INT NOT NULL PRIMARY KEY IDENTITY,
ContactName VarChar(50) NULL,
Company VarChar(45) NULL,
Phone VarChar(12) NULL,
)
CREATE TABLE Shippers
(
ShipperID INT NOT NULL PRIMARY KEY IDENTITY,
Company VarChar(45) NULL,
Phone VarChar(12) NULL,
)
CREATE TABLE Orders
(
OrderID INT NOT NULL PRIMARY KEY IDENTITY,
OrderDate DATETIME NULL,
ShippedDate DATETIME NULL,
ShipperID INT NULL,
Freight DECIMAL NULL,
CustomerID INT NULL,
CONSTRAINT fk_Orders_Shippers
FOREIGN KEY ShipperID
REFERENCES Shippers(ShipperID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
CONSTRAINT fk_Orders_Customers
FOREIGN KEY CustomerID
REFERENCES Customers(CustomerID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
And these are the errors I get:
Msg 102, Level 15, State 1, Line 21
Incorrect syntax near 'ShipperID'.
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near 'ACTION'.
Msg 102, Level 15, State 1, Line 28
Incorrect syntax near 'ACTION'.
Any ideas what the problem is?
The error messages point you to a couple of surplus commas, a missing comma and improper foreign key syntax due to missing parentheses.
This is the correct version:
CREATE TABLE Customers (
CustomerID INT NOT NULL PRIMARY KEY IDENTITY,
ContactName VarChar(50) NULL,
Company VarChar(45) NULL,
Phone VarChar(12) NULL -- SURPLUS COMMA REMOVED
)
CREATE TABLE Shippers (
ShipperID INT NOT NULL PRIMARY KEY IDENTITY,
Company VarChar(45) NULL,
Phone VarChar(12) NULL -- SURPLUS COMMA REMOVED
)
CREATE TABLE Orders (
OrderID INT NOT NULL PRIMARY KEY IDENTITY,
OrderDate DATETIME NULL,
ShippedDate DATETIME NULL,
ShipperID INT NULL,
Freight DECIMAL NULL,
CustomerID INT NULL,
CONSTRAINT fk_Orders_Shippers
FOREIGN KEY (ShipperID) -- PARENTHESES ADDED
REFERENCES Shippers(ShipperID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
, -- COMMA ADDED
CONSTRAINT fk_Orders_Customers
FOREIGN KEY (CustomerID) -- PARENTHESES ADDED
REFERENCES Customers(CustomerID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)

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

T-SQL: CHECK constraint not working

I have the following T-SQL schema. The problem I am having is that the check constraint on the Download table is not working. I am still able to insert records into that table that contain NULL values for both ProductId and CategoryId. Why is this so?
I want both the ProductId and CategoryId columns to allow NULL values, but for any given record only one of these are allowed to be set to NULL, the other needs to be a corresponding Id of the Category or Product table.
CREATE TABLE Category (
Id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
Description nvarchar(100) NULL,
ParentCategoryId int NULL
CONSTRAINT fk_CategoryId_CategoryId FOREIGN KEY (Id) REFERENCES Category(Id)
)
GO
CREATE TABLE Product (
Id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
Title nvarchar(100) NOT NULL,
TagLine nvarchar(MAX) NOT NULL,
Description nvarchar(MAX)NULL,
CategoryId int NOT NULL,
ImageUrl nvarchar(255) NULL,
Keywords nvarchar(200) NOT NULL
CONSTRAINT fk_ProductCategoryId_CategoryId FOREIGN KEY (CategoryId) REFERENCES Category(Id)
)
GO
CREATE TABLE Download (
Id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
Title nvarchar(100) NOT NULL,
Description nvarchar(MAX) NULL,
CategoryId int NULL,
ProductId int NULL,
DownloadUrl nvarchar(255) NOT NULL,
CONSTRAINT fk_DownloadCategoryId FOREIGN KEY (CategoryId) REFERENCES Category(Id),
CONSTRAINT fk_DownloadProductId FOREIGN KEY (ProductId) REFERENCES Product(Id),
CONSTRAINT chk_ReferencesCategoryOrProduct CHECK (ProductID != NULL AND CategoryId != NULL)
)
GO
Use:
CONSTRAINT chk_ReferencesCategoryOrProduct CHECK (ProductID IS NOT NULL
OR CategoryId IS NOT NULL)
NULL isn't a value -- it's a placeholder for the lack of a value. Which is why you need to use specific syntax to check for it.