Monday, October 1, 2018

.::: Create Database, Table and Entry Data, Check Database Size on Microsoft SQL Server 2012 Using TSQL :::.

When working with a command or query window, the language used by SQL Server is TSQL.

Step 1 -Create Database

Create a new database called teguhtrihartodb by entering the following TSQL then pressing the “Execute” button on the top menu.

CREATE DATABASE teguhtrihartodb;
GO
The Messages window on the bottom of the screen should say, ”Command(s) completed successfully.”

Step 2 -Use new Database

USE teguhtrihartodb;
GO

Delete that line of TSQL and enter: USE teguhtrihartodb; . Again press execute. This tells the Query window to run any future commands against they newly created teguhtrihartodb database rather than against master.

Step 3 -Create new Table

Next, we’ll create an Karyawan table to hold data. Delete any TSQL in the Query window and execute the following:

CREATE TABLE Karyawan
(
 EmpName varchar(50),
 Wage money
);

Step 4 -Enter Data

Execute the following statement to enter data into the newly created table.

INSERT INTO Karyawan
 (EmpName, Wage)
VALUES
 ('Kitagawa', 10.5);

INSERT INTO Karyawan
 (EmpName, Wage)
VALUES
 ('Keiko', 11);

INSERT INTO Karyawan
 (EmpName, Wage)
VALUES
 ('Sailormars', 50);

INSERT INTO Karyawan
 (EmpName, Wage)
VALUES
 ('Teguh Triharto', 22);

Step 5 – View Data

The final step is to view the data just entered. Remove the previos INSERT statements and exeucte the following TSQL.

select * from Karyawan;

Step 6 - Check Database Size

    SELECT      sys.databases.name, 
                CONVERT(VARCHAR,SUM(size)*8/1024)+' MB' AS [Total disk space] 
    FROM        sys.databases  
    JOIN        sys.master_files 
    ON          sys.databases.database_id=sys.master_files.database_id 
    GROUP BY    sys.databases.name 
    ORDER BY    sys.databases.name


Step 6 – Resume all command

CREATE DATABASE teguhtrihartodb;
GO

USE teguhtrihartodb;
GO
CREATE TABLE Karyawan
(
 EmpName varchar(50),
 Wage money
);

INSERT INTO Karyawan
 (EmpName, Wage)
VALUES
 ('Kitagawa', 10.5);

INSERT INTO Karyawan
 (EmpName, Wage)
VALUES
 ('Keiko', 11);

INSERT INTO Karyawan
 (EmpName, Wage)
VALUES
 ('Sailormars', 50);

INSERT INTO Karyawan
 (EmpName, Wage)
VALUES
 ('Teguh Triharto', 22);

select * from Karyawan;

No comments:

Post a Comment

Popular Posts