UPDATE customers SET cust_email='[email protected]' WHERE cust_id=10005;
UPDATE customers SET cust_name='The fudds',cust_email='[email protected]' WHERE cust_id=10005;
UPDATE customers SET cust_email=NULL WHERE cust_id=10005;
DELETE FROM customers WHERE cust_id=10005;
TRUNCATE custnew;
删除表中所有
DELETE custnew;
不带WHERE字句
TRUNCATE custnew;
TRUNCAT,其实是,先删除表custnew,然后重新建表custnew;
CREATE TABLE IF NOT EXISTS customers(
cust_id int(11) 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;
CREATE TABLE vendors(
vend_id int(11) NOT NULL AUTO_INCREMENT,
vend_name char(50) NOT NULL,
vend_address char(50) NULL,
vend_city char(50) NULL,
vend_state char(5) NULL,
vend_zip char(10) NULL,
vend_country char(50) NULL,
PRIMARY KEY(vend_id)
)ENGINE=InnoDB;
CREATE TABLE orderitems(
order_num int NOT NULL,
order_item int NOT NULL,
prod_id char(10) NOT NULL,
quantity int NOT NULL,
item_price decimal(8,2) NOT NULL,
PRIMARY KEY(order_num,order_item)
)ENGINE=InnoDB;
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;
CREATE TABLE orders(
order_num int(11) NOT NULL AUTO_INCREMENT,
order_date datetime NOT NULL,
cust_id int(11) NOT NULL,
PRIMARY KEY(order_num)
)ENGINE=InnoDB;
ALTER TABLE vendors ADD vend_phone char(20);
ALTER TABLE vendors DROP COLUMN vend_phone;
decimal(8,2),
8:整数部分的位数不能超过 8-2 = 6
2:小数部分的位数不能超过2。如果小数位数不足2,会自动补齐;如果小数位数超过2,会自动截断;
单个列做主键
组合列做主键
获取 AUTO_INCREMENT的值:SELECT last_insert_id();
使用DEFAULT 设置默认值。
mysql中的几种数据库引擎:
MyISAM:支持全文本搜索,不支持事务处理
Memory:和MyISAM类似,支持全文本搜索,不支持事务处理;数据存储在内存中,速度快,特别适合临时表;
InnoDB:不支持全文本搜索,支持事务处理
修改表结构
ALTER TABLE vendors ADD vend_phone char(20);
ALTER TABLE vendors DROP COLUMN vend_phone;
创建外键:
ALTER TABLE orders ADD CONSTRAINT fk_orders_customers FOREIGN KEY(cust_id) REFERENCES customers(cust_id);
重命名表名称
RENAME TABLE customers TO backup_customers;
RENAME TABLE orders TO backup_orders;
RENAME TABLE backup_customers TO customers1,backup_orders TO orders1;
删除表
DROP TABLE vendors;
删除数据库
DROP DATABASE test; //test为数据库名称