SQL Server期末超級無敵沖刺高分筆記
想要資料庫期末考試全用SQL寫?庫相關和表相關的背就完事了,至于增刪改查呢就要靠平常的積累了,題目做多了就好了,
如果想要增刪改查相關的筆記呢就點贊吧,點贊的人多了我就有整理的動力了@_@ ! ! !
--==================資料庫相關====================================
--使用庫test
use test;
--創建資料庫test
create database test
on (
name = test,
filename = 'E:\Test\test.mdf',
size = 10mb,
filegrowth = 5mb,
maxsize = 100mb
)
log on (
name = test_log,
filename = 'E:\Test\test_log.ldf',
size = 5mb,
filegrowth = 5mb,
maxsize = 50mb
);
--將資料庫改名為mytest
alter database test modify name = mytest;
--將資料庫主檔案邏輯名更名為mytest
alter database mytest modify file(name = 'test', newname = 'mytest');
--將資料庫日志檔案邏輯名更名為mytest_log
alter database mytest modify file(name = 'test_log', newname = 'mytest_log');
--將資料庫test洗掉
drop database mytest;
--================================表相關===============================================
--創建student表格
create table student (
name varchar(10),
age int,
sex varchar(10)
);
--表格洗掉
drop table student;
--更改表名
exec sp_rename 'student', 'person';
--更改表中屬性名
exec sp_rename 'person.name', 'pname';
--更改表中屬性型別
alter table person alter column pname varchar(15);
--添加表中屬性
alter table person add id int;
--洗掉表中屬性
alter table person drop column id;
--添加非空約束
alter table person alter column pname varchar(15) not null;
--添加主鍵
alter table person add constraint pk_person primary key (pname);
--添加唯一約束
alter table person add constraint uk_person unique (age);
--添加外鍵,被添加的一方必須是主鍵
alter table student add constraint fk_student_person foreign key (name) references person (pname);
--添加check約束
alter table person add constraint ck1_person check (pname like 'a%');
alter table person add constraint ck2_person check (age < 20);
--洗掉主鍵和約束
alter table person drop constraint keyname;
--===================================增刪改查相關==============================================================================
,,,
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/229282.html
標籤:其他
