MySQL基础操作(一):
1.1 mysql表复制
1.2 mysql索引
1.3 mysql视图
1.4 mysql内置函数
1.1 mysql表复制
复制表结构+复制表数据
create table t2 like t1;
insert into t2 select * from t1;
MariaDB [user]> create table t1(
-> id int unsigned auto_increment,
-> name varchar(30) not null,
-> primary key(id));
Query OK, 0 rows affected (0.00 sec)
MariaDB [user]> show tables;
+----------------+
| Tables_in_user |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
MariaDB [user]> desc t1;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | | NULL | |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
MariaDB [user]> insert into t2 select * from t1;
ERROR 1146 (42S02): Table 'user.t2' doesn't exist
MariaDB [user]> show tables;
+----------------+
| Tables_in_user |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
MariaDB [user]> insert into t1(name) values('user1');
Query OK, 1 row affected (0.00 sec)
MariaDB [user]> insert into t1(name) values('user2');
Query OK, 1 row affected (0.00 sec)
MariaDB [user]> select * from t1;
+----+-------+
| id | name |
+----+-------+
| 1 | user1 |
| 2 | user2 |
+----+-------+
2 rows in set (0.00 sec)
MariaDB [user]> create table t2 like t1;
Query OK, 0 rows affected (0.00 sec)
MariaDB [user]> show tables;
+----------------+
| Tables_in_user |
+----------------+
| t1 |
| t2 |
+----------------+
2 rows in set (0.00 sec)
MariaDB [user]> desc t2;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | | NULL | |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
MariaDB [user]> insert into t2 select * from t1;
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [user]> select * from t2;
+----+-------+
| id | name |
+----+-------+
| 1 | user1 |
| 2 | user2 |
+----+-------+
2 rows in set (0.00 sec)
1.2 mysql索引
1、alter table用来创建普通索引、unique索引或primary key索引
alter table table_name add index index_name(column_list)
alter table table_name add unique(column_list)
alter table table_name add primary key(column_list);
alter table table_name modify id int auto_increment;
2、create index
create table index index_name on table_name(column_list)3、drop index
drop index index_name on table_name;1、创建视图:
mysql>create view v_t1 as select * from t1 where id>4 and id<8;2、view视图的帮助信息:
mysql>? view3、查看视图:
mysql>show tables;4、删除视图v_t1:
mysql>drop view v_t1;1 row in set (0.00 sec)
2、数学函数:
bin(decimal_number) //十进制转二进制
chiling(number) //向上取整
floor(number) //向下取整
max(col) //取最大值,聚合时使用
min(col) //取最小值,聚合时使用
sqrt(num) //开平方
rand() //返回0-1内的随机数s
3、日期函数
curdate() //返回当前日期
curtime() //返回当前时间
now() //返回当前的日期时间
unix_timestamp(date) //返回当前date的unix时间戳
from_unixtime() //返回unix时间戳的日期指
week(date) //返回日期date为一年中的第几周
year(date) //返回日期date的年份
datediff(expr,expr2) //返回起始时间expr和结束时间expr2间天数