peri hokiperihokiclarustologisticsrimashaop.compola taktis menang gates of olympus x1000 perihokipemain perihoki bongkar trik jepep starlight princess x1000perihoki beri panduan lengkap bermain mahjong ways 2 pgsofttips cara penghasil uang pgsoft mahjong ways 2 perihokimahjong wins 3 perihoki memberikan penggali terbesarfenomena gates of olympus buka peluang besar bagi duta76cara dapat pola emas rahasia di duta7 wild west goldtrik menang pgsoft mahjong ways 2 modal receh maxwin duta76duta76 main pgsoft mahjong ways 2 di sela istirahatscatter hitam mahjong wins 3 spin pasti jepe duta76perihoki spin mahjong ways 2 pgsoft scatter hitam maxwinsimbol mahjong wins 3 yang sering muncul di awal perihokitrik gampang menang mahjong winscuan mahjong wins 3 tanpa hentipola spin taruhan kecil mahjong winsstrategi kunci scatter hitam beruntungacor total mahjong wayskemenangan maksimal mahjong wayskemenangan pria pekanbaru mahjong waysmahjong wins3 fenomena baru sumatera baratmenangkan mahjong dengan lincahmenembus batas keberuntungan mahjong waysmenjajal keberuntungan di mahjong wayspola gratis mahjong ways1scatter hitam senjata rahasia mahjong winsteknik spinning menguntungkan untuk scatter hitamanalisis rtp tertinggi mahjong winsfenomena scatter hitam mahjong winsibu rumah tangga dan keberuntungan mahjongkeseruan main mahjong wins3menang mahjong pengguna baru di sosial mediamenghargai setiap momen di mahjong waysserunya main mahjong di warung internetspin slow motion mancing scatterstrategi jitu maxwin mahjong waysstrategi jitu menang mahjong modal kecil jackpot besaranak kampung papua menggemparkan mahjongkeajaiban scatter hitam mahjong wayskeberhasilan joni mahjong winskejaiban mahjong ways di ujung jalanketua mahjong wins3 kehadiran kemenangan dimulai dari balimahjong kehadiran yang menguntungkanmahjong menggila adat88panduan kilat raih 108 jutatips memenangkan mahjongbarista kopi menang 110 juta spin mahjongdakota76 scatter hitam mahjong wins 3gagal cpns menang mahjong wins 3 rp120 jutakisah karyawan minimarket dapat thr lebih awal mahjong winskisah pelajar sma kuasai mahjong wins 3 inspirasi sekolahkisah penjaga parkir tanah abang motor baru mahjong winskisah sukses ibu rumah tangga bandung mahjong winsmahasiswa kkn beli iphone15 mahjong wins3 dakotamantan anak band influencer mahjong scatter hitampola rtp senin gates of olympus guru matematikaidola scatter hitam gampang meledakidola sensasi jackpotidola pola scatter hitamidola hasilkan jackpot dalam waktu singkatidola mahjong wins 3 bocortukang cuci motor menemukan keberuntungan di mahjong

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Khóa chính Primary Key trong SQL Server - Freetuts
GETTING STARTED
DATA DEFINITION
DATA QUERIES
CONDITION & OPERATORS
EXPLORED
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Khóa chính Primary Key trong SQL Server

Trong bài này chúng ta sẽ tìm hiểu đên khóa chính primary key trong SQL Server và cách tạo khóa chính cho một table. Khóa chính rất quan trọng vì nó dùng để phân biệt các dòng dữ liệu trong bảng.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

1. Primary key là gì?

Khóa chính là tập hợp một hoặc nhiều column giúp phân biệt các record trong một table, đây là thông tin rất quan trọng bởi nếu thiếu nó thì lược đồ CSDL của bạn chỉ là đồ bỏ đi.

Ví dụ bảng SINHVIEN thì thường ta sẽ có column MASINHVIEN dùng để nhận diện các sinh viên, tên sinh viên có thể bị trùng nhưng mã sinh viên thì không thể trùng.

Đặc điểm của khóa chính như sau:

Bài viết này được đăng tại [free tuts .net]

  • Có thể thiết lập khóa chính bằng một hoặc nhiều column, trong thực tế thì nên 1 column
  • Khóa chính không được NULL, và là duy nhất (unique)
  • Khóa chính nếu là kiểu số nguyên thì nên thiết lập tăng tự động sẽ giúp tối ưu database

2. Cách tạo primary key

Chúng ta có hai cách tạo primary key, thứ nhất là tạo trực tiếp ở lệnh create table và thứ hai là sử dụng lệnh alter table.

Tạo trực tiếp ở lệnh create table

Cách này có hai cú pháp như sau:

Cú pháp 1
CREATE TABLE table_name (
    pk_column data_type PRIMARY KEY,
    ...
);

Cú pháp 2
CREATE TABLE table_name (
    pk_column_1 data_type,
    pk_column_2 data type,
    ...
    PRIMARY KEY (pk_column_1, pk_column_2)
);

Với cú pháp 1 thì chỉ dùng được ở trường hợp khóa chính chỉ có một column, còn cú pháp 2 thì dùng cho trường hợp nhiều column.

Ví dụ 1: Tạo bảng activities thuộc schema sales, vì khóa chính chỉ có 1 column nên dùng cú pháp 1.

CREATE TABLE sales.activities (
    activity_id INT PRIMARY KEY IDENTITY,
    activity_name VARCHAR (255) NOT NULL,
    activity_date DATE NOT NULL
);

Ví dụ 2: Tạo bảng participants thuộc schema sales, vì khóa chính có 2 column nên dùng cú pháp 2.

CREATE TABLE sales.participants(
    activity_id int,
    customer_id int,
    PRIMARY KEY(activity_id, customer_id)
);

Tạo bằng lệnh Alter Table

Lệnh ALTER TABLE có thể dùng để thay đổi cấu trúc của table nên ta có thể dùng nó để xác định khóa chinh sau khi tạo bảng.

Ví dụ: Tạo bảng events thuộc schema sales. Chúng ta sẽ có hai bước như sau.

Bước 1: Tạo bảng

CREATE TABLE sales.events(
    event_id INT,
    event_name VARCHAR(255),
    start_date DATE NOT NULL,
    duration DEC(5,2)
);

Bước 2: Sử dụng lệnh alter table để tạo khóa chính

ALTER TABLE sales.events 
ADD PRIMARY KEY(event_id);

Trên là hai cách tạo khóa chính (primary key) trong SQL Server. Bài này mình dừng ở đây, bài tiếp theo mình sẽ nói đến cách tạo khóa ngoại cho table.

Cùng chuyên mục:

Hàm CHAR trong SQL Server

Hàm CHAR trong SQL Server

Cách sử dụng hàm CHAR trong SQL Server

Hàm CHARINDEX trong SQL Server

Hàm CHARINDEX trong SQL Server

Cách sử dụng hàm CHARINDEX trong SQL Server

Hàm CONCAT trong SQL Server

Hàm CONCAT trong SQL Server

Cách sử dụng hàm CONCAT trong SQL Server

Hàm + trong SQL Server

Hàm + trong SQL Server

Cách sử dụng hàm + trong SQL Server

Hàm DATALENGTH trong SQL Server

Hàm DATALENGTH trong SQL Server

Cách sử dụng hàm DATALENGTH trong SQL Server

Hàm LEFT trong SQL Server

Hàm LEFT trong SQL Server

Cách sử dụng hàm LEFT trong SQL Server

Hàm LEN trong SQL Server

Hàm LEN trong SQL Server

Cách sử dụng hàm LEN trong SQL Server

Hàm LOWER trong SQL Server

Hàm LOWER trong SQL Server

Cách sử dụng hàm LOWER trong SQL Server

Hàm LTRIM trong SQL Server

Hàm LTRIM trong SQL Server

Cách sử dụng hàm LTRIM trong SQL Server

Hàm NCHAR trong SQL Server

Hàm NCHAR trong SQL Server

Cách sử dụng hàm NCHAR trong SQL Server

Hàm PATINDEX trong SQL Server

Hàm PATINDEX trong SQL Server

Cách sử dụng hàm PATINDEX trong SQL Server

Hàm REPLACE trong SQL Server

Hàm REPLACE trong SQL Server

Cách sử dụng hàm REPLACE trong SQL Server

Hàm RIGHT trong SQL Server

Hàm RIGHT trong SQL Server

Cách sử dụng hàm RIGHT trong SQL Server

Hàm RTRIM trong SQL Server

Hàm RTRIM trong SQL Server

Cách sử dụng hàm RTRIM trong SQL Server

Hàm SPACE trong SQL Server

Hàm SPACE trong SQL Server

Cách sử dụng hàm SPACE trong SQL Server

Hàm STR trong SQL Server

Hàm STR trong SQL Server

Cách sử dụng hàm STR trong SQL Server

Hàm UPPER trong SQL Server

Hàm UPPER trong SQL Server

Cách sử dụng hàm UPPER trong SQL Server

Hàm SUBSTRING trong SQL Server

Hàm SUBSTRING trong SQL Server

Cách sử dụng hàm SUBSTRING trong SQL Server

Hàm STUFF trong SQL Server

Hàm STUFF trong SQL Server

Cách sử dụng hàm STUFF trong SQL Server

Hàm ABS trong SQL Server

Hàm ABS trong SQL Server

Cách sử dụng hàm ABS trong SQL Server

Top