-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-Create_table.sql
More file actions
58 lines (48 loc) · 1.77 KB
/
03-Create_table.sql
File metadata and controls
58 lines (48 loc) · 1.77 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
/*
Create table
The aim of this session is to create tblperson and tblGender table and establish primary key and foring key constraints
In SQL server table can be create graphically usingSQL server Management studio(SSMS) or
using a query. Foreign key references can be added graphically using SSMS or using a query
Alter table ForeignKeyTable add constraint ForeignKeyTable_ForiegnKeyColumn_FK
FOREIGN KEY (FreignKeyColumn) reference PrimaryKeytable(PrimaryKeyColumn)
Foriign key are used to enforce database integrity. In layman's terms. A foreigm key in one table points to a primary key in another table
The foreign key constraint prevents invellid data from beign inserted into the foreign key column. the values that enter into the foreign
key column, has to be one of the values contained in the table it points to.
*/
/*
Create table
create table "Table name"
(
//table attribute
"column name" "data type" "NULL or NOT NULL" "KEY specification"
)
*/
use new_database
GO
Create table tblGender
(
ID int NOT NULL primary key,
Gender nvarchar(20) NOT NULL
)
use new_database
go
drop table tblGender
use new_database
go
Create table tblPerson
(
ID int NOT NULL primary key,
Name nvarchar(50) NOT NULL,
Email nvarchar(50) NOT NULL,
GenderID int,
)
/*
In SQL server table can be create graphically usingSQL server Management studio(SSMS) or
using a query. Foreign key references can be added graphically using SSMS or using a query
Alter table ForeignKeyTable add constraint ForeignKeyTable_ForiegnKeyColumn_FK
FOREIGN KEY (FreignKeyColumn) references PrimaryKeytable(PrimaryKeyColumn)
*/
use new_database
GO
alter table tblPerson add constraint tblPerson_foreignKey_FK
foreign key (GenderID) references tblGender (ID)