GIÁO TRÌNH MÔN CƠ SƠ DỮ LIỆU

4 381 0
GIÁO TRÌNH MÔN CƠ SƠ DỮ LIỆU

Đang tải... (xem toàn văn)

Thông tin tài liệu

Tài liệu môn CSDL

Tao CSDL bằng lệnh Create database create database SalesDb on ( name= Salesdb_dat, filename='d:\\data\salesdat.mdf', size = 10mb, maxsize = 50mb, filegrowth=5% ) log on ( name= salesDb_log, filename='d:\\data\salesdat.log', size=5mb, maxsize=25mb, filegrowth=10% ) Go Thay đổi CSDL bằng lệnh Alter alter database SalesDb modify file (name='salesdb_log', size=10mb) alter database SalesDb add file (name=salesdb_data2, filename= 'd:\\data\salesdat2.mdf', size=5mb, maxsize=10mb) Xóa CSDL bằng lệnh Drop Drop database salesDb1 Thực thi các thủ tục sẵn execute sp_renamedb salesdb, salesDb1 đổi tên execute sp_addtype isbn, 'smallint', 'not null' tạo kiểu dữ liệu execute sp_addtype zipcode, 'char(10)', null execute sp_droptype isbn xóa kiểu dữ liệu use salesDb1 execute sp_help trợ giúp select domain_name, data_type, character_maximum_length from information_schema.domains xem order by domain_name Tạo bảng dữ liệu với các ràng buộc identity, not null use salesDb1 create table Employees ( employeeID smallint identity(1,1) not null, firstname nvarchar(30) not null, Lastname nvarchar(30) not null, Address1 nvarchar(60) not null, city nvarchar(15) not null, state char(2) not null, phone varchar(24) not null, DOB datetime not null, hiredate datetime not null, positionid tinyint not null ) Thay đổi bảng dữ liệu bằng lệnh Alter alter table employees add address2 nvarchar(16) not null go use salesDb1 alter table employees drop column address2 alter table employees alter column address2 nvarchar(60) Xóa bảng dữ liệu bằng lệnh Drop Drop table employees Xóa tất cả dữ liệu trong bảng bằng lệnh Truncate (không bật bẫy lỗi) Truncate table employees Tạo bảng tạm thời cục bộ (sẽ xóa khi kết thúc phiên làm việc) create table #tamcucbo ( id int primary key, cola varchar(30) not null, colb varchar(30) not null ) Tạo bảng tạm thời cho tất cả người dùng (sẽ xóa khi kết thúc phiên làm việc) create table ##tamtoancuc ( id int primary key, cola varchar(30) not null, colb varchar(30) not null ) Tạo ràng buộc default alter table employees add default 'party' for address2 không tạo tên ràng buộc alter table employees add constrainst add_defa default 'party' for address tênm ràng buộc go Tạo một defaul và bind nó với một cột create default addr as 'CA' go execute sp_bindefault addr, 'employees.address2' Định danh identity khi insert không cần insert giá trị này use salesDb1 Create table table1 ( ID INT identity, firstname varchar(30) not null, Lastname varchar(30) not null ) insert table1 (firstname, Lastname ) values ('minh','thu') set identity_insert table1 on insert table1 (ID, firstname, Lastname ) values (99, 'minh','thu') chỉ định giá trị Tạo ràng Check alter table table1 add constraint qua_chk check (firstname != 'nga') alter table table1 add constraint quat_chk check (id != 10) go Tạo rule và bind create rule activeda as @date between '01/01/70' and getdate() go sp_bindrule activeda, 'employees.DOB' execute sp_unbindrule 'employees.DOB' use salesDb1 Create table table2 ( manv INT not null, firstname varchar(30) not null, Lastname varchar(30) not null ) Tạo ràng buộc khóa chính primary key alter table table2 add constraint t2_pk primary key (manv) exec sp_helpconstraint table2 chỉ các ràng buộc Xóa ràng buộc alter table table2 drop constraint t2_pk Tạo ràng buộc unique alter table nhanvien add scmnd char(15) constraint nv_unique unique dạng cột alter table nhanvien add constraint nv_pb_fk foreign key (mapb) references phong(mpb) dạng bảng . Tao CSDL bằng lệnh Create database create database SalesDb on ( name= Salesdb_dat, filename='d:\datasalesdat.mdf',. filename='d:\datasalesdat.log', size=5mb, maxsize=25mb, filegrowth=10% ) Go Thay đổi CSDL bằng lệnh Alter alter database SalesDb modify file (name='salesdb_log', size=10mb). (name=salesdb_data2, filename= 'd:\datasalesdat2.mdf', size=5mb, maxsize=10mb) Xóa CSDL bằng lệnh Drop Drop database salesDb1 Thực thi các thủ tục có sẵn execute sp_renamedb

Ngày đăng: 28/03/2014, 20:37

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan