重要的ddl語句:
- create database - 建立新資料庫
- alter database - 修改資料庫
- create table - 建立新錶
- alter table - 變更(改變)資料庫表
- drop table - 刪除表
- create index - 建立索引(搜尋鍵)
- drop index - 刪除索引
1.select語句
select 語句用於從表中選取資料。
結果被儲存在乙個結果表中(稱為結果集)。
語法:select 列名稱 from 表名稱
或select * from 表名稱
例子:
select lastname,firstname from persons
使用distinct關鍵字select
distinct company from orders
使用where子句規定選擇的標準
語法:select 列名稱 from 表名稱 where 列 運算子 值
select * from persons where city='beijing'
常用運算子有:= 、<>(不等於)、>、<、between(在某個範圍之類)、like(搜尋某種模式)
使用like操作符搭配where
「%」 可用於定義萬用字元(模式中缺少的字母)。
從 「persons」 表中選取居住在以 「n」 開始的城市裡的人:
select * from persons
where city like
'n%'
從 「persons」 表中選取居住在以 「g」 結尾的城市裡的人:
select * from persons
where city like
'%g'
使用and 、or 搭配whereselect * from persons where firstname='thomas'
and lastname='carter'
select * from persons where firstname='thomas'
or lastname='carter'
2.insert into 語句
insert into 語句用於向**中插入新的行。
語法:
insert
into 表名稱 values (值1, 值2,....)
或
insert into table_name (列1, 列2,...) values (值1, 值2,....)
3.update 語句
update 語句用於修改表中的資料。
語法:update 表名稱 set 列名稱 = 新值 where 列名稱 = 某值
update person set address = 'zhongshan 23', city = 'nanjing'
where lastname = 'wilson'
4.delete 語句
delete 語句用於刪除表中的行。
語法:delete from 表名稱 where 列名稱 = 值
例子:
刪除某一行資料
delete
from person where lastname = 'wilson'
刪除所有(僅保留表頭)
delete
from table_name
或
delete * from table_name
5.create database 語句
create database 用於建立資料庫。
語法:create database database_name
6.create table 語句
create table 語句用於建立資料庫中的表。
語法:
create
table 表名稱
(列名稱1 資料型別,
列名稱2 資料型別,
列名稱3 資料型別,
....
)
資料型別
描述integer(size)
int(size)
smallint(size)
tinyint(size)
僅容納整數。在括號內規定數字的最大位數。
decimal(size,d)
numeric(size,d)
容納帶有小數的數字。
」size」 規定數字的最大位數。」d」 規定小數點右側的最大位數。
char(size)
容納固定長度的字串(可容納字母、數字以及特殊字元)。
在括號中規定字串的長度。
varchar(size)
容納可變長度的字串(可容納字母、數字以及特殊的字元)。
在括號中規定字串的最大長度。
date(yyyymmdd)
容納日期。
7.create index
語句用於在表中建立索引。
語法:
create
index index_name
on table_name (column_name)
用unique約束值建立唯一的索引create
unique index index_name
on table_name (column_name)
8.drop
用於刪除索引、表和資料庫。
刪除專案
語句語法
刪除**中的索引
drop index
alter table table_name drop index index_name
刪除表(表的結構、屬性以及索引也會被刪除)
drop table
drop table 表名稱
用於刪除資料庫
drop database
drop database 資料庫名稱
僅僅刪除**中的資料
truncate table
truncate table 表名稱
9.alter table
用於在已有的表中新增、修改或刪除列。
語法:
在表中新增列:
alter
table table_name
add column_name datatype
在表中刪除列:
alter
table table_name
drop
column column_name
在表中修改列的型別:
alter
table table_name
alter
column column_name datatype
SQL基本語法
update 有關update,急!在oracle資料庫中 表 a id firstname,lastname 表 b id,lastname 表 a 中原來id,firstname兩個欄位的資料是完整的 表 b中原來id,lastname兩個欄位的資料是完整的 現在要把表 b中的lastname欄...
SQL基本語法
1.select列名稱 from 表名稱 2.selectdistinct 列名稱 from 表名稱 3.select列名稱 from 表名稱 where 列 運算子值 eg select frompersons where city beijing select from persons wher...
sql基本語法
啟動 關閉資料庫 1.首先進入到資料庫所在的目錄c program files postgresql 11 bin 2.pg ctl.exe d data start stop 進入使用者 psql h localhost p 5432 u 使用者名稱 可以將使用者postgres中所有的表資料備份...