標籤:模式 sage 進階 相同 column 刪除資料庫 資料類型 desc 分組
建立資料庫:
命令: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資料庫常用命令