1.查看所有数据库
show databases;
如图:
2.查看当前使用的数据库
select database();
如图:
3.查看数据库使用端口
show variables like 'port';
如图:
4.查看数据库编码
show variables like 'character%';
如图:
character_set_client 为客户端编码方式;
character_set_connection 为建立连接使用的编码;
character_set_database 为数据库的编码;
character_set_results 为结果集的编码;
character_set_server 为数据库服务器的编码;
只要保证以上采用的编码方式一样,就不会出现乱码问题。
5.查看数据库表信息,两种方式:
列出表的列信息
查看表结构信息:
查看数据表当时创建表的语句
修改表结构:
在表中添加列名:
alter table students add brithday datetime default '2011-11-11 11:11:11';
------修改表结构 alter add/modify/change
------修改表–添加字段
------alter table 表名 add 列名 类型/约束;
------生日信息
alter table students add brithday datetime default '2011-11-11 11:11:11';
----修改表–修改字段;不重命名版
----alter table 表名 modify 列名 类型及约束:
alter table students modify brithday date default '2011-11-11';
这条命令主要是把之前定义的datetime换掉了
修改表–修改字段:重命名版
alter table 表名 change 原列名 新列名 类型及约束;
alter table students change brithday brith date default '2011-11-11';
这条命令主要是修改原列名
—修改表–删除字段
alter table students drop brith;
代码:
tudents | CREATE TABLE `students` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(15) NOT NULL,
`age` tinyint(3) unsigned DEFAULT '0',
`high` decimal(5,2) DEFAULT '0.00',
`gender` enum('男','女','中性','保密') DEFAULT NULL,
`cls_id` int(10) unsigned NOT NULL,
`brith` date DEFAULT '2011-11-11',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
删除表
drop table students;
–数据增删改查
----insert [into] 表名 values (值1,值2,…)
----全列插入 值和表的字段的顺序一一对应
----占位符:只有主键字段才有占位符的概念 ,其表示为 0,default ,null
----全列插入在实际开发中用的不多,如果表结构一旦发生变化,全列插入就会报错
insert into students values (0,'小乔',18,180.00,'女',2);
刚才多敲了一个回车,弄成了两个数据,现在我们把它改删除一个
mysql> delete from students -> where id=1;
----指定列插入
----值何列一一对应
mysql> insert into students
-> (name,age,high,gender)
-> values
-> ('张飞',20,190.00,'女');
Query OK, 1 row affected, 1 warning (0.03 sec)
-----多行插入 批量插入
----insert into 表名(列1,…) values (值1,…),(值2,…)…
1)第一种方式:
mysql> insert into students
-> values
-> (0,'孙尚香',18,180.00,'女',2),
-> (1,'孙悟空',15,170.00,'男',2);
第二种方式:
代码:
mysql> insert into students
-> (name,age,high,gender)
-> values
-> ('八戒',20,175.00,'男'),
-> ('悟空',22,180.00,'男');
—修改update
----where 表示修改的范围
----update 表名set 列1=值1,列2=值2…[where 条件]
----没有where 进行条件限制就是 全表更新
代码:
mysql> update students set age=20;
加了where条件进行限制的
-----删除
–物理删除
–delect from tbname [where 条件判断]
delete from students where id =4
****************************************************************************************************************************************************完成的select语句
select distinct *
from 表名
where …
group by … having …
order by …
limit start,count
按顺序为:
from 表名
where…
group by
select distinct *#去重
having …
order by …
limit start,count
实际应用中,不会用到全部,只是其中的一部分