Yes/No column in SQL 2018 studio - sql

Can anyone explain how to create a yes/no column in SQL Studio 2018? BOOL or bit(1) are not working.
Create Table Diseases
(
P_ID int not null,
NameofDis varchar(100),
DateofDis date,
Condition bit(1),
);

You can use simple BIT datatype or you can use BOOLEAN data type like this
Create Table Diseases(
P_ID int not null,
NameofDis varchar(100),
DateofDis date,
Condition BIT,
);
or
Create Table Diseases(
P_ID int not null,
NameofDis varchar(100),
DateofDis date,
Condition BOOLEAN,
);

Try using BIT instead of BIT(1). This will create the column as a yes/no field aka true/false field where 1 is yes/true and 0 is no/false.
Your code:
Create Table Diseases(
P_ID int not null,
NameofDis varchar(100),
DateofDis date,
Condition bit
);

Related

sql creating table issue

unexpected beginning of statement
the coding
If you are using SQL then the issue is with create query where you passes int(10)
you can not pass any size to int type of fields
create table acc
(acc_id int not null primary key,
username_acc varchar(30) not null,
....)
Try with above
Thanks

SQL Server 2014 : help creating tables

I am new to MSSQL 2014 Server, my professor listed these steps to make a table, I don't know the proper steps to create tables in the pictures listed below, please help.
Create and populate (insert values) the following tables per table description and data values provided
DEPARTMENT
EMPLOYEE
PROJECT
ASSIGNMENT
Add a SQL Comment to include /* * Your First Name_Your Last Name* */ when inserting corresponding values for each table.
What I tried so far:
CREATE TABLE DEPARTMENT(
DepartmentName Text(35) PRIMARY KEY,
BudgetCode Text(30) NOT NULL,
OfficeNumber Text(15) NOT NULL,
Phone Text(12) NOT NULL, );
I have put this to my query and the error is
Msg 2716, Level 16, State 1, Line 1 Column, parameter, or variable #1: Cannot specify a column width on data type text.
Try this(I assume that your table exists in dbo schema):
IF OBJECT_ID(N'dbo.DEPARTMENT', N'U') IS NOT NULL
BEGIN
DROP TABLE DEPARTMENT
END
GO
CREATE TABLE DEPARTMENT(
DepartmentName varchar(35) PRIMARY KEY,
BudgetCode varchar(30) NOT NULL,
OfficeNumber varchar(15) NOT NULL,
Phone varchar(12) NOT NULL
);
You can not define width for Text data type. In case which you need to define width you can use char or varchar data types. Also keep in mind that if you need to work with Unicode characters then you will need to use nchar or nvarchar instead.

SQL: Creating table with Enum-like attribute that is one of only several types

Does SQL have an OR or | equivalent for attribute types that are only one of a few possibliities (ie Enum types)?
Example using my best guess for the status attribute in the following table:
CREATE TABLE Rental (
status ("open" | "closed"),
date datetime,
id int PRIMARY KEY
)
I want status to be either "open" or "closed", nothing else. Is there syntax for this, or should I use CHAR(6) or should I use a constraint instead?
You may use check constraints when defining you table to narrow the domain of the attribute.
CREATE TABLE Rental (
status char(6),
date datetime,
id int PRIMARY KEY
);
alter table Rental
add constraint status_valid_value
check (status is null or (status in ('open','closed')));
CREATE TABLE Rental (
status char(6),
date datetime,
id int PRIMARY KEY
Check (status='open' OR status='closed')
)
It's also better to store status like int (1- open, 0 - closed, an so on)

SQL Create table in database ssms

I would like to create a database with a couple tables in it. I am using SSMS and it is easy enough to accomplish said task by right-click creating, but I would like to use query/command line (not sure how to phrase that).
Create a database and table
CREATE DATABASE test_employees;
CREATE TABLE dbo.EmployeesBasicInfo
(
MyKeyField VarChar(8) PRIMARY KEY,
FirstName VarChar(30) NOT NULL,
LastName VarChar(50) NOT NULL,
DateStarted DateTime NOT NULL,
Age Int NOT NULL,
ModifiedDate DateTime NULL
);
But I have no idea where the table goes or how to move/link it to database test_employees.
Also, if you feel ambition in answering my question then the next step is to auto-generate data for all fields. Any links you could provide would be helpful. And, if anything I'm doing is not best-practice please let me know - I'm just getting into SQL.
After you've created the database you need to
Use test_employees
Go
This sets your current working database for subsequent statements.
Alternatively you can qualify your create table with the database name
Create Table test_employees.dbo.EmployeesBasicInfo (
MyKeyField varchar(8) primary key,
FirstName varchar(30) not null,
LastName varchar(50) not null,
DateStarted DateTime not null,
Age int not null,
ModifiedDate datetime null
);
You've probably created this table in your default database, which may well be master.
Also for any recent version of SQL Server, consider using datetime2 instead of datetime (or date if you don't need the time component). It's better in all respects.
Here is some code to create a table 1st and then you can add some test data into it to practice with .......
TO CREATE A TABLE :
Create Table dbo.BasicInfo (
MyKeyField INT primary key IDENTITY(1,1),
FirstName varchar(30) not null,
LastName varchar(50) not null,
DateStarted DateTime not null,
Age int not null,
ModifiedDate datetime null
)
GO
TO ADD PRACTICE DATA:
DECLARE #Value INT = 1
WHILE #Value <= 100
BEGIN
INSERT INTO dbo.BasicInfo (FirstName, LastName, DateStarted, Age, ModifiedDate)
VALUES ('First_Name_' + CAST(#Value AS VARCHAR), 'Last_Name_'+ CAST(#Value AS VARCHAR), DATEADD(DAY, -CONVERT(INT, (5000+1)*RAND()), GETDATE()),
18 + CONVERT(INT, (30-10+1)*RAND()), DATEADD(DAY, 10 + (30-10)*RAND() ,DATEADD(DAY, -CONVERT(INT, (5000+1)*RAND()), GETDATE())))
SET #Value = #Value + 1
END
if you want to add more then 100 rows in your table just replace the number or rows you wish to add to your table with 100 in this code

how to function parameter take value automatically in sql server

i have create a table
infobool (
book_id int identity(1,1) primary key,
bid varchar(20),bname varchar(50)
)
now i want use a function
dbo.book_id(#a int)
returns varchar(20)
as
begain
return 'bkid00'+convert(varchar(10),#a)
end
now how this function take automatically value from book_id column
It looks like you are just trying to prepend a value to your book_id column?
You can achieve that without a function, try something like this:
SELECT 'bkid00' + CONVERT(VARCHAR(10, book_id), bid
FROM infobool