-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-Check_constraint.sql
More file actions
91 lines (71 loc) · 2.71 KB
/
07-Check_constraint.sql
File metadata and controls
91 lines (71 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
check constraint
If a column is marked as an identity column, then the values for this column are
automatically generated, when you insert a new raw into the table
Create a table tblPerson
{
PersonId int Identity(1,1) Primary key,
Name nvarchar(50) Null
}
Note Seed and increment values are optional, if you don't specify the identity and seed they both default to 1
To Explicitly supply a value for identity column
1. First turn on Identity insert-SET Identity_insert tblPerson ON
2. In the insert query specify the column list
isert into tblPerson(PersonID, Name) Values(2,'Jhon')
If you have deleted all the rows in a table and you want to reset the identity column
values use DBCC CHECKDENT command
DBCC CHECKIDENT('tblPerson', RESEED, 0)
*/
use new_database
GO
select * from tblPerson
alter table tblPerson
Drop column Age
alter table tblPerson
drop constraint CK_tblPerson_age
insert into tblPerson(ID,Name, email) values(11, 'Tokir','Tokir@gmail.com')
Create table Web
{
ID int NOT NULL Identity[(1,1)] Primary key,
Name nvarchar[(50)] NOT NULL;
Mail nvarchar[(50)] NOT NULL;
Phone nvarchar[(50)] NULL;
Address nvarchar[(50)] NULL;
GenderID int null
}
--new table into My_new_database
use My_new_database
GO
Create table gender
(
ID int not NULL PRIMARY KEY,
Gender nvarchar(20) NOT NULL
)
Select * from gender
Insert into gender(ID,Gender) values(1,'Male');
Insert into gender(ID,Gender) Values(2,'Female');
Insert into gender(ID,Gender) Values(3,'Others');
use My_new_database
GO
--Creating tblPerson table
Create table tblPerson
(
ID int NOT NULL IDENTITY(1,1) PRIMARY KEY,
NAME nvarchar(50) NOT NULL,
Mail nvarchar(50) NOT NULL,
Phone nvarchar(50) NOT NULL,
age int NOT NULL,
[Address] nvarchar(100) NOT NULL,
--Foreign key constaint at Declaration
GenderID int NULL FOREIGN KEY REFERENCES Gender(ID),
--Check constraint for age
CONSTRAINT CHK_Person CHECK (age > 0 AND age < 150),
)
--Printing data into screen
Select *from tblPerson
--Inserting data into tblPerson table
Insert into tblPerson(name, Mail, Phone, age, address, GenderID) values('Firoz', 'Firoz@gmail.com', '012345678', 23,'Dhaka', 1);
Insert into tblPerson(name, Mail, Phone, age, address, GenderID) values('Maliha', 'Maliha@gmail.com', '012345678', 23,'Dhaka', 2);
Insert into tblPerson(name, Mail, Phone, age, address, GenderID) values('mollika', 'Mollika@gmail.com', '012345678', 23,'Dhaka', 2);
Insert into tblPerson(name, Mail, Phone, age, address, GenderID) values('Nahid', 'Nahid@gmail.com', '012345678', 23,'Dhaka', 1);
Insert into tblPerson(name, Mail, Phone, age, address, GenderID) values('Asabuddin', 'Asabuddin@gmail.com', '012345678', 23,'Dhaka', 1);