0% found this document useful (0 votes)
13 views

Mazhocdata - SQL Course - Lesson 9

Uploaded by

Thảo Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Mazhocdata - SQL Course - Lesson 9

Uploaded by

Thảo Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

07

Modifying & Defining


Database
Defining Database / Table

-- Create a new database: -- Create a new table:


CREATE DATABASE database_name CREATE TABLE table_name_1 (
-- Delete a database: column_1 datatype [not null]
DROP DATABASE database_name , column_2 datatype
, column_3 datatype ,
,…)
-- Delete a table:
DROP TABLE table_name

@Copyright 2022 Mazhocdata


Practice
Exercise 25: Tạo Database tên là napas_transaction
Tiếp theo tạo các bảng dữ liệu lưu trư thông tin sau:
1. Bảng transaction bao gồm các thông tin:
(trans_id, customer_id, trans_type, bank_id, amount, trans_status, trans_date)
2. Bảng bank_info gồm (bank_id, bank_name, bank_type)
3. Bảng status_info gồm (trans_status, error_group, message)
4. Bảng customer_profile (customer_id, verified_kyc, dob, name, id_number, id_address)

@Copyright 2022 Mazhocdata


Defining Primary key & Foreign key for a table

Way 1: Define keys when creating new table Way 2: define a key for an existing table
CREATE TABLE table_name_1 ( ALTER TABLE table_name
column_1 datatype [not null] PRIMARY KEY ADD PRIMARY KEY (column_1)
, column_2 datatype
, column_3 datatype , ALTER TABLE table_name
,…) ADD FOREIGN KEY (column_2) REFERENCES table_name_1 (column_1)

CREATE TABLE table_name_2 (


column_a datatype [not null] PRIMARY KEY
ALTER TABLE transaction
, column_b datatype
ADD PRIMARY KEY (trans_id)
, column_c datatype FOREIGN KEY REFERENCES
table_name_1 (column_1)
,…) ALTER TABLE transaction
ADD FOREIGN KEY (bank_id) REFERENCES bank_info (bank_id)

@Copyright 2022 Mazhocdata


Add or Delete a column

-- add a new column into a table


ALTER TABLE table_name
ADD phone_number INT

-- delete a new column into a table


ALTER TABLE table_name
DROP COLUMN phone_number INT

@Copyright 2022 Mazhocdata


Insert data

Way 1:
INSERT INTO table_name_1 ( column1, column 2)
VALUES (value1, value2)

Way 2:
INSERT INTO table_name_1
VALUES (value1, value2, value3)

Way 3:
INSERT INTO table_name
SELECT *
FROM data_source
WHERE …

@Copyright 2022 Mazhocdata


Delete & Update data

DELETE UPDATE
Way 1: Delete all rows in a table Way 1: Update manually
TRUNCATE TABLE table_name UPDATE table_name
SET column_1 = new_value
Way 2: Delete rows in a table with some conditions , column 2 = new_value
DELETE table_name ,…
WHERE column_1 < 1000 WHERE …

Way 2: Update by data from another table


UPDATE table_1
SET table_1.column_a = table_2.column_b
FROM table_2

@Copyright 2022 Mazhocdata


Import a CSV file from Azure Data Studio
Step 1: Download SQL Server Import

Install this extension

@Copyright 2022 Mazhocdata


Import a CSV file from Azure Data Studio
Step 2: Import Wizard

• Right click on your destination database


• Select “Import Wizard”

@Copyright 2022 Mazhocdata


Import a CSV file from Azure Data Studio
Step 3: Import your CSV file

à Server name

à Your destination database

à Your CSV file

à Rename table

@Copyright 2022 Mazhocdata


Import a CSV file from Azure Data Studio
Step 4: Modify information of table

@Copyright 2022 Mazhocdata


Import a CSV file from Azure Data Studio
Step 4: Modify information of table

@Copyright 2022 Mazhocdata


08
Share your queries
with VIEWS
Definition
A VIEW in SQL Server is like a virtual
table that contains data from one or
multiple tables. It does not hold any data
and does not exist physically in the
database. Similar to a SQL table, the
view name should be unique in a
database.

@Copyright 2022 Mazhocdata


How to CREATE a VIEW?

CREATE VIEW Name AS


Select column1, Column2...Column N
From tables
Where conditions;

ALTER VIEW Name AS


Select column1, Column2...Column N
From tables
Where conditions;

@Copyright 2022 Mazhocdata


Connect Excel to SQL Server database
Step 1: Open Excel à Data à Get Data à From SQL Server

@Copyright 2022 Mazhocdata


Connect Excel to SQL Server database
Step 2: Input your SQL server info

@Copyright 2022 Mazhocdata


Connect Excel to SQL Server database
Step 3: Choose a destination table

@Copyright 2022 Mazhocdata

You might also like