标签:语法 root ash -- 创建数据库 进入 sele query roo
Mysql
这是一个关系型数据库,存在表的概念。
结构 数据库可以存放多张表,每个表可以存放多个字段,每个字段可以存放多个记录。
Dos命令操作数据库
Phpstudy使用终端打开数据库
第一步,打开phpstudy。在其他选项菜单点击Mysql工具。在其中打开Mysql命令行,
然后就可以输入了。他会出现Enter Password:
如果是第一次打开,默认密码为root,
对数据库进行增删查改
查看数据库的指令 :show databases;
;分号是数据库的结束符,没有加分号,即使按回车,也代表这个语句没有结束
mysql> show databases;
+-------------------------------+
|Database |
+-------------------------------+
|information_schema |
|mysql |
|performance_schema |
|test |
+-------------------------------+
创建数据库 create database数据库名;
mysql> create database d1;
Query OK,1 row affected (0.00sec)
Mysql>show databases;
+---------------------------------+
|Database |
+--------------------------------|
|information_schema |
|d1 |
|mysql |
|performance_schema |
|test |
+-------------------------------+
5 rows in set (0.00sec)
删除数据库的指令 drop database 数据库名;
mysql>drop database d1;
Query OK, 0 rows affected(0.20 sec)
Mysql> show databases;
+---------------------------------+
|Database |
+--------------------------------|
|information_schema |
|mysql |
|performance_schema |
|test |
+-------------------------------+
4 rows in set(0.00 sec);
进入某一个数据库中 use 数据库名;
mysql> use d2;
Database changed //进入数据库成功
对数据表增删查改
查看数据表 show tables;
mysql>show tables;
+-------------------------------+
|Tables_in_d2 |
+-------------------------------+
|t1 |
+-------------------------------+
创建表
Create table 表名(字段1类型,字段2类型)
mysql>create table t1(id int,name int)
Query OK, 0 rows affected (0.25 sec)
删除表
Drop table 表名
mysql>drop table t1;
Query OK, 0 rows affected(0.19 sec)
mysql>show tables;
Empty set (0.01 sec)
修改表名
Alter table 表名 rename 新表名
mysql>alter table t1 rename t3;
Query OK, 0 rows affected(0.01 sec)
mysql>show tables;
+-------------------------------+
|Tables_in_d2 |
+-------------------------------+
|t2 |
|t3 |
+-------------------------------+
进行表里对字段进行操作
查看表的定义 desc 表名;
mysql>desc t1;
+-------------+---------------+----------+———+-------------------+——————+
|Field | Type |Null | Key | Default |Extra |
+-------------+---------------+----------+———+-------------------+——————+
| id | int(11) |YES | | NULL |Extra |
| name | int(11) |YES | | NULL |Extra |
+-------------+---------------+----------+———+-------------------+——————+
添加字段
字段定义 字段名和字段类型 都要写
alter table 表名 add 字段名义
mysql>alter table t1 add age int;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates : 0 Warnings:0
mysql >desc t1;
+-------------+---------------+----------+———+-------------------+——————+
|Field | Type |Null | Key | Default |Extra |
+-------------+---------------+----------+———+-------------------+——————+
| id | int(11) |YES | | NULL |Extra |
| name | int(11) |YES | | NULL |Extra |
| age | int(11) |YES | | NULL |Extra |
+-------------+---------------+----------+———+-------------------+——————+
删除字段
Alter table 表名 drop 字段名;
mysql>alter table t1 drop age;
Query ok, 0 rows affected(0 07sec)
Records:0 Duplicates: 0 Warnings:0
mysql>desc t1;
+-------------+---------------+----------+———+-------------------+——————+
|Field | Type |Null | Key | Default |Extra |
+-------------+---------------+----------+———+-------------------+——————+
| id | int(11) |YES | | NULL |Extra |
| name | int(11) |YES | | NULL |Extra |
+-------------+---------------+----------+———+-------------------+——————+
修改字段
Alter table 表名 change 旧的字段名 字段定义;
mysql>alter table t1 change name age char;
Query OK, 0 rows affected (0.09 sec)
Records :0 Duplicates : 0 Warnings :0
mysql >desc t1;
+-------------+---------------+----------+———+-------------------+——————+
|Field | Type |Null | Key | Default |Extra |
+-------------+---------------+----------+———+-------------------+——————+
| id | int(11) |YES | | NULL |Extra |
| age | int( 1) |YES | | NULL |Extra |
+-------------+---------------+----------+———+-------------------+——————+
修改字段类型
mysql>alter table t1 modify age int;
Query OK, 0 rows affected (0.07 sec)
Records:0 Duplicates: 0 Warnings:0
mysql >desc t1;
+-------------+---------------+----------+———+-------------------+——————+
|Field | Type |Null | Key | Default |Extra |
+-------------+---------------+----------+———+-------------------+——————+
| id | int(11) |YES | | NULL |Extra |
| age | int(11) |YES | | NULL |Extra |
+-------------+---------------+----------+———+-------------------+——————+
2 rows in set (0.02 sec)
给字段添加数据(记录)
添加一条记录
Insert init 表名(id,age)value(值1,值2);
mysql >insert into t2(id,name) value(1,111);
Query OK, 1 row affected(0.03 sec)
mysql> insert into t2(id,name) value(2,221);
Query OK, 1 row affected (0.00 sec)
添加不指定字段名的语法
Insert into 表名 value(值1,值2);
mysql>insert into t2 values(4,441);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t2;
+-------+--------+
|id |name |
+-------+--------+
|1 | 111 |
|2 | 221 |
|3 | 331 |
|4 | 441 |
+-------+---------+
多条记录添加
insert into表名 value(值1,值2),(值1,值2),(值1,值2),(值1,值2);
mysql> insert into t3 values(1,20),(2,21),(3,26);
Query OK, 3 rows affected (0.00 sec)
Records:3 Duplicates: 0 Warnings:0
查看记录
select * from 表名; 查看所有的字段记录
select id from 表名; 查看单个的资源记录
select id,age from 表名; 查看多个字段的记录
mysql> select * from t3;
+-------------+-------------+
|id | age |
+-------------+-------------+
|1 | 20 |
|2 | 21 |
|3 |26 |
+--------------+-------------+
按条件查询
select * from 表名 where条件
条件表达式 > < >= <= = != and且 or 或
mysql >select * from t3 where age>30
+-------------+-------------+
|id | age |
+-------------+-------------+
|4 | 56 |
|5 | 62 |
|8 |54 |
|9 |34 |
|10 |36 |
+--------------+-------------+
5 rows in set(0.07 sec)
mysql>secect*from t3 where age>30 and age<50;
+-------------+-------------+
|id | age |
+-------------+-------------+
|9 | 34 |
|10 | 36 |
+-------------+-------------+
排序查询
select*from 表名 order by 字段名【asc/desc】
Asc 由低到高 desc 有高到低
+-------------+-------------+
|id | age |
+-------------+-------------+
|5 | 62 |
|4 | 56 |
|8 | 54 |
|10 | 36 |
|9 | 34 |
|7 |29 |
|3 |26 |
|6 |21 |
|2 |21 |
|1 |20 |
+--------------+-------------+
限制查询
select * from t3 limit 2,5
+-------------+-------------+
|id | age |
+-------------+-------------+
|3 | 26 |
|4 | 56 |
|5 | 62 |
|6 |21 |
|7 |29 |
+-------------+-------------+
mysql >select *from t3 limit 5;
+-------------+-------------+
|id | age |
+-------------+-------------+
|1 | 20 |
|2 | 21 |
|3 | 26 |
|4 |56 |
|5 |62 |
+-------------+-------------+
删除所有 删除记录
Delete from 表名
按条件删 delete from 表名 where 条件表达式
mysql> delete from t3 where id=3;
Query OK, 1 row affected (0.06 sec)
mysql > select * from t3;
+-------------+-------------+
|id | age |
+-------------+-------------+
|1 | 12 |
|2 | 12 |
|4 |12 |
|5 |62 |
|6 |56 |
+-------------+-------------+
改数据
Update 表名 set 字段=值
如果不带条件 会把字段下面的记录全改
mysql> update t3 set age=12;
Query OK, 10 rows affected (0.07 sec)
Rows matched : 10 Changed : 10 Warnings:
mysql >select * from t3;
+-------------+-------------+
|id | age |
+-------------+-------------+
|1 |12 |
|2 | 12 |
|3 | 12 |
|4 | 12 |
|5 | 12 |
|6 |12 |
|7 |12 |
|8 |12 |
|9 |12 |
|10 |12 |
+--------------+-------------+
按条件更新
mysql >update t3 set age=56 where id = 6;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed : 1 Warnings:0
mysql>select * from t3;
+-------------+-------------+
|id | age |
+-------------+-------------+
|1 |12 |
|2 | 12 |
|3 | 12 |
|4 | 12 |
|5 | 12 |
|6 |56 |
|7 |12 |
|8 |12 |
|9 |12 |
|10 |12 |
+--------------+-------------+
标签:语法 root ash -- 创建数据库 进入 sele query roo
原文地址:https://www.cnblogs.com/txf-123/p/10686063.html