MySQL基础操作(一)

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)
create unique index index_name on table_name(column_list)

3、drop index

drop index index_name on table_name;

4、alter table table drop
alter table table_name drop index index_name;

alter table t1 modify id int unsigned not null;
alter table table_name drop primary key;

1.3 mysql视图

1、创建视图:

mysql>create view v_t1 as select * from t1 where id>4 and id<8;

2、view视图的帮助信息:

mysql>? view
alter view
create view
drop view

3、查看视图:

mysql>show tables;

4、删除视图v_t1:

mysql>drop view v_t1;

1.4 mysql内置函数

1、字符串函数:
concat(string[,...])                 //连接字符串
lcase(string)                        //转换成小写
ucase(string)                        //转换成大写
length(string)                       //string长度
ltrim(string)                        //去除前端空格
rtrim(string)                        //去除后端空格
repeat(string,count)                 //重复count次
replace(str,search_str,replace_str)  //在str中用replace_str替换search_str
substring(str,position,length)       //从str的position开始,取length个字符
space(count)   //生成count个空格

MariaDB [user]> select concat("hello","world") myname;
+------------+
| myname     |
+------------+
| helloworld |
+------------+
1 row in set (0.00 sec)

MariaDB [user]> select lcase("MYSQL");
+----------------+
| lcase("MYSQL") |
+----------------+
| mysql          |
+----------------+
1 row in set (0.00 sec)

MariaDB [user]> select ucase("mysql");
+----------------+
| ucase("mysql") |
+----------------+
| MYSQL          |
+----------------+
1 row in set (0.00 sec)

MariaDB [user]> select length('linux');
+-----------------+
| length('linux') |
+-----------------+
|               5 |
+-----------------+
1 row in set (0.00 sec)

MariaDB [user]> select ltrim("         hello world");
+-------------------------------+
| ltrim("         hello world") |
+-------------------------------+
| hello world                   |
+-------------------------------+
1 row in set (0.00 sec)

MariaDB [user]> select rtrim("hello world           ");
+---------------------------------+
| rtrim("hello world           ") |
+---------------------------------+
| hello world                     |
+---------------------------------+
1 row in set (0.00 sec)

MariaDB [user]> select rtrim("hello world          ! ");
+----------------------------------+
| rtrim("hello world          ! ") |
+----------------------------------+
| hello world          !           |
+----------------------------------+
1 row in set (0.00 sec)

MariaDB [user]> select repeat ('linux',3);
+--------------------+
| repeat ('linux',3) |
+--------------------+
| linuxlinuxlinux    |
+--------------------+
1 row in set (0.00 sec)

MariaDB [user]> select replace('linux is very good','linux','php');
+---------------------------------------------+
| replace('linux is very good','linux','php') |
+---------------------------------------------+
| php is very good                            |
+---------------------------------------------+
1 row in set (0.00 sec)

MariaDB [user]> select substr('linux is very good',1,4) as linux;
+-------+
| linux |
+-------+
| linu  |
+-------+
1 row in set (0.01 sec)

MariaDB [user]> select concat(space(20),'linux');
+---------------------------+
| concat(space(20),'linux') |
+---------------------------+
|                     linux |
+---------------------------+

      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间天数



你可能感兴趣的:(Mysql,mysql基础操作,mysql索引,mysql视图,mysql内置函数)