How can I convert data contained if yes do somethings or no don't do anything to table in SQL Server - sql

I have data I want to looking to design table as in the picture

Create a table with the following design
CREATE TABLE dbo.MyData
(
SeqNo INT IDENTITY(1,1) CONSTRAINT Pk_MyDate_SeqNo PRIMARY KEY,
Diseases VARCHAR(255),
YesNo BIT CONSTRAINT Df_MyData_YesNo DEFAULT(0),
Duration INT,
Treatement VARCHAR(255)
)
Insert the value 1 To the Column YesNo for value Yes for No insert Value 0

Related

SQL auto increment pgadmin 4

I am trying to make a simple database with an number generator but why do I get the error below?
ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 2: IDNumber int NOT NULL AUTO_INCREMENT,
Code:
CREATE TABLE Finance
(
IDNumber int NOT NULL AUTO_INCREMENT,
FinName varchar(50) NOT NULL,
PRIMARY KEY(IDNumber)
);
The subject of the question mentions pgAdmin 4, so here's how to do it there.
First, add a column to your table, then click the little edit icon:
Then go to Constraints and select the Identity type:
This generates SQL similar to this:
CREATE TABLE public.my_table_name
(
id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
PRIMARY KEY (id)
);
For Postgres you have to use SERIAL
CREATE TABLE Finance
(
IDNumber SERIAL PRIMARY KEY,
FinName varchar(50) NOT NULL
);
IN pgadmin-2.
step 01:
create seq:
and set info:
step 02: go to ID in table and set constraints
if it is sql the syntax is the following
CREATE TABLE Persons (
ID int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
You are using a MySQL syntax which won't work in SQL Server.
CREATE TABLE Finance
(
IDNumber int NOT NULL IDENTITY(1,1) PRIMARY KEY,
FinName varchar(50) NOT NULL
);
The following code should work for SQL Server.
IDENTITY(1,1) is SQL Server's way of saying "auto increment".
The starting value for IDENTITY is 1, and it will increment by 1 for each new record.
So : IDENTITY(startvalue, incrementvalue)
there are two options; one is to use the "datatype" serial or create a sequence and use this sequence as a default value for your integer as follows:
CREATE SEQUENCE your_seq;
CREATE TABLE foo(
id int default nextval('your_seq'::regclass),
other_column TEXT);
INSERT INTO foo(other_column) VALUES ('bar') RETURNING *;
It is important to note that specifying a table as follows:
CREATE TABLE tablename (
colname SERIAL);
is equivalent to:
CREATE SEQUENCE tablename_colname_seq AS integer;
CREATE TABLE tablename (
colname integer NOT NULL DEFAULT nextval('tablename_colname_seq'));
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;
More information about serial can be found here.

Create table with UNIQUEIDENTIFIER as PK

I have been trying (unsuccessfully) to create a table with a UNIQUEIDENTIFIER as the primary key. The script below will create the table just fine, however there is no PK specified. This is my first time using a UNIQUEIDENTIFIER, however. Any ideas?
T-SQL
CREATE TABLE [dbo].[AgentRelationshipCodes]
(
Id UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID(),
RelationshipId char(3) NULL,
EffectiveDate datetime NULL,
LastChangeDate datetime NOT NULL,
LastChangeId char(6) NOT NULL,
AgtTableId int FOREIGN KEY REFERENCES AgentTransmission(ID)
);
Management Studio
create table [dbo].[AgentRelationshipCodes]
(
Id UNIQUEIDENTIFIER NOT NULL PRIMARY KEY DEFAULT NEWID(),
.
.
.
)
Repeat for the other tables.
Answered here by Christian Hayter

How i can change and run sql file into mssql

I have my database in sqlite .I exported sql file from sqlite manager. Now on executing that file into Microsoft SQL Server 2012 it is giving error on following lines.
DROP TABLE
IF EXISTS "Admin";
CREATE TABLE "AdvancePayments" (
"Id" INT PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
"date" VARCHAR,
"Name" VARCHAR,
"Amount" INT,
"Remarks" VARCHAR);
INSERT INTO "AdvancePayments"
VALUES (
1,
'11/22/2013',
'wqqw',
21,
'fgdsr');
CREATE TABLE "DVouchers" (
"DVid" INT PRIMARY KEY NOT NULL,
"vdate" VARCHAR DEFAULT(NULL),
"Vehiclenum" VARCHAR,
"Coupen" BOOL,
"Saleperson" VARCHAR,
"DVnumber" INT,
"Ares" VARCHAR,
"Ctitle" VARCHAR,
"remarks" VARCHAR,
"mEntry" BOOL,
"Cust_Id" INT,
FOREIGN KEY (Cust_Id) REFERENCES Customer_New(Cust_Id));
What is correct syntax for mssql?
You should write it as:
--DROP TABLE IF EXISTS Admin;
--query to drop a table
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TableName]') AND type in (N'U'))
DROP TABLE [dbo].[TableName]
GO
CREATE TABLE [AdvancePayments]
(
[Id] INT IDENTITY(1,1) PRIMARY KEY NOT NULL ,--The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature
[date] VARCHAR(10),-- best practice is to use datetime datatype to store date feilds.
[Name] VARCHAR(100),
[Amount] INT,
[Remarks] VARCHAR(1000)
);
INSERT INTO AdvancePayments
VALUES (
--1,-- you cannot give an explicit value to identity column untill IDENTITY_INSERT is ON. So comment this.
'11/22/2013',
'wqqw',
21,
'fgdsr');
CREATE TABLE [DVouchers] (
[DVid] INT PRIMARY KEY NOT NULL,
[vdate] VARCHAR(20) DEFAULT(NULL),
[Vehiclenum] VARCHAR(100),
[Coupen] BIT,-- Bit is the datatype used for Boolean values.
[Saleperson] VARCHAR(100),
[DVnumber] INT,
[Ares] VARCHAR(100),
[Ctitle] VARCHAR(100),
[remarks] VARCHAR(100),
[mEntry] BIT,
[Cust_Id] INT,
FOREIGN KEY (Cust_Id) REFERENCES Customer_New(Cust_Id)
);
Note: In MS SQL default length of varchar datatype is 1. so you will have to specify length to each
varchar variable.
TSQL:
IF NOT OBJECT_ID('dbo.TableToDrop') IS NULL
BEGIN
DROP TABLE dbo.TableToDrop
END;
// change dbo. to your schema if necessary

When I create a table in SQL, how do I set an attribute not null?

I wanna create a table in SQL, and if the I wanna set the attributes to NULL, how do I do it?
For example, I wanna create a table named Courses and its attributes "CourseNo" and "Title" must not be null. I created one below:
CREATE TABLE Courses(
CourseNo INTEGER CHECK(100<=CourseNo<=999) PRIMARY KEY,
Title VARCHAR(100),
)
ALTER TABLE Courses
ALTER COLUMN CourseNo INTEGER NOT NULL
ALTER COLUMN Title VARCHAR(100) NOT NULL
Is this correct?
whatever you wrote that one correct but good practice is to write not null at a time of defining table. do like this:
CREATE TABLE Courses(
CourseNo INTEGER CHECK(100<=CourseNo<=999) PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
)
primary key is by default NOT NULL always, hence you no need to declare it as NOT NULL
NOT NULL in alter is used when u want to make change in column definations of table later
Change that Title VARCHAR(100) to Title VARCHAR(100) NOT NULL.

SQL Server : create error how to write database name with the table name

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.