By GokiSoft.com| 16:48 09/05/2023|
SQL Server/MySQL

[Source Code] Code bài học 1 - G2212I

Nội dung kiến thức:
- Giới thiệu về sql (sql server, mysql, oracle, ...)
	Ví dụ:
		Thông tin sinh viên(tên, tuôi, địa chỉ, email, sđt)
		1 trieu ban ghi
	Giai phap:
		Thông thường:
			File -> Tim kiem
			Excel -> Tim kiếm
		Liên kết vs ngôn ngữ lập trình (C, C++, C#, ...) -> kho dữ liêu
	Solution:
		SQL (SQL Server, MySQL, ...) -> Phần mềm -> Bộ lệnh (thêm dữ liệu, sửa, xóa, ...)
			Hiểu về tổ chức dữ liệu -> Logic
- Khái niệm khi học SQL:
	- database -> CSDL
	- table (bảng dữ liệu)
		quản lý sinh viên:
			sinh viên (tên, tuổi, ...) -> Table
			điểm (sinh viên, điểm, môn hoc) ->table
			môn học (tên môn học, ...) -> Table

- Cài đặt môi trường
- Kiến thức sql
	- Tạo CSDL
		-> Tao tool -> DONE
		-> Tạo bằng lệnh:
	- Tạo table
		-> Student
			First Name
			Last Name
			Address
			Phone
		-> Tools -> DONE
		-> Lệnh:
	- Viết 1 vài truy vấn cơ bản
		-> Chèn dữ liệu
			-> Tools -> done
			-> Lệnh
		-> Hiển thị
			-> Tools -> DONE
			-> Lệnh

-- Tao CSDL
create database G2212I
go

-- Kich CSDL
use G2212I
go

-- Tao table student2
create table student2 (
	first_name nvarchar(20),
	last_name nvarchar(20),
	address nvarchar(200),
	phone nvarchar(50)
)
go

-- Xem du lieu
select * from student2
go

-- Chen du lieu
insert into student2 (first_name, last_name, address, phone)
values
(N'Diep', N'TRAN VAN', N'Ha Noi', N'123123123')
go

insert into student2 (first_name, last_name, address, phone)
values
(N'ASDAS', N'asdasd VAN', N'Ha Noi', N'123123123')
go

insert into student2 (first_name, last_name, address, phone)
values
(N'dfgdfg', N'756ghjgh VAN', N'Ha Noi', N'123123123'),
(N'8978978', N'vbnv VAN', N'Ha Noi', N'123123123'),
(N'345345', N'756ghfhfgjgh VAN', N'Ha Noi', N'123123123')
go

-- Xem thong tin du lieu nang cao
select * from student2
go

select first_name, phone from student2
go

select first_name 'Ten', phone 'So Dien Thoai' from student2
go

select first_name as 'Ten', phone 'So Dien Thoai' from student2
go

select first_name as 'Ten', phone as 'So Dien Thoai' from student2
go
-- Tao CSDL
create database G2212I
go

-- Kich CSDL
use G2212I
go

-- Tao table student2
create table student2 (
	first_name nvarchar(20),
	last_name nvarchar(20),
	address nvarchar(200),
	phone nvarchar(50)
)
go

-- Xem du lieu
select * from student2
go

-- Chen du lieu
insert into student2 (first_name, last_name, address, phone)
values
(N'Diep', N'TRAN VAN', N'Ha Noi', N'123123123')
go

insert into student2 (first_name, last_name, address, phone)
values
(N'ASDAS', N'asdasd VAN', N'Ha Noi', N'123123123')
go

insert into student2 (first_name, last_name, address, phone)
values
(N'dfgdfg', N'756ghjgh VAN', N'Ha Noi', N'123123123'),
(N'8978978', N'vbnv VAN', N'Ha Noi', N'123123123'),
(N'345345', N'756ghfhfgjgh VAN', N'Ha Noi', N'123123123')
go

-- Xem thong tin du lieu nang cao
select * from student2
go

select first_name, phone from student2
go

select first_name 'Ten', phone 'So Dien Thoai' from student2
go

select first_name as 'Ten', phone 'So Dien Thoai' from student2
go

select first_name as 'Ten', phone as 'So Dien Thoai' from student2
go

--Primary Key
select * from student2
go

insert into student2 (first_name, last_name, address, phone)
values
(N'Diep', N'TRAN VAN', N'Ha Noi', N'123123123')
go

-- Tao table student3
create table student3 (
	id int primary key,
	first_name nvarchar(20),
	last_name nvarchar(20),
	address nvarchar(200),
	phone nvarchar(50)
)
go

insert into student3 (id, first_name, last_name, address, phone)
values
(1, N'Diep', N'TRAN VAN', N'Ha Noi', N'123123123')
go

select * from student3

insert into student3 (id, first_name, last_name, address, phone)
values
(2, N'Diep', N'TRAN VAN', N'Ha Noi', N'123123123')
go

insert into student3 (id, first_name, last_name, address, phone)
values
(20, N'Diep', N'TRAN VAN', N'Ha Noi', N'123123123')
go

-- drop table
drop table student3
go

delete from student2
go

-- Tao table student3
create table student3 (
	id int primary key identity(1,1),
	first_name nvarchar(20) not null default 'Aptech',
	last_name nvarchar(20) not null,
	address nvarchar(200) not null,
	phone nvarchar(50) unique
)
go

insert into student3 (first_name, last_name, address, phone)
values
(N'Diep', N'TRAN VAN', N'Ha Noi', N'4565767')
go

insert into student3 (first_name, last_name, address)
values
(N'Diep', N'TRAN VAN', N'Ha Noi')
go

select * from student3

-- Error: first_name -> khong dc de trong
insert into student3 (last_name, address, phone)
values
(N'TRAN VAN', N'Ha Noi', N'456546546')
go

--2) Alter table
	--- add new column
	--- thay kieu du lieu column
	--- xoa column
-- Bo sung them column email thi lam the nao
alter table student3
add email nvarchar(150)
go

alter table student3
alter column email int
go

alter table student3
alter column email nvarchar(150)
go

alter table student3
add abc int
go

alter table student3
drop column abc
go
Nội kiến thức:
1) Constraint -> basic
	- Primary key
	- identity
	- unique
	- not null/null
	- default
2) Alter table
	- add new column
	- thay kieu du lieu column
	- xoa column 
3) xoa table

Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó