MySQL19-22

#########################
#第十九章 插入数据 
#########################

#####插入完整的行 
#null表示允许为空 notnull表示不能为空 
#列的数目要保持一致 
insert into customers
values(null,'Pep E. LaPew','100 Main Street','Los Angeles','CA','90046','USA',null,null);

#保险做法 
insert into customers(cust_id,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country,cust_contact,cuat_email)
values(null,'Pep E. LaPew','100 Main Street','Los Angeles','CA','90046','USA',null,null);

#有的时候有些列可以省略 
#1.此列定义允许为null
#2.在表定义中给出默认值 

######插入多个行 
#第一种方法 
insert into customers(cust_id,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country,cust_contact,cuat_email)
values(null,'Pep E. LaPew','100 Main Street','Los Angeles','CA','90046','USA',null,null),
	(null,'M.Martian','42 Galaxy Way','New York','NY','11213','USA',null,null);
#第二种方法 
insert into customers(cust_id,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country,cust_contact,cuat_email)
values(null,'Pep E. LaPew','100 Main Street','Los Angeles','CA','90046','USA',null,null);
insert into customers(cust_id,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country,cust_contact,cuat_email)
values(null,'M.Martian','42 Galaxy Way','New York','NY','11213','USA',null,null);


##插入检索出的数据 insert select
insert into customers(cust_id,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country,cust_contact,cuat_email)
select cust_id,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country,cust_contact,cuat_email
from custnew;
#把custnew中所有数据插入 customers
#这个例子中两个表名称一样,其实MySQL不关心名字对不对应,只要位置对应即可 



############################
#第20章 更新和删除数据 
############################

#更新数据 update...set...where....
#更新单个列 
update customers 
set cust_email='elmer@fudd.com'
where cust_id=10005;
#更新多个列(最后一个列不用逗号)
update customers 
set cust_name='The Fudds',
    cust_email='elmer@fudd.com'
where cust_id=10005;
#删除某个列的值 设置Null
update customers 
set cust_email=null
where cust_id=10005;

##删除数据 delete from ...where
delete from customers
where cust_id=10006;

###1.delete删除的是行,update删除的是列 
###2.delete删除的是表的内容 不是表本身 
###3.truncate table从表中删除所有的行 


###################################
#第21章 创建和操纵表 
###################################

##表创建基础 
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(255) NULL ,
  PRIMARY KEY (cust_id)
) ENGINE=InnoDB;

#1.  名字在create table之后
#2.  主键由primary key指定 ,主键的值必须唯一 
#    但是可以有两个主键比如:primary key(order,item)表示这两个的组合值是唯一的 
#    主键必须NOT NULL
#3.  列名要指定数据类型,是否允许为空
#4.  auto_increment自动增量 必须和主键组合使用,默认情形起始值为1,每次增量为1 
#    select last_insert_id()返回最后一个auto_increment值
#5.  指定默认值 default,看下例 
CREATE TABLE orderitems
(
  order_num  int          NOT NULL ,
  order_item int          NOT NULL ,
  prod_id    char(10)     NOT NULL ,
  quantity   int          NOT NULL  default 1,
  item_price decimal(8,2) NOT NULL ,
  PRIMARY KEY (order_num, order_item)
) ENGINE=InnoDB;
#6.  引擎类型 innofb memory myisam
#    注意:不同引擎类型的表之间不能连接(外键不能连接)


##更新表 alter table
alter table vendors
add vend_phone char(20);#添加新的列 
alter table vendors
drop column vend_phine;#删除列(不仅仅是列的值 )

#alter table的一种常见用途是定义外键 ?????
alter table orderitems
add constraint fk_orderitems_orders
foreign key(order_num) references orders (order_num);

##删除表 删除整个表而不是内容 
drop table customers2;


##重命名表 
rename table customers2 to customers;
rename table backup_customers to customers,
             backup_vendors to vendors;
             

#########################
#第22章 使用视图 
#########################

#创建视图 
create view productcustomers as
select cust_name,cust_contact,prod_id
from customers,orders,orderitems
where customers.cust_id=orders.cust_id
and   orderitems.order_num=orders.order_num;#创建了一个视图 

select * from productcustomers;
select cust_name,cust_contact
from productcustomers
where prod_id='TNT2' #视图中的元素 ,简化使用 


##用视图重新格式化检索出的数据 
##用视图过滤不想要的数据 

##更新视图 (很多都不可以更新,,,) 






















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值