MySQL basic
Contents
MySQL basic
install
|
|
查看表结构
|
|
mysqldump
|
|
mysql GUI client for Linux
Jetbrain.Datagrip
jdbc url
|
|
MySQL heidisql 变量
这种变量语法只能用在 mysql 里, postgresql 不支持
|
|
配置文件位置
|
|
查端口
|
|
字段默认值
|
|
MySQL管理员用户名: root
密码安装MySQL时指定.
登录MySQL: MySQL -u root -p
退出后用wiloon登录, 然后show databases; 应该可以看到enlab了.
查看建表语句
|
|
查看版本
|
|
podmn
|
|
MySQL client, conn
|
|
|
|
查表字段名
|
|
|
|
insert
|
|
+——————-+——-+ | Variable_name | Value | +——————-+——-+
| Threads_cached | 58 |
| Threads_connected | 57 | ###这个数值指的是打开的连接数
| Threads_created | 3676 |
| Threads_running | 4 | ###这个数值指的是激活的连接数,这个数值一般远低于connected数值
+——————-+——-+
Threads_connected 跟show processlist结果相同,表示当前连接数。准确的来说,Threads_running是代表当前并发数
这是是查询数据库当前设置的最大连接数
MySQL> show variables like ‘%max_connections%';
+—————–+——-+
| Variable_name | Value |
+—————–+——-+
| max_connections | 1000 |
+—————–+——-+
可以在/etc/my.cnf里面设置数据库的最大连接数
[MySQLd]
max_connections = 1000
archlinux start MySQL service
select @@tx_isolation;
show full processlist;
systemctl start MySQLd.service
查看所有用户
use MySQL;
select * from user;
当前日期
select now() from dual;
set password
SET PASSWORD FOR user0@localhost= PASSWORD(“password”);
注意后面这句话 “COLLATE utf8_general_ci”,大致意思是在排序时根据utf8变码格式来排序
授权之后该用户才能用他自己的用户名密码访问MySQL.
MySQL-限制返回记录数limit
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
SELECT * FROM table order by time desc LIMIT n;
auto_increment
alter table tb_name modify id int auto_increment primary key;
export one table
MySQLdump -uroot -p DBName TableName> foo.sql
创建表:
create table tbl_ecp (
id int primary key not null,
english varchar(256) not null,
chinese varchar(512),
pronunciation varchar(256),
create_datetime datetime
);
drop table dbname table_name ;
drop table table_name ;
把字段 id 设成自增: auto_increment.
自增字段必须设成主键.
alter table tbl_log modify column id integer not null auto_increment, add primary key (id);
systemdate(), now()
sysdate是去读取系统的时间戳,now是读取数据库的时间戳
MySQL添加字段的方法并不复杂,下面将为您详细介绍MySQL添加字段和修改字段等操作的实现方法,希望对您学习MySQL添加字段方面会有所帮助。
1.登录数据库
MySQL -u root -p 数据库名称
2.查询所有数据表
show tables;
3.查询表的字段信息
desc 表名称;
4.1添加表字段
alter table table1 add transactor varchar(10) not Null;
alter table table1 add id int unsigned not Null auto_increment primary key
4.2.修改某个表的字段类型及指定为空或非空
-alter table 表名称 change 字段名称 字段名称 字段类型 [是否允许非空];
alter table tbl_user change password password varchar(256)
-alter table 表名称 modify 字段名称 字段类型 [是否允许非空];
alter table tbl_user modify deleted char(1) not null;
alter table 表名称 modify 字段名称 字段类型 [是否允许非空]; 4.3.修改某个表的字段名称及指定为空或非空
alter table 表名称 change 字段原名称 字段新名称 字段类型 [是否允许非空
4.4如果要删除某一字段,可用命令: ALTER TABLE mytable DROP 字段 名;
导出
MySQLdump -uwiloon -pPASSWORD -default-character-set=utf8 enlab >enlab.sql
导入
MySQL -uusername -ppassword db_name < db_name.sql
日期格式化函数date_format()
|
|
还可以用一个USE db_name语句启动文本文件。在这种情况下,不需要在命令行中指定数据库名:
shell> MySQL < text_file
如果正运行MySQL,可以使用source或.命令执行SQL脚本文件:
MySQL> source filename
查看MySQL版本
在MySQL中: MySQL> status;
eg:
[root@linuxtest test]# MySQL -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.30-community MySQL Community Server (GPL)
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
MySQL> CREATE DATABASE test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.06 sec)
MySQL> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| cacti |
| MySQL |
| test | |
+——————–+
5 rows in set (
索引相关常用命令:
- 创建主键
CREATE TABLE pk_tab2
(
id
int(11) NOT NULL AUTO_INCREMENT,
a1
varchar(45) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
创建唯一索引
create unique index indexname on tablename(columnname);
alter table tablename add unique index indexname(columnname);
- 创建单列一般索引
create index indexname on tablename(columnname);
alter table tablename add index indexname(columnname);
创建单列前缀索引
create index indexname on tablename(columnname(10)); //单列的前10个字符创建前缀索引
alter table tablename add index indexname(columnname(10)); //单列的前10个字符创建前缀索引
- 创建复合索引
create index indexname on tablename(columnname1,columnname2); //多列的复合索引
create index indexname on tablename(columnname1,columnname2(10)); //多列的包含前缀的复合索引
alter table tablename add index indexname(columnname1,columnname2); //多列的复合索引
alter table tablename add index indexname(columnname1,columnname(10)); //多列的包含前缀的复合索引
删除索引
drop index indexname on tablename;;
alter table tablename drop index indexname;
查看索引
show index from tablename;
show create table pk_tab2;
ClassNotFoundException: com.MySQL.jdbc.Driver
download and install connector/J , the JDBC driver for MySQL.
MySQL-connector-java-5.1.15-bin.jar
http://blog.sina.com.cn/s/blog_5dc960cd0100ea2h.html
http://database.51cto.com/art/201011/234549.htm
http://blog.csdn.net/myxx520/article/details/5130249
http://yh1022.iteye.com/blog/288693
http://blog.sina.com.cn/s/blog_5dc960cd0100ea2h.html
http://blog.csdn.net/flying_hawk/article/details/3498476
http://blog.sina.com.cn/s/blog_4d73c2c20100h8gp.html
http://bbs.csdn.net/topics/350006598
http://blog.chinaunix.net/uid-20382003-id-3022768.html
https://blog.csdn.net/weixin_40482816/article/details/87074689
MySQL 查看版本,version
MySQL -V
MySQL Ver 14.14 Distrib 5.5.32, for debian-linux-gnu (x86_64) using readline 6.2
MySQL函数
select version();
在MySQL中: MySQL> status
MySQL> status;
MySQL Ver 14.7 Distrib 4.1.10a, for redhat-linux-gnu (i686)Connection id: 416
SSL: Not in use
Current pager: stdout
Using outfile: "
Using delimiter: ;
Server version: 3.23.56-log
Protocol version: 10
Connection: Localhost via UNIX socket
Client characterset: latin1
Server characterset: latin1
UNIX socket: /tmp/MySQL_3311.sock
Uptime: 62 days 21 hours 21 min 57 secThreads: 1 Questions: 584402560 Slow queries: 424 Opens: 59664208 Flush tables: 1 Open tables: 64 Queries per second avg: 107.551
MySQL -help
MySQL –help | grep Distrib
MySQL Ver 14.7 Distrib 4.1.10a, for redhat-linux-gnu (i686)
MySQL删除指定数据库中的表
http://phpcode8.com/lamp/MySQL-lamp/MySQL-empty-tables.html
SELECT concat(‘DROP TABLE IF EXISTS ‘, table_name, ‘;')
FROM information_schema.tables
WHERE table_schema = ‘table0’;
Author -
LastMod 2011-04-15