第四课(MySQL基础知识)

2016年11月14学习内容:

一、插入语句

现有表如下:

CREATE TABLE `customers` (
`cust_id` int(11) NOT NULL AUTO_INCREMENT,
`cust_name` char(50) NOT NULL,
`cust_address` char(50) DEFAULT NULL,
`cust_city` char(50) DEFAULT NULL,
`cust_state` char(5) DEFAULT NULL,
`cust_zip` char(10) DEFAULT NULL,
`cust_country` char(50) DEFAULT NULL,
`cust_contact` char(50) DEFAULT NULL,
`cust_email` char(255) DEFAULT NULL,
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=10008 DEFAULT CHARSET=utf8

插入语句一般有以下几种情况:
1. 插入全部行:

insert into table customers values(null,'name1',null,'city','state','zip','china','contact1','xxx@163.com');

其中自增列和不需要填写的列用null替代。
2. 插入全部或者不分行:

insert into table(cust_name,cust_city,cust_state,cust_zip,cust_country,cust_contact,cust_email) customers values('name1',null,'city','state','zip','china','contact1','xxx@163.com');

其中自增列和不需要填写的列不写出来即可,插入时尽量使用这种写法,灵活,不容易出错。
3. 插入多条数据:

insert into table(cust_name,cust_city,cust_state,cust_zip,cust_country,cust_contact,cust_email) customers values('name1',null,'city1','state1','zip1','china1','contact1','xxx@163.com'),
('name2',null,'city2','state2','zip2','china2','contact2','xxx@163.com');

插入多条语句,也可以使用多条insert,但是没有这样效率高,尽量用这种写法。
4. 从别的数据表中查询并插入:

insert into table_new (column1, column2, column3, ...) select column1, column2, column3,... from table_old; 

其中第二个select可以包含where语句,进行过滤。
如果两张表的表结构完全一样,可以使用以下语句替代:

insert into table_new select * from table_old;

如果数据检索是最重要的(通常是这样),则你可以通过在INSERT和INTO之间添加关键字LOW_PRIORITY,指示MySQL降低INSERT语句的优先级。UPDATE和DELETE语句也一样。

二、更新语句

update customers set cust_email = 'new email' where cust_id = '100002';
update customers set cust_email = 'new email', cust_contact = ' new contact person';

注意:第二条语句没有使用where,相当于更新整个表,需要注意别出错了。
如果想设值为空,可以如下:

update customers set cust_email = null where cust_id = '100002';

三、删除语句

1 . 删除特定行

delete from customers where cust_id = '100002';

2 . 删除所有行

delete from customers;或者 truncate table customers;

前者相当于逐行删除,后者相当于直接删除,然后新建表。
输入更新和删除是需要注意 如果其他表有该行的外键,可能会更新或者删除出错,这是正常的,避免无意删除重要的数据,因此外键还是很有用的。

四、创建和操纵表

  1. 创建表
    是否为空 NULL:允许为空 NOT NULL 不允许为空
    设置默认值 DEFAULT NULL 、DEFAULT 0等;
    设置主键 PRIMARY KEY (cust_id)或者PRIMARY KEY (cust_id,cust_name),主键可以有多列,主键会自动建索引
    设置自增 AUTO_INCREMENT,每个表只允许一个AUTO_INCREMENT列,而且它必须被索引。
    引擎 ENGINE=xx 引擎类型可以混用,同一个数据库的不同表可以使用不同的引擎。外键不能跨不同的引擎。
    InnoDB:事务处理引擎,不支持全文本搜索;
    MEMORY:在功能等同于MyISAM,但由于数据存储在内存(不是磁盘)中,速度很快(特别适合于临时表);
    MyISAM是一个性能极高的引擎,它支持全文本搜索,但不支持事务处理。
  2. 更新表
    添加列: alter table venders add vend_phone char(20);
    删除列: alter table venders drop column vend_phone;
    添加索引 alter table orderitems add constraint fk_orderitems_orders foreign key (order_num) references orders (order_num);
  3. 删除表
    drop table venders;
  4. 重命名表
    rename table customers2 to customers;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值