Creation of tables
1. Create column (field): Column name + type
2. Set the primary key column: the ability to uniquely represent a single piece of data
3. Set Unique key: Design--index/Key--add--Unique key (select column)--OK
The contents of a unique key cannot be duplicated
4. Foreign key Relationship: a table (from a table) where one of the columns refers to the primary key table in another table (primary table)
The purpose of foreign key relationships is to make the database more complete and more secure.
FK foreign key PK primary key
Because there are various relationships, SQL Server databases are generally referred to as relational databases
Design Table:
The three main paradigms of the database:
1. First paradigm: (Atomicity of each column)
Each column is the smallest atom in a program that does not open the split
2, the second paradigm: (each column must be related to the primary key)
Federated primary Key
3, the third paradigm: (each column must have a self-built relationship with the primary key)
T-SQL statement:
Creating database: Create Database name
Working with databases (switching databases): Use database name
To create a table:
CREATE TABLE Table name
(
code int,
Name varchar (50)
)
Use MyTest
Go
CREATE TABLE Minzu
(
Code varchar (primary key),
Name varchar (50)
)
Go
CREATE TABLE Renyuan
(
Code varchar (primary key),
Name varchar (unique),
Sex bit,
Nation varchar () NOT NULL references Minzu (code),
Birthday datetime
)
CREATE TABLE Friends
(
IDS int identity primary Key,
Mecode varchar (50),
FCode varchar (50)
)
Self-growing column
Key words
If more than one statement is to be executed together, the GO keyword is required after each statement
To create a foreign key relationship table, create a primary table, and then create a table
Primary key primary Key can only occur once
Unique single key can appear multiple times
Not NULL non-null
References foreign key relationship (reference)
Identity self-growth
SQL CREATE TABLE