0
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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@AhmedKato By putting that line right after your create database statement. See the link I've added.
this is my code CREATE DATABASE USE agom COLLATE Arabic_CI_AS CREATE TABLE agom.dbo.Branches but it tell me database agom does not exist and error near USE
@AhmedKato Then also make the create statement a separate batch: CREATE DATABASE agom COLLATE Arabic_CI_AS; GO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.