数据库的基本操作
-
创建数据库
mysql> create database my2020715
-
删除数据库
mysql> drop database my2020715;
-
选择数据库
mysql> create database my2020715;
mysql> use my2020715;
- 创建表
-
查看表
mysql> show tables;
mysql> desc runoob_tbl;
-
删除表
mysql> drop table runoob_tbl;对表数据的增,删,改, 查,排序:
向表内插入数据: insert into 表名(字段) values (字段对应的值);
mysql> insert into runoob_tbl(runoob_title,runoob_author,submission_date) values (“by”,“ppy”,now());
now() 函数获取ubuntu 的系统日期。查看表的内容: select * from 表名
mysql> select * from runoob_tbl;更新表的数据: update 表名 set 字段 = 新值 where 主键(对应的字段) = 键值;
mysql> update runoob_tbl set runoob_author = “kitty” where runoob_id = 1002;表数据的删除: delete from 表名 where 主键(对应的字段) = 键值;
mysql> delete from runoob_tbl where runoob_id = 1001;查看记录: select * from 表名 where 字段 like ‘字段结尾的相同元素’;
mysql> select * from runoob_tbl where runoob_author like ‘%y’; -
对表数据的排序:select * from 表名 order by 字段 desc(降序)/asc(升序)
mysql> select * from runoob_tbl order by runoob_author desc;
- MySQL 里面的alter指令
删除表字段: alter table 表名 drop 字段;
mysql> alter table table_1 drop i;
添加表字段: alter table 表名 add 字段 字段属性
mysql> alter table table_1 add i int;
修改表字段:
属性:alter table 表名 modify 字段(已有字段) 属性
mysql> alter table table_1 modify c char(10);
字段名:alter table 表名 change 字段(已有) 字段 属性
mysql> alter table table_1 change i j bigint;
-
Mysql 里面两个表的链接
Inner join : 两个表之间的内部链接:select a.表1字段,a.表1 字段,b.表2字段 from 表1 a inner join 表2 b a.表1字段 = b. 表2字段;
mysql> select a.c ,a.j ,b.b from table_1 a inner join table_2 b on a.c = b.c;
mysql> SELECT a.c, a.j, b.b FROM table_1 a, table_2 b WHERE a.c = b.c;
Left join:两个表的左链接:
mysql> select a.c ,a.j ,b.b from table_1 a left join table_2 b on a.c = b.c;
Right join :两个表之间的右链接:
mysql> select a.c ,a.j ,b.b from table_1 a right join table_2 b on a.c = b.c;