MySQL学习笔记(1)
//登录MySQL
mysql -uroot -padmin
//查询数据库
show databases;
//选择数据库
use stu;
//查询数据库中的表
show tables;
建表语句
create table if not exists user
(
userid int primary key auto_increment,
username varchar(4) not null,
sex enum('男','女') default '男'
)Engine=InnoDB DEFAULT CHARSET=utf8mb4;
//auto_increment:自动生成线性递增序列
//primary key 主键 。如果一个表包含auto_increment列,则它会被视为primary key
//Engine:与列定义一起,还可以指定存储引擎。
列出所有引擎
mysql> show engines\G
*************************** 1. row ***************************
Engine: MEMORY
Support: YES
Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 2. row ***************************
Engine: MRG_MYISAM
Support: YES
Comment: Collection of identical MyISAM tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 3. row ***************************
Engine: CSV
Support: YES
Comment: CSV storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 4. row ***************************
Engine: FEDERATED
Support: NO
Comment: Federated MySQL storage engine
Transactions: NULL
XA: NULL
Savepoints: NULL
*************************** 5. row ***************************
Engine: PERFORMANCE_SCHEMA
Support: YES
Comment: Performance Schema
Transactions: NO
XA: NO
Savepoints: NO
*************************** 6. row ***************************
Engine: MyISAM
Support: YES
Comment: MyISAM storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 7. row ***************************
Engine: InnoDB
Support: DEFAULT
Comment: Supports transactions, row-level locking, and foreign keys
Transactions: YES
XA: YES
Savepoints: YES
*************************** 8. row ***************************
Engine: BLACKHOLE
Support: YES
Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
XA: NO
Savepoints: NO
*************************** 9. row ***************************
Engine: ARCHIVE
Support: YES
Comment: Archive storage engine
Transactions: NO
XA: NO
Savepoints: NO
9 rows in set (0.00 sec)
查看user表的结构
show create table user;
复制表的结构
create table table1 like tabel2 ;
插入、更新和删除行
//插入数据
insert into user(username) values
('张三'),
('李四');
//删除数据
delete from user where username='张三';
//修改表中数据
update user set sex='女' where username='李四';
//where 子句是强制性的。如果没有给出它,delete 将删除表中所有的行。建议在事务中修改数据,以便在发现任何错误的时候轻松的回滚更改
replace 、insert、on duplicate key update
在很多情况下,需要处理重复项。行的唯一性由主键标识。如果行已经存在,则replace会简单地删除行并且插入新行;如果行不存在,则replace等同于insert
在行已经存在的情况下处理重复项,则需要使用,on duplicate key update 。如果制定了on duplicate key update 选项,并且insert 语句在primary key 中引发了重复值,则MySQL会用新值更新已有行;
replace into user(username,sex) values('李四','女');
//结果受影响的行为两行;
修改表的结构测试 on duplicate key update 一列 age;
alter table user add column age int defalut 18;
//测试on duplicate key update
insert into table user(username) on duplicate key update age=age+1;
truncating table
删除整个表需要很长时间,因为MySQL需要逐行执行操作,删除表的所有行(保留表结构)的最快方法是使用truncate table 语句
truncating table 是MySQL中的DDL操作,也就是说一旦数据被清空,就不能回滚;
truncate table user;