1 / 21

Một số câu lệnh SQL cơ bản

Một số câu lệnh SQL cơ bản. Tạo và xóa database. create database <tên_csdl> Các file data, log sẽ nằm trong thư mục Data của SQL Xóa database drop database <ten_database>. Tạo database create database test on ( name = test_data, filename = "C:test.mdf", size = 1,

Download Presentation

Một số câu lệnh SQL cơ bản

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Một số câu lệnh SQL cơ bản

  2. Tạo và xóa database • create database <tên_csdl> • Các file data, log sẽ nằm trong thư mục Data của SQL • Xóa database • drop database <ten_database> • Tạo database create database test on ( name = test_data, filename = "C:\test.mdf", size = 1, maxsize = 2, filegrowth = 20% ) log on ( name = test_log, filename = "C:\test.ldf", size = 1, maxsize = 2, filegrowth = 20% ) Các file data, log sẽ nằm trong ổ đĩa C

  3. Cấu hình với database • sp_dboption ‘csdl’, ‘read only’, ‘true’ : thiết lập csdl chỉ cho phép read • sp_dboption ‘csdl’, ‘autoshrink’, ‘true’ : cho phép csdl thu hẹp không gian trống định kì • sp_dboption ‘csdl’, ‘single_user’, ‘true’ : csdl chỉ cho phép một người dùng • dbcc drinkdatabase(‘csdl’, n%) : thu hẹp không gian thừa của CSDL xuống còn n% so với ban đầu

  4. Tạo bảng và xóa bảng • Tạo bảng create table <ten_bang> ( <ten_truong> <kieu_dl> [rang_buoc], <ten_truong> <kieu_dl> [rang_buoc] ) Ví dụ create table hocvien ( maso varchar(20) primary key, hoten varchar(30) not null ) • Xóa bảng (xóa cả dữ liệu và cấu trúc bảng) drop table <ten_bang>

  5. Thay đổi cấu trúc bảng • Thêm trường mới alter table <ten_bang> add <ten_cot1> <kieu_dl1> [rang_buoc], <ten_cot2> <kieu_dl2> [rang_buoc] • Xóa bỏ trường đã tồn tại alter table <ten_bang> drop column <ten_cot1>, <ten_cot2> • Thay đổi thuộc tính cho cột (mỗi lần thay đổi một cột) alter table <ten_bang> alter column <ten_cot> <kieu_dl_moi>

  6. Tạo ràng buộc Primary key • Tạo mới bảng create table student ( code int primary key ) create table student ( code int constraintpk_codeprimary key [(code)] ) • Bổ sung khóa chính vào trường trong bảng alter table student add constraint pk_codeprimary key (code)

  7. Tạo ràng buộc Unique • Tạo bảng mới create table student ( code int unique ) create table student ( code int constraint uni_code unique (code) ) • Bổ sung ràng buộc vào bảng đã tồn tại alter table student add constraint uni_code unique (code)

  8. Tạo ràng buộc identity • Tạo mới bảng create table student ( code int identity (1, 1) ) create table student ( code int identity ) create table student ( code int constraint idt_id identity )

  9. Tạo ràng buộc default • Tạo bảng mới create table student ( phone varchar(10) default ‘N/A’ ) create table student ( phone varchar(10) constraint def_phone default ‘N/A’ ) • Bảng đã tồn tại alter table student add constraint def_phone default ‘N/A’ for phone

  10. Ràng buộc foreign key • Tạo bảng mới create table mark ( code int foreign key referencesstudent(code) on delete cascade on update set null ) create table mark ( code int constraint fk_code foreign key referencesstudent(code) on delete cascade on update set null ) • Bảng đã tồn tại alter table mark add constraint fk_codeforeign key (code) references student (code) on delete cascade on update set null

  11. Tạo ràng buộc foreign key • Option1: on delete/on update • Option 2: • no action: không cho bản chi cha xóa hay cập nhật • cascade: nếu bản ghi cha bị xóa hay cập nhật thì bản ghi con sẽ bị ảnh hưởng theo

  12. Ràng buộc not null • Tạo bảng mới create table student ( name varchar(20) not null ) • Không cho phép khi bảng đã tồn tại

  13. Ràng buộc check • Tạo bảng mới create table mark ( mark int check (mark >= 0 and mark <= 10) ) create table mark ( mark int constraint ck_mark check(mark >= 0 and mark <= 10) ) • Bảng đã tồn tại alter table mark add constraint ck_mark check( mark in (3, 5, 6) )

  14. Cú pháp tổng quát • Tạo ràng buộc cho bảng mới create table <ten_bang> (<ten_cot> <kieu_dl> <rangbuoc>) • Bảng đã tồn tại alter table <ten_bang> add constraint <ten_constraint> <loai_constraint> • Xóa constraint alter table <ten_bang> drop constraint <ten_constraint>

  15. Lưu ý khi dùng alter table • Dùng với add: có thể add một lúc nhiều cột, các cột có ràng buộc là default, identity, check • Dùng với alter column: cột chỉ thay đổi là null/not null • Dùng với add constraint: có thể add nhiều constraint một lúc

  16. Xóa dữ liệu với delete • Xóa tất cả dữ liệu trong bảng delete from <tên_bảng> • Xóa dữ liệu ở các hàng thỏa mãn điều kiện delete from <tên_bảng> where <điều kiện>

  17. Chèn dữ liệu với insert • Chèn dữ liệu đầy đủ vào hàng, bảng có bao nhiêu trường thì chèn vào bấy nhiêu giá trị Insert into <tên_bảng> values(gt1, gt2,…) • Chèn dữ liệu cho một số trường trong bảng, các trường khác null bắt buộc phải liệt kê Insert into <tên_bảng>(trường1, trường2,…) values(gt1, gt2,…) • Chèn dữ liệu vào bảng từ bảng khác Insert into <tên_bảng> select <trường1>, <trường2>,… from <tên_bảng2>

  18. Sửa dữ liệu với update • Cập nhật dữ liệu cho tất cả các hàng trong bảng trên trường chỉ ra Update <tên_bảng> set <trường> = <giá_trị> • Cập nhật dữ liệu cho một số hàng trong bảng, cập nhật giá trị cho trường chỉ ra Update <tên_bảng> set <trường> = <giá_trị> where <điều_kiện>

  19. Truy vấn dữ liệu với select • Hiển thị tất cả các bản ghi, mỗi bản ghi chứa đầy đủ các trường select * from <tên_bảng> • Hiển thị tất cả các bản ghi, mỗi bản ghi chỉ chứa một số trường select <trường1>,<trường2>,… from <tên_bảng>

  20. Truy vấn dữ liệu với select (t.t) • Hiển thị các bản ghi thỏa mãn điều kiện, mỗi bản ghi chứa đầy đủ các trường select * from <tên_bảng> where <điều_kiện> • Hiển thị các bản ghi thỏa mãn điều kiện, mỗi bản ghi chỉ chứa một số trường select <trường1>,<trường2>,… from <tên_bảng> where <điều_kiện>

  21. Truy vấn dữ liệu với select (t.t) • Hiển thị dữ liệu có sắp xếp select <trường1>,… from <tên_bảng> where <điều_kiện> order by <trường> • Hiển thị dữ liệu theo nhóm Select <trường1>,… from <tên_bảng> where <điều_kiện> group by <trường1>,… having <điều_kiện> Lưu ý: • tất cả các trường trong mệnh đề group by phải xuất hiện trong select • các trường xuất hiện ở điều kiện trong mệnh đề having phải xuất hiện trong select

More Related