MySQL-5.5之基础操作练习

装完了MySQL,接着就得熟悉一下操作命令。

1. 数据定义语句 DDL

create (database | table | index)

drop (database | table | index)

alter (database | table)

rename (table)

1.1 create

mysql> create database school;
Query OK, 1 row affected (0.00 sec)

mysql> show create database school;
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| school   | CREATE DATABASE `school` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> use school;
Database changed
mysql> 
mysql> create table student(
    -> `id` int(5) not null auto_increment,
    -> `name` char(20) not null,
    -> `sex` char(5) not null,
    -> `age` tinyint(2) not null default ‘0‘,
    -> primary key(id),
    -> key index_name(name));
Query OK, 0 rows affected (0.02 sec)

mysql> desc student;
+-------+------------+------+-----+---------+----------------+
| Field | Type       | Null | Key | Default | Extra          |
+-------+------------+------+-----+---------+----------------+
| id    | int(5)     | NO   | PRI | NULL    | auto_increment |
| name  | char(20)   | NO   | MUL | NULL    |                |
| sex   | char(5)    | NO   |     | NULL    |                |
| age   | tinyint(2) | NO   |     | NULL    |                |
+-------+------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> show create table student\G
*************************** 1. row ***************************
       Table: student
Create Table: CREATE TABLE `student` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `name` char(20) NOT NULL,
  `sex` char(5) NOT NULL,
  `age` tinyint(2) NOT NULL DEFAULT ‘0‘,
  PRIMARY KEY (`id`),
  KEY `index_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> create index index_age on student(age);
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;                          
+-------+------------+------+-----+---------+----------------+
| Field | Type       | Null | Key | Default | Extra          |
+-------+------------+------+-----+---------+----------------+
| id    | int(5)     | NO   | PRI | NULL    | auto_increment |
| name  | char(20)   | NO   | MUL | NULL    |                |
| sex   | char(5)    | NO   |     | NULL    |                |
| age   | tinyint(2) | NO   | MUL | NULL    |                |
+-------+------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

1.2 drop

mysql> use school;
Database changed
mysql> 
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
| test01           |
| test02           |
+------------------+
3 rows in set (0.00 sec)
mysql> 
mysql> drop table test01;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;      
+------------------+
| Tables_in_school |
+------------------+
| student          |
| test02           |
+------------------+
2 rows in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> drop database test;
Query OK, 0 rows affected (0.12 sec)

mysql> show databases;    
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
+--------------------+
4 rows in set (0.00 sec)

mysql> use school;
Database changed
mysql> 
mysql> desc student;
+-------+------------+------+-----+---------+----------------+
| Field | Type       | Null | Key | Default | Extra          |
+-------+------------+------+-----+---------+----------------+
| id    | int(5)     | NO   | PRI | NULL    | auto_increment |
| name  | char(20)   | NO   | MUL | NULL    |                |
| sex   | char(5)    | NO   |     | NULL    |                |
| age   | tinyint(2) | NO   | MUL | NULL    |                |
+-------+------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> drop index index_age on student;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;                   
+-------+------------+------+-----+---------+----------------+
| Field | Type       | Null | Key | Default | Extra          |
+-------+------------+------+-----+---------+----------------+
| id    | int(5)     | NO   | PRI | NULL    | auto_increment |
| name  | char(20)   | NO   | MUL | NULL    |                |
| sex   | char(5)    | NO   |     | NULL    |                |
| age   | tinyint(2) | NO   |     | NULL    |                |
+-------+------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

1.3 alter

mysql> show create database test;
+----------+---------------------------------------------------------------+
| Database | Create Database                                               |
+----------+---------------------------------------------------------------+
| test     | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> alter database test CHARACTER SET GBK COLLATE gbk_chinese_ci; # 改库的字符集
Query OK, 1 row affected (0.00 sec) 

mysql> show create database test;                                   
+----------+--------------------------------------------------------------+
| Database | Create Database                                              |
+----------+--------------------------------------------------------------+
| test     | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET gbk */ |
+----------+--------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show character set;
+----------+-----------------------------+---------------------+--------+
| Charset  | Description                 | Default collation   | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5     | Big5 Traditional Chinese    | big5_chinese_ci     |      2 |
| dec8     | DEC West European           | dec8_swedish_ci     |      1 |
| cp850    | DOS West European           | cp850_general_ci    |      1 |
| hp8      | HP West European            | hp8_english_ci      |      1 |
| koi8r    | KOI8-R Relcom Russian       | koi8r_general_ci    |      1 |
| latin1   | cp1252 West European        | latin1_swedish_ci   |      1 |
| latin2   | ISO 8859-2 Central European | latin2_general_ci   |      1 |
| gbk      | GBK Simplified Chinese      | gbk_chinese_ci      |      2 |
| latin5   | ISO 8859-9 Turkish          | latin5_turkish_ci   |      1 |
| armscii8 | ARMSCII-8 Armenian          | armscii8_general_ci |      1 |
| utf8     | UTF-8 Unicode               | utf8_general_ci     |      3 |

mysql> desc test01;
+-------+--------+------+-----+---------+-------+
| Field | Type   | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| id    | int(4) | NO   | PRI | 0       |       |
| fit   | int(5) | YES  | MUL | NULL    |       |
+-------+--------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> alter table test01 drop primary key;
Query OK, 0 rows affected (0.17 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test01;
+-------+--------+------+-----+---------+-------+
| Field | Type   | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| id    | int(4) | NO   |     | 0       |       |
| fit   | int(5) | YES  | MUL | NULL    |       |
+-------+--------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> alter table test01 drop index index_fit;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test01;
+-------+--------+------+-----+---------+-------+
| Field | Type   | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| id    | int(4) | NO   |     | 0       |       |
| fit   | int(5) | YES  |     | NULL    |       |
+-------+--------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> select * from student;
+----+--------+-----+-----+
| id | name   | sex | age |
+----+--------+-----+-----+
|  1 | 小东   | 男  |  13 |
|  2 | 小南   | 女  |  13 |
|  3 | 小北   | 男  |  13 |
|  4 | 小西   | 女  |  13 |
+----+--------+-----+-----+
4 rows in set (0.10 sec)

mysql> alter table student drop age;
Query OK, 4 rows affected (0.16 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> 
mysql> select * from student;                 
+----+--------+-----+
| id | name   | sex |
+----+--------+-----+
|  1 | 小东   | 男  |
|  2 | 小南   | 女  |
|  3 | 小北   | 男  |
|  4 | 小西   | 女  |
+----+--------+-----+
4 rows in set (0.00 sec)

mysql> alter table student add age tinyint(2) not null;
Query OK, 4 rows affected (0.10 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from student;                          
+----+--------+-----+-----+
| id | name   | sex | age |
+----+--------+-----+-----+
|  1 | 小东   | 男  |   0 |
|  2 | 小南   | 女  |   0 |
|  3 | 小北   | 男  |   0 |
|  4 | 小西   | 女  |   0 |
+----+--------+-----+-----+
4 rows in set (0.00 sec)

1.4 rename

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
+------------------+
1 row in set (0.00 sec)

mysql> rename table student to boy;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;                
+------------------+
| Tables_in_school |
+------------------+
| boy              |
+------------------+
1 row in set (0.00 sec)

2. 数据操作语句 DML

insert

select

update

delete

2.1 insert

mysql> insert into student(name,sex,age) values(‘小东‘,‘男‘,‘13‘),(‘小南‘,‘女‘,‘13‘);
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> 
mysql> select * from student;
+----+--------+-----+-----+
| id | name   | sex | age |
+----+--------+-----+-----+
|  1 | 小东   | 男  |  13 |
|  2 | 小南   | 女  |  13 |
+----+--------+-----+-----+
2 rows in set (0.00 sec)

mysql> insert into student values(3,‘小北‘,‘男‘,‘13‘),(4,‘小西‘,‘女‘,‘13‘);      
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from student;
+----+--------+-----+-----+
| id | name   | sex | age |
+----+--------+-----+-----+
|  1 | 小东   | 男  |  13 |
|  2 | 小南   | 女  |  13 |
|  3 | 小北   | 男  |  13 |
|  4 | 小西   | 女  |  13 |
+----+--------+-----+-----+
4 rows in set (0.00 sec)

2.2 select

mysql> select * from student;                        
+----+--------+-----+-----+
| id | name   | sex | age |
+----+--------+-----+-----+
|  1 | 小东   | 男  |   0 |
|  2 | 小南   | 女  |   0 |
|  3 | 小北   | 女  |  12 |
|  4 | 小西   | 女  |  13 |
+----+--------+-----+-----+
4 rows in set (0.00 sec)

mysql> select * from student where name=‘小北‘;
+----+--------+-----+-----+
| id | name   | sex | age |
+----+--------+-----+-----+
|  3 | 小北   | 女  |  12 |
+----+--------+-----+-----+
1 row in set (0.00 sec)

mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)

mysql> select user,host from mysql.user where user=‘root‘ and host=‘localhost‘;
+------+-----------+
| user | host      |
+------+-----------+
| root | localhost |
+------+-----------+
1 row in set (0.00 sec)

2.3 update

mysql> update student set age=13 where id=4;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from student;
+----+--------+-----+-----+
| id | name   | sex | age |
+----+--------+-----+-----+
|  1 | 小东   | 男  |   0 |
|  2 | 小南   | 女  |   0 |
|  3 | 小北   | 男  |   0 |
|  4 | 小西   | 女  |  13 |
+----+--------+-----+-----+
4 rows in set (0.00 sec)
mysql> update student set sex=‘女‘,age=12 where id=3;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from student;                        
+----+--------+-----+-----+
| id | name   | sex | age |
+----+--------+-----+-----+
|  1 | 小东   | 男  |   0 |
|  2 | 小南   | 女  |   0 |
|  3 | 小北   | 女  |  12 |
|  4 | 小西   | 女  |  13 |
+----+--------+-----+-----+
4 rows in set (0.00 sec)

2.4 delete

mysql> select * from student;                        
+----+--------+-----+-----+
| id | name   | sex | age |
+----+--------+-----+-----+
|  1 | 小东   | 男  |   0 |
|  2 | 小南   | 女  |   0 |
|  3 | 小北   | 女  |  12 |
|  4 | 小西   | 女  |  13 |
+----+--------+-----+-----+
4 rows in set (0.00 sec)

mysql> delete from student where id=4;
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;                  
+----+--------+-----+-----+
| id | name   | sex | age |
+----+--------+-----+-----+
|  1 | 小东   | 男  |   0 |
|  2 | 小南   | 女  |   0 |
|  3 | 小北   | 女  |  12 |
+----+--------+-----+-----+
3 rows in set (0.00 sec)

mysql> delete from student where name=‘小南‘;
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;                
+----+--------+-----+-----+
| id | name   | sex | age |
+----+--------+-----+-----+
|  1 | 小东   | 男  |   0 |
|  3 | 小北   | 女  |  12 |
+----+--------+-----+-----+
2 rows in set (0.00 sec)

3. 数据库管理语句

show

create user

grant

revoke

3.1 show

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
+--------------------+
4 rows in set (0.00 sec)

mysql> use school;
Database changed
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
+------------------+
1 row in set (0.00 sec)

mysql> show create database school;
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| school   | CREATE DATABASE `school` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show create table student\G
*************************** 1. row ***************************
       Table: student
Create Table: CREATE TABLE `student` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `name` char(20) NOT NULL,
  `sex` char(5) NOT NULL,
  `age` tinyint(2) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `index_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)

mysql> show grants for [email protected];  
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected]                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*23AE809DDACAF96AF0FD78ED04B6A265E05AA257‘ WITH GRANT OPTION |
| GRANT PROXY ON ‘‘@‘‘ TO ‘root‘@‘localhost‘ WITH GRANT OPTION                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

3.2 create user

mysql> select user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | 127.0.0.1 |                                           |
+------+-----------+-------------------------------------------+
2 rows in set (0.00 sec)

mysql> 
mysql> create user [email protected]‘192.168.0.%‘ identified by ‘123‘;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,password from mysql.user;          
+-------+-------------+-------------------------------------------+
| user  | host        | password                                  |
+-------+-------------+-------------------------------------------+
| root  | localhost   | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root  | 127.0.0.1   |                                           |
| logen | 192.168.0.% | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+-------+-------------+-------------------------------------------+
3 rows in set (0.00 sec)

mysql> select user,host,password from mysql.user;
+-------+-------------+-------------------------------------------+
| user  | host        | password                                  |
+-------+-------------+-------------------------------------------+
| root  | localhost   | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root  | 127.0.0.1   |                                           |
| logen | 192.168.0.% | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+-------+-------------+-------------------------------------------+
3 rows in set (0.00 sec)

mysql> drop user [email protected]‘192.168.0.%‘;            
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | 127.0.0.1 |                                           |
+------+-----------+-------------------------------------------+
2 rows in set (0.00 sec)

3.3 grant

mysql> grant insert,delete,update,select on school.student to [email protected]‘192.168.0.%‘ identified by ‘123‘;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for [email protected]‘192.168.0.%‘;                                      +----------------------------------------------------------------------------------------------------------------+
| Grants for [email protected]%                                                                                   |
+----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘logen‘@‘192.168.0.%‘ IDENTIFIED BY PASSWORD ‘*23AE809DDACAF96AF0FD78ED04B6A265E05AA257‘ |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `school`.`student` TO ‘logen‘@‘192.168.0.%‘                            |
+----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

[[email protected] ~]# mysql -h192.168.0.88 -ulogen -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.55-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| school             |
+--------------------+
2 rows in set (0.00 sec)

mysql> use school;
Database changed
mysql> 
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student          |
+------------------+
1 row in set (0.00 sec)

mysql> 
mysql> select * from student;
+----+--------+-----+-----+
| id | name   | sex | age |
+----+--------+-----+-----+
|  1 | 小东   | 男  |   0 |
|  3 | 小北   | 女  |  12 |
+----+--------+-----+-----+
2 rows in set (0.00 sec)

3.4 revoke

mysql> revoke all on school.student from [email protected]‘192.168.0.%‘;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for [email protected]‘192.168.0.%‘;                  
+----------------------------------------------------------------------------------------------------------------+
| Grants for [email protected]%                                                                                   |
+----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘logen‘@‘192.168.0.%‘ IDENTIFIED BY PASSWORD ‘*23AE809DDACAF96AF0FD78ED04B6A265E05AA257‘ |
+----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

[[email protected] ~]# mysql -h192.168.0.88 -ulogen -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.55-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
时间: 2024-10-12 17:55:56

MySQL-5.5之基础操作练习的相关文章

mysql 5.5.39 基础操作01

1.mysql版本应该怎么选择 由于5.5-5.6对mysql性能做了较大优化,所以一般选择mysql 5.5 或者 5.6 mysql 5.5.xx -5.6.xx 产品的特殊性,所以编译方式也和早期的产品安装方式不同,采用cmake或者gmake方式进行编译安装.即 ./cmake ; make ; make install ,生产场景的具体命令及参数为详见mysql 一键安装脚本 一键安装脚本下载链接 http://down.51cto.com/data/2228998 2.设置mysql

MySQL学习笔记——1.基础操作

1.命令行使用 f7 查看历史命令 \c 退出执行 1064 语法错误 1046 未选择数据库 1366 数据中有汉字,但未设定字符集 tee D:/abc.sql 可将输入的命令形成sql文件进行保存 set names 编码 设置字符集(命令行默认编码为gbk) 2.入门语句 连接MySQL mysql -uroot -p*** 查看现有库 show databases; 创建库 create database 库名 charset utf8; 删除库 drop database 库名; 选

Mysql安装和基础操作

1.环境检查: 先检查是否已经安装了:rpm -qa |grep mysql ---两个都检查下,查看mysql是否安装 rpm -qa |grep MySQL若安装有可删除:rpm -e ****** 2.安装:1)安装前准备:mkdir usr/mysql cd进入该目录,上传安装包到/usr/mysql目录下2)安装:rpm -ivh MySQL-server-5.0.16-0.i386.rpm rpm -ivh MySQL-elient-5.0.16-0.i386.rpm3)验证安装是否

mysql的基础操作指令整理|环境redhat6

1.安装 yum-y install mysql mysql-server service mysqld start|stop|restart        ##启动|停止|重启 chkconfig mysqld on|off                  ##开机启动|关闭 此时输入:mysql就能启动进入(记得是start状态) 2.改密 mysqladmin –uroot password             ##创建密码 mysqladmin–uroot –pxxoo passw

mysql基础操作、sql技巧和sql的常见优化

一.常见操作 1.复制表结构create table t2 like t1 复制表数据insert into t2 select * from t1 2.mysql索引 alter table用来创建普通索引.unique索引或primary key索引 alter table t add index index_name(column_list) alter table t add unique(column_list) alter table t add primary key(column

MYSQL 基础操作

1.MySQL基础操作 一:MySQL基础操作 1:MySQL表复制 复制表结构 + 复制表数据 create table t3 like t1; --创建一个和t1一样的表,用like(表结构也一样) insert into t3 select * from t1; --t1的数据全部拿过来,注意是表结构一致才select* ,否则选择相应的的字段列插入 create table t1( id int unsigned not null auto_increment primary key,

PHP - PDO 之 mysql 基础操作

<?php /* pdo 学习 */ $dsn = 'mysql:host=localhost;dbname=cswl';//构建连接dsn $db = new pdo($dsn,'root','');//实例化pdo[第二个参数是用户名,第三个为密码] #.查询 $obj = $db->prepare("show tables;");//准备预处理sql语句 $obj->execute();//执行预处理语句 $obj->fetchALL(PDO::FETCH

着重基础之—MySql 不能遗忘的索引操作

着重基础之—MySql 不能遗忘的索引操作 关于MySql索引的基础知识我就不在这里写了,我不太想当信息的搬运工. 技巧分享:Workbench 作为一款专为MySQL设计的ER/数据库建模工具.除了管理数据库外,其实也是一款Sql语句生成利器.合理利用,将带来事半功倍的效果.当然,前提是我们对基础知识的了解. 我先来整理一些索引操作的Sql语句,之所以整理,起因是我遇到问题时,在百度里查到的回复,基本上都是错的,需要有人来纠正. 1.多主键(PRIMARY)删除 假设场景:你在表里定义了多个主

Mysql常用基础操作(备忘录)

常常忘记mysql的一些命令行操作,甚至于说,比较复杂的sql格式记不住或忘记了,也可能根本不会考虑去记,因此,做一下汇总,当下次出现恍惚时不至于去百度挨个找,有时就是记不起来,但是只要给点药引子,立马会想起来. 一.mysql基础操作 mysql命令规则:每行sql后边都跟个分号表示一个命令语句结束 1.登录本地mysql数据库 $ mysql -u root -p$ password 2.更改root密码 mysql>mysqladmin -uroot password 'yourpassw

MySQL 之基础操作及增删改查等

一:MySQL基础操作 使用方法: 方式一: 通过图型界面工具,如 Navicat,DBeaver等 方式二: 通过在命令行敲命令来操作 SQL ( Structure query language ) 结构化查询语言 SQL语言分为4个部分:DDL(定义).DML(操作).DQL(查询).DCL(控制) 1.DDL语句 数据库定义语言: 数据库.表.视图.索引.存储过程,例如CREATE DROP ALTER 2.DCL语句 数据库控制语言: 例如控制用户的访问权限GRANT.REVOKE 3