标签:检索 customers cust update 查改 MySQL 增删 select name
目录1 select
1.1 检索单个列
select prod_name
from products;
1.2 检索多个列
select prod_id, prod_name, prod_price
from products;
1.3 检索所有列
select *
from products;
1.4 检索不同的行
select distinct vend_id
from products;
1.5 限制结果
select prod_name
from products
limit 5;
此语句使用 select
语句检索单个列。limit 5
指示 MySQL 返回不多于 5 行。
select prod_name
from products
limit 5, 5;
limit 5, 5
指示MySQL返回从行5开始的5行。第一个数为开始位置,第二个数为要检索的行数。
2 insert
顾名思义,insert
是用来插入(或添加)行到数据库表的。插入可以用几种方式使用:
- 插入完整的行;
- 插入行的一部分;
- 插入多行;
- 插入某些查询的结果。
2.1 插入完整的行
insert into customers(cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
values('Pep E. LaPew', '100 Main Street', 'Los Angeles', 'CA', '90046', 'USA', null, null);
2.2 插入多个行
insert into customers(cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country)
values('Pep E. LaPew', '100 Main Street', 'Los Angeles', 'CA', '90046', 'USA');
insert into customers(cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country)
values('M. Martian', '42 Galaxy Way', 'New York', 'NY', '11213', 'USA');
2.3 插入检索出的数据
假如你想从另一表中合并客户列表到你的 customers 表。不需要每次读取一行,然后再将它用INSERT插入,可以如下进行:
insert into customers(cust_id,
cust_contact,
cust_email,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country)
select cust_id,
cust_contact,
cust_email,
cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country
from custnew;
3 update
为了更新(修改)表中的数据,可使用 update
语句。可采用两种方式使用 update
:
- 更新表中特定行;
- 更新表中所有行。
3.1 更新表中特定行
update customers
set cust_email = 'joy@example.com'
where cust_id = 10005;
3.2 更新表中所有行
update customers
set cust_email = 'joy@example.com';
3.3 更新多个列
update customers
set cust_name = 'the fudds',
cust_email = 'joy@example.com'
where cust_id = 10005;
3.4 删除指定的列
为了删除某个列的值,可设置它为 null
(假如表定义允许 null
值)。
update customers
set cust_email = null
where cust_id = 10005;
4 delete
为了从一个表中删除(去掉)数据,使用 delete
语句。可以两种方式使用 delete
:
- 从表中删除特定的行;
- 从表中删除所有行。
4.1 删除指定的行
delete from customers
where cust_id = 10006;
4.2 删除所有行
delete from customers;
5 更新和删除的指导原则
下面是许多 SQL 程序员使用 update
或 delete
时所遵循的习惯。
- 除非确实打算更新和删除每一行,否则绝对不要使用不带
where
子句的update
或delete
语句。 - 保证每个表都有主键,尽可能像
where
子句那样使用它(可以指定各主键、多个值或值的范围)。 - 在对
update
或delete
语句使用where
子句前,应该先用select
进行测试,保证它过滤的是正确的记录,以防编写的where
子句不正确。 - 使用强制实施引用完整性的数据库,这样 MySQL 将不允许删除具有与其他表相关联的数据的行。
标签:检索,customers,cust,update,查改,MySQL,增删,select,name 来源: https://www.cnblogs.com/clipboard/p/13882362.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。