雖然select語句可以一次性查出所有的資料,但受計算機螢幕大小的限制,每次隻能看到部分行的内容,要看其它内容的時候就要用到滾動條。由于網絡的限制,對于web應用來說,這種方式的效率非常低下,資料量比較大的時候幾乎是不能使用的。
事實上,通常采用的方法是根據螢幕的大小,一次隻查出部分行并顯示,如果要看其它的内容,通常采用分頁的方式。
那如何在SQL語句中查部分行的内容呢?就要用到limit關鍵字了。
kingbase中可以用下面的語句:
1、查詢第一行記錄:
select *
from 表名 limit 1
2、查詢第n行到第m行記錄
select * from 表名 offset
n-1 limit m-n;
3、查詢前n行記錄
select * from 表名 limit n;
mysql中可以用下面的語句:
select * from 表名 limit
n-1,m-n;
上面的n和m可以通過計算得到,比如用“select count(*) from
表名”先得到所有的資料的個數,再根據每一個頁面能夠顯示的項目數,進行簡單的計算,就可以得到想要得結果。
靈活運用上面的語句有時可以起到意想不到的效果!比如要查找選修課程最多的學生的學号和選課數可以隻用下面的一條語句就可以實作了。
select
學号,count(課程号) 選課數 from 學生選課.選課 group by 學号 order
by 選課數 desc limit 1;
當然如果隻想查出選修課程最多的學生的學号就要麻煩一些了,用下面的語句
select 學号
from(select 學号,count(課程号) 選課數 from 學生選課.選課 group
by 學号) t order by t.選課數 desc limit 1;
建立表
create tabel 表名
create table customers
(
cust_id int not null auto_increment,
--不能為空,增量(可以付初始值))
cust_name char(50) not null,
cust_address char(50) null,
cust_city char(50) null,
cust_state char(5) null,
cust_zip char(10) null,
cust_country char(50) null,
cust_contact char(50) null,
cust_email char(50) null,
primary key(cust_id)
--指定主鍵
)engine=innodb;
--引擎
組合主鍵
primary key(order_num,order_item)
--删除資料庫
drop database 資料庫名字
------------------------------------------------------------
item_price decimal(8,2) not null,
quantity int not null default 1,
primary key(cust_id,quantity)
------------------------------------------------------------------
--給vendors表增加一個vend_phone的清單
alter table vendors
add vend_phone char(20);
--删除整個表
drop table customers2;
--重命名表
rename table customers2 to customers;
--删除列
drop column vend_phone;
隻修改列的資料類型的方法:
通常可以寫成 alter table 表名 modify column 列名 新的列的類型
例如:student表中列sname的類型是char(20),現在要修改為varchar(20),SQL語句如下
alter table student modify column sname varchar(20);
同時修改列名和列的資料類型的方法:
通常可以寫成 alter table 表名 change column 舊列名 新列名 新的列類型
例如:student表中列sname的類型是char(20),現在要修改為stuname
varchar(20),SQL語句如下
alter table student change column sname stuname
varchar(20);
----------------------------------------------------------------------------------------------------
--插入行
insert into customers
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email
)
values
null,
'pep e.lapew',
'100 main street',
'los angeles',
'ca',
'90046',
'usa',
);
--插入多行
cust_country
'pep e,lapew',
'usa'
),
'm.martian',
'42 galaxy way',
'new york',
'ny',
'11213',
'usa'
--表插入到表
cust_id,
cust_email,
select cust_id,
cust_coutact,
--從custnew裡面
from custnew;
------------------------------------------------------------------------
修改行
update customers
set cust_email = '[email protected]'
cust_name = ‘the fudds’
where cust_id = 10005;
删除值
set cust_email = null
删除一行
delete from customers
where cust_id = 10006;
修改所有行
----------------------------------------------------------------------------
--降低into優先級
insert low_priority into
--降低update優先級
insert low_priority update
--降低delete優先級
insert low_priority delete