表是資料庫的主要物件,也是資料庫其他物件的基礎。表分為永久性表和臨時性表。通過sql語句可實現建立、修改和刪除表,以及查詢表的資料。我的理解:列對應表的結構,行對應表的資料。
//建立表需要使用create關鍵字
create
table student(uid varchar(20
)not
null
,name varchar(20
)not
null
,*** varchar(20
)not
null
)//建立名為student的資料表,varchar表示可變長度的char,not null表示資料不能為空
//在建立之後修改表的結構需要使用alter table table_name 尋找表
//新增列需要使用add關鍵字,刪除列需要使用drop關鍵字
alter
table student add age int
notnull
alter
table student drop age
//刪除表需要使用drop關鍵字
drop student
//插入使用insert into table_name(item,item,....,) values(value,value)
//item與value一一對應即可
insert
into student(uid,name,***,age)
values(1
,1,'male',18
)
//修改使用update關鍵字,可對特定的一行或多行進行修改
update student
set ***=
'male'
,age=age+
1//為修改的內容
where uid>
7//指定修改的行
//刪除使用delete 關鍵字,可刪除特定的一行或多行
delete
from table_name
where search_condition
//檢索使用select關鍵字,可檢索特定的一行或多行的特定的列
select ***,uid from student where age>
19//age>19的行***,uid列的資料
select
*from student where age>
19//age>19的行所有列的資料
對於匹配查詢,like關鍵字的具有4種匹配符,可組合使用
匹配符含義
%任意字元
_任意單字元
裡的任意字元
[^]不含裡的任意字元
select
*from student where *** like
'%ale'
限制表中行或列的資料,及表之間的資料實現資料的完整性。
型別:主鍵約束、唯一性約束、外來鍵約束、check約束和預設約束
限制表中特定的一列或者多列都具有唯一值,且所包含的列不能有空值,確保資料的唯一性
//設定主鍵約束
///建立時新增
constraint stu_id primary
key(uid,name)
///建立後新增
alter
table student add
constraint stu_id primary
key(uid,name)
//帶有約束名的主鍵約束
alter
table student add
primary
key(uid)
//刪除主鍵約束
alter
table student drop
primary
key//查詢主鍵資訊
select column_name from information_schema.
`key_column_usage`
where table_name=
'student'
and constraint_name=
'primary'
同主鍵約束的關係
聯絡:都不允許約束物件重複
區別primary key
unique
唯一的標識表中的資料,可定義特定的一列或多列為主鍵
限制不受主鍵約束的列上資料的唯一性
不能更新資料
只要資料唯一即可更新
不允許null
允許null
可做外來鍵
不可做外來鍵
///建立使新增
constraint stu_uid unique
(uid)
///建立後新增
alter
table student add
constraint stu_uid unique
(uid)
限制乙個表中的某些列與其他表中的某些列的關聯,從而實現表之間的依賴關係。在其他關聯的表中只能刪除沒有依賴的資訊。
alter
table student
addconstraint stu_outkey
foreign
key(uid)
//外來鍵設定
references mark(uid)
//所關聯的其他表的列
在mark表中只能刪除student中沒有的uid
對錶的某一列資料的範圍限制
alter
table student
addconstraint stu_check
check
(score>
0and score<
101)
//check(logic_expression)
向表中插入的資料有很多相同的元素,可以將其設定為預設約束
//新增預設約束
alter
table mark alter
column score set
default
90//選定列 set default
alter
table mark modify score int
default
90//
//刪除預設約束
alter
table mark modify score int
//插入資料時不會報錯
alter
table mark alter
column mark drop
default
//
儲存過程封裝了可以重複使用的transact-sql語句。它儲存在伺服器上,已經通過預編譯
分類:系統、本地、臨時、遠端、擴充套件~
~:儲存過程
create procedure additem()
begin
declare i int default 3;
while (i<11) do
insert into student(uid,name,***,age) values(i,i,'female',20);
set i=i+1;
end while;
end
MS SQL系統資料表內容
1.今天聽到有人問 怎麼用sql語句取指定列的資料型別,覺得很無聊,但也許是我還不知道有什麼用的原因吧.查了一下,這些東西都是存於每乙個資料庫的syscolumns表裡面得,name就是列名,xtype就是資料型別,但是這個xtype是數字的,下面是數字和資料型別對應的關係 xtype 34 ima...
資料表操作
1 建立資料表 create table if not exists table name column name data type,2 檢視資料表 show tables show tables from mysql 3 檢視資料表結構 show columns from tbl name 4 ...
資料表操作
create table 表名 欄位名 型別 約束,欄位名 型別 約束 例 建立學生表,字段要求如下 姓名 長度為10 create table students name varchar 10 例 建立學生表,字段要求如下 姓名 長度為10 年齡 create table students nam...