列即字段
行即記錄
一、數據庫:
1.建立數據庫
create database 數據庫名;
use 數據庫名;create database exp1;
use exp1;
mysql同樣
2.刪除數據庫
drop database 數據庫名;
drop database exp1;
mysql同樣
二、表:
1.創建表
create table 表名(
列名1 類型1 [約束],
列名2 類型2 [約束],
...
);create table Student(
Sno Char(7) primary key,--學號
Sname Char(10) not null,--學生姓名
Ssex Char(2) not null,--性別
Sage Smallint,--年齡
Clno Char(5) not null--學生所在班級號
);
mysql同樣
2.刪除表
drop table 表名;
drop table test;
mysql同樣 不過都儘量少寫 威力太大
3.修改表
3.1修改表名
exec sp_rename '舊錶名','新表名'
exec sp_rename 'test', 'test1'
mysql不一樣
alter table 舊錶名 rename to 新表名;
alter table user1 rename to user10;
3.2添加字段
alter table 表名 add 字段名 字段描述;
alter table Student add Birthday Datetime;
mysql一樣
3.3刪除字段
alter table 表名 drop column 字段名;--必須加column 不加指的是刪除約束
alter table Student drop column Class;
mysql不一樣 不用加column
alter table 表名 drop 字段名;
alter table user drop pwd;
3.4修改字段名
sp_rename '表名.舊字段名','新字段名'
sp_rename 'Student.Class','clazz';
mysql不一樣
alter table 表名 change 字段名稱 新字段描述; -- 可以順便改描述
alter table user change password pwd varchar(10);
3.5修改字段描述
alter table 表名 alter column 字段名 新描述
alter table Student alter column Class char(10);
mysql不一樣
alter table 表名 modify 字段名稱 字段類型 [約束];
alter table user modify pwd int; -- 不能改名稱 只能改描述
三、索引
1.添加索引
create [unique] [clustered] index 索引名 on 表名(字段名 [asc],字段名名 [desc]); --asc升序 desc降序 默認升序
create unique index Stusno on Student(Sno);--student表按學號升序建立唯一索引
create unique index SCno on Cj(Sno asc,Cno desc);--Cj表按學號升序 課程號降序建立唯一索引
create clustered index CjGde on Cj(Grade desc);--建立聚簇索引 (聚集索引)
-- sql server可視化工具建索引 類型改成索引即可 建立聚簇索引將‘創建爲聚集的’改爲‘是’即可
2.重命名索引
exec sp_rename '表名.索引名','新索引名'
exec sp_rename 'Student.Stusno','Stuno';
3.刪除索引
drop index 表名.索引名
drop index Student.Stuno;
以上爲自己的筆記,也可以參考https://www.cnblogs.com/yuzhonghua/p/7612594.html