創建數據庫:
命令:create database 數據庫名;
示例:create database student;
刪除數據庫:
命令:drop database 數據庫名;
示例:drop database student;
新建表格:
命令:create table 表名
(列名 數據類型,列名2.....)
示例:create table student
(sname char(20),sid int)
刪除表格:
命令:drop table 表名
示例:drop table student
修改表結構:
(插入(新增)列)
命令:alter table 表名
add 新列名 數據類型
示例:alter table student
add sage int
(刪除列)
命令:alter table 表名
drop column 列名
示例:alter table student
drop column sid
(修改列類型)
命令:alter table 表名
alter column 列名 數據類型
示例:alter table student
alter column sid float(浮點型)
(新增約束)
命令:alter table 表名
alter column 列名 新數據類型
示例:alter table student
alter column PK_sid primary key(sid)(新增的約束類型是主鍵約束)
(刪除約束)
命令:alter table 表名
drop 列名
示例:alter table student
drop PK_sid
查詢表內容:
命令:select 要查詢的數據列名
from 表名
where 篩選條件(無法對分組后的數據進行篩選)
(高級搜索)【group by 列名(分組)
having 篩選條件(只能對分組后的數據進行篩選)
order by 排序方式(控制數據最后輸出的排列方式有正序:asc、倒敘:desc)】
示例:select sid
from student
where sid=2
【group by sid
having sid=1
order by desc】
在表中插入數據:(值與列必須一一對應)
命令:insert into 表名
(列名 ,列名)
values
(值,值)
示例:insert into 表名
(sname,sid,sage)
values
(‘張三’,12,15)
修改表中數據值:
命令:update from 表名
set 列名=新值
示例:update from student
set sname='李四'
查詢模式:(批量插入多條數據)
命令:insert into 表名(值的總數必須和列的總數相同)
select 值,值,值 union all
selevt 值,值,值
示例:insert into 表名
select '張三',15,18
select '李四',16,19
視圖:
命令:create view 視圖名
as
select 列
from 表名
示例:create view students
as
select sname
from student
SQL Server刪除表及刪除表中數據的方法
刪除表的T-SQL語句為:
drop table <表名>
drop是丟棄的意思,drop table表示將一個表徹底刪除掉。
刪除表數據有兩種方法:delete和truncate。
delete的用法如下:
delete from <表名> [where條件]
truncate的用法如下:
truncate table <表名>
delete和truncate的區別如下:
1、delete可以刪除表中的一條或多條數據,也可以刪除全部數據;而truncate只能將表中的全部數據刪除。
2、delete刪除表數據后,標識字段不能復用。也就是說如果你把id=10(假如id是標識字段)的那行數據刪除了,你也不可能再插入一條數據讓id=10.
3、truncate刪除表數據后,標識重新恢復初始狀態。默認為初始值為1,也就是說,truncate之后,再插入一條數據,id=1.
【摘自】
1.https://www.cnblogs.com/yuzhonghua/p/7612594.html