• Home
  • About
    • Hanna's Blog photo

      Hanna's Blog

      I wanna be a global developer.

    • Learn More
    • Email
    • LinkedIn
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

[MSSQL] GameDB

08 Feb 2021

Reading time ~2 minutes

Reference by [C#과 유니티로 만드는 MMORPG 게임 개발 시리즈]Part5: 데이터베이스

CREATE DATABASE

  • CREATE DATABASE
    • creates a new database
CREATE DATABASE GameDB;
MSSQL GameDB
  • CREATE TABLE
    • creates a new table
    • defines column and constraints
CREATE TABLE accounts(
	accountId INTEGER NOT NULL,
	accountName VARCHAR(10) NOT NULL,
	coins INTEGER DEFAULT 0,
	createdTime DATETIME
);
MSSQL GameDB
  • DROP TABLE
    • removes the table
DROP TABLE accounts;
  • ALTER TABLE
    • changes the table
    • ADD: add new column
ALTER TABLE accounts
ADD lastEnterTime DATETIME;
MSSQL GameDB
  • DROP COLUMN
    • removes the column
ALTER TABLE accounts
DROP COLUMN lastEnterTime;
MSSQL GameDB
  • ALTER COLUMN
    • changes the column
ALTER TABLE accounts
ALTER COLUMN accountName VARCHAR(20) NOT NULL;
MSSQL GameDB
  • PRIMARY KEY
    • assigns the column to primary key
ALTER TABLE accounts
ADD PRIMARY KEY (accountId);
MSSQL GameDB
  • You can assign primary key in GUI
MSSQL GameDB
  • PRIMARY KEY improves the performance of database
MSSQL GameDB
  • CONSTRAINT
    • defines the name of constraint
    • it makes easier to remove the contraint
ALTER TABLE accounts
ADD CONSTRAINT PK_Account PRIMARY KEY(accountId);
ALTER TABLE accounts
DROP CONSTRAINT PK_Account;

INDEX

  • for regulation, you can use INDEX to index data

  • Clustered
    • data is saved by order(ex. alpabet, number, etc.)
    • normally, it is primary key
  • Non-Clustered
    • data is saved by index
  • CREATE INDEX
    • makes new non-clustered index
CREATE INDEX i1 ON accounts(accountName);
MSSQL GameDB
  • You can make index by several values
CREATE UNIQUE INDEX i3 ON accounts(accountName, coins);
MSSQL GameDB
  • UNIQUE
    • defines the column has dintinct values
CREATE UNIQUE INDEX i2 ON accounts(accountName);
MSSQL GameDB
  • DROP
    • removes the index
DROP INDEX accounts.i1;
MSSQL GameDB
  • CLUSTERED
    • makes clustered index when there is not primary key in table
CREATE CLUSTERED INDEX i4 ON accounts(accountName);
MSSQL GameDB

JOIN

  • JOIN
    • join all elements of each table
SELECT *
FROM testA
	CROSS JOIN testB;
MSSQL GameDB

TRANSACTION

  • TRANSACTION
    • groups process into units
    • You should write ROLLBACK or COMMIT at the end
BEGIN TRAN;
	INSERT INTO accounts VALUES(2, 'Hanna2', 100, GETUTCDATE());
MSSQL GameDB
  • ROLLBACK
    • returns the process to before status
ROLLBACK;
MSSQL GameDB
  • COMMIT
    • comfirm the process
BEGIN TRAN;
	INSERT INTO accounts VALUES(2, 'Hanna2', 100, GETUTCDATE());
COMMIT;
MSSQL GameDB
  • LOCK
    • there is not COMMIT or ROLLBACK in TRAN, the process is in lock

TRY - CATCH

  • TRY - CATCH
    • when the process has error or exception, tries CATCH
    • if there is not error or exceiption in TRY, then process the process in TRY
  • @TRANCOUNT
    • number of transaction
BEGIN TRY
	BEGIN TRAN;
		INSERT INTO accounts VALUES (1, 'Hanna', 100, GETUTCDATE());;
		INSERT INTO accounts VALUES (2, 'Hanna2', 100, GETUTCDATE());;
	COMMIT;
END TRY
BEGIN CATCH
	IF @@TRANCOUNT>0
		ROLLBACK
	PRINT('ROLLBACK')
END CATCH
MSSQL GameDB
BEGIN TRY
	BEGIN TRAN;
		INSERT INTO accounts VALUES (1, 'Hanna', 100, GETUTCDATE());;
		INSERT INTO accounts VALUES (3, 'Hanna3', 100, GETUTCDATE());;
	COMMIT;
END TRY
BEGIN CATCH
	IF @@TRANCOUNT>0
		ROLLBACK
	PRINT('ROLLBACK')
END CATCH
MSSQL GameDB

Download



DatabaseMSSQLRDBMS Share Tweet +1