Mysql 基础语法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql 基础语法相关的知识,希望对你有一定的参考价值。
数据库基本语法: DDL:Data Defination Lanuage: 数据定义语言 CREATE, ALTER, DROP DML:Data Manapulate Language: 数据操作语言 INSERT, REPLACE, UPDATE, DELETE DCL:Data Control Language: 数据控制语言 GRANT, REVOKE 常用命令: mysql命令不区分大小写 这些是基本常识 DDL CREATE ALTER DROP DML 操纵语言 INSERT UPDATE DELETE DCL 控制语言 GRANT REVOKE 第一篇 DDL 库和表 create alter drop mysql> create database mydb; Query OK, 1 row affected (0.01 sec) mysql> use mydb; Database changed mysql> show tables; Empty set (0.00 sec) mysql> create table students (id char(20),name char(20),age char(10),score char(10)); 建表并添加4个字段和字段的类型 Query OK, 0 rows affected (0.10 sec) mysql> show tables; +----------------+ | Tables_in_mydb | +----------------+ | students | +----------------+ 1 row in set (0.00 sec) mysql> create table teachers as select * from students; 建立教师表,字段同学生表 Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show tables; +----------------+ | Tables_in_mydb | +----------------+ | students | | teachers | +----------------+ 2 rows in set (0.00 sec) mysql> desc students; mysql> desc teachers; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | char(20) | YES | | NULL | | | name | char(20) | YES | | NULL | | | age | char(10) | YES | | NULL | | | score | char(10) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ mysql> alter table teachers add subject varchar(100); 添加字段 Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc teachers; +---------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+--------------+------+-----+---------+-------+ | id | char(20) | YES | | NULL | | | name | char(20) | YES | | NULL | | | age | char(10) | YES | | NULL | | | score | char(10) | YES | | NULL | | | subject | varchar(100) | YES | | NULL | | +---------+--------------+------+-----+---------+-------+ mysql> alter table teachers drop score; 删除字段 Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc teachers; +---------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+--------------+------+-----+---------+-------+ | id | char(20) | YES | | NULL | | | name | char(20) | YES | | NULL | | | age | char(10) | YES | | NULL | | | subject | varchar(100) | YES | | NULL | | +---------+--------------+------+-----+---------+-------+ mysql> alter table teachers change id teacherid char(30); 修改字段名,用change Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc teachers; +-----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+-------+ | teacherid | char(30) | YES | | NULL | | | name | char(20) | YES | | NULL | | | age | char(10) | YES | | NULL | | | subject | varchar(100) | YES | | NULL | | +-----------+--------------+------+-----+---------+-------+ mysql> alter table teachers modify teacherid varchar(5); 修改字段属性,用modify Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc teachers; +-----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+-------+ | teacherid | varchar(5) | YES | | NULL | | | name | char(20) | YES | | NULL | | | age | char(10) | YES | | NULL | | | subject | varchar(100) | YES | | NULL | | +-----------+--------------+------+-----+---------+-------+ mysql> drop table teachers; 删除表,drop table table_name Query OK, 0 rows affected (0.00 sec) 总结1: 查看库中的表:SHOW TABLES FROM db_name; 查看表的结构:DESC tb_name; 删除表:DROP TABLE tb_name; 修改表: ALTER TABLE tb_name MODIFY 改字段属性 CHANGE 改字段名称 ADD 添加字段 DROP 删除字段 help create table获取帮助 help alter table 第二篇 DML 数据修改 insert update delete ; select mysql> insert into students (id,name,age,score) value(‘1‘,‘wxtan‘,‘18‘,‘99‘),(‘2‘,‘yull‘,‘17‘,‘80‘); 增加表内数据,insert into Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from students; +------+-------+------+-------+ | id | name | age | score | +------+-------+------+-------+ | 1 | wxtan | 18 | 99 | | 2 | yull | 17 | 80 | +------+-------+------+-------+ mysql> insert into students value(‘3‘,‘AA‘,‘30‘,‘60‘); (如果全部字段都添加可以不用写) Query OK, 1 row affected (0.00 sec) mysql> select * from students; +------+-------+------+-------+ | id | name | age | score | +------+-------+------+-------+ | 1 | wxtan | 18 | 99 | | 2 | yull | 17 | 80 | | 3 | AA | 30 | 60 | +------+-------+------+-------+ mysql> update students set age=14 where id=1; 修改表内数据,update table_name Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from students; +------+-------+------+-------+ | id | name | age | score | +------+-------+------+-------+ | 1 | wxtan | 14 | 99 | | 2 | yull | 17 | 80 | | 3 | AA | 30 | 60 | +------+-------+------+-------+ 3 rows in set (0.00 sec) mysql> update students set age=‘28‘ where name=‘yull‘; 修改表内数据,update table_name Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from students; +------+-------+------+-------+ | id | name | age | score | +------+-------+------+-------+ | 1 | wxtan | 14 | 99 | | 2 | yull | 28 | 80 | | 3 | AA | 30 | 60 | +------+-------+------+-------+ 3 rows in set (0.00 sec) mysql> select * from students; +------+-------+------+-------+ | id | name | age | score | +------+-------+------+-------+ | 1 | wxtan | 14 | 99 | | 2 | yull | 28 | 80 | | 5 | bb | 44 | 55 | | 6 | cc | 44 | 60 | +------+-------+------+-------+ 4 rows in set (0.00 sec) mysql> delete from students where age=‘44‘; 删除表内数据,delete from Query OK, 2 rows affected (0.00 sec) mysql> select * from students; +------+-------+------+-------+ | id | name | age | score | +------+-------+------+-------+ | 1 | wxtan | 14 | 99 | | 2 | yull | 28 | 80 | +------+-------+------+-------+ mysql> delete from students where age=‘14‘ or score=‘80‘; 可以用or and等条件表达式 Query OK, 2 rows affected (0.00 sec) mysql> select * from students; Empty set (0.00 sec) mysql> select * from students; 数据查询,select from +------+--------+------+-------+ | id | name | age | score | +------+--------+------+-------+ | 1 | apple | 20 | 88 | | 3 | xigua | 10 | 50 | | 2 | banana | 30 | 68 | +------+--------+------+-------+ 3 rows in set (0.00 sec) mysql> select id,name from students; +------+--------+ | id | name | +------+--------+ | 1 | apple | | 3 | xigua | | 2 | banana | +------+--------+ 3 rows in set (0.00 sec) mysql> select id,name from students where age=20; +------+-------+ | id | name | +------+-------+ | 1 | apple | +------+-------+ 1 row in set (0.00 sec) 第三篇 DCL 权限 grant revoke;show grants for mysql> grant select,insert,update on mydb.students to ‘wxtan2‘@‘192.168.8.%‘ identified by ‘123‘; 给权限到某表,授予某账户(账户不存在则创建) mysql> flush privileges; 刷新权限 [[email protected] mysql]# mysql -uwxtan2 -p123 -h192.168.8.104 从192.168.8.%进入数据库 Welcome to the MariaDB monitor. MySQL [mydb]> select * from students; +------+--------+------+-------+ | id | name | age | score | +------+--------+------+-------+ | 1 | apple | 20 | 88 | | 3 | xigua | 10 | 50 | | 2 | banana | 30 | 68 | +------+--------+------+-------+ 3 rows in set (0.00 sec) MySQL [mydb]> insert into students value(‘4‘,‘guapi‘,‘40‘,‘50‘); 有insert权限 Query OK, 1 row affected (0.02 sec) MySQL [mydb]> update students set name=‘apple222‘ where id=‘1‘; 有update权限 Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 MySQL [mydb]> select * from students; +------+----------+------+-------+ | id | name | age | score | +------+----------+------+-------+ | 1 | apple222 | 20 | 88 | | 3 | xigua | 10 | 50 | | 2 | banana | 30 | 68 | | 4 | guapi | 40 | 50 | +------+----------+------+-------+ 4 rows in set (0.00 sec) MySQL [mydb]> delete from students where name=‘banana‘; 没有delete权限 ERROR 1142 (42000): DELETE command denied to user ‘wxtan2‘@‘192.168.8.101‘ for table ‘students‘ mysql> revoke update on mydb.students from ‘wxtan2‘@‘192.168.8.%‘; 回收update权限 mysql> flush privileges; MySQL [mydb]> update students set name=‘apple545‘ where id=‘1‘; 发现无法执行update命令 ERROR 1142 (42000): UPDATE command denied to user ‘wxtan2‘@‘192.168.8.101‘ for table ‘students‘ MySQL [mydb]> insert into students value(‘6‘,‘guapi‘,‘40‘,‘50‘); insert命令不受影响 Query OK, 1 row affected (0.00 sec) MySQL [mydb]> select * from students; select命令不受影响 +------+----------+------+-------+ | id | name | age | score | +------+----------+------+-------+ | 1 | apple555 | 20 | 88 | | 3 | xigua | 10 | 50 | | 2 | banana | 30 | 68 | | 4 | guapi | 40 | 50 | | 6 | guapi | 40 | 50 | +------+----------+------+-------+ mysql> show grants for ‘wxtan2‘@‘192.168.8.%‘; 查询用户的权限命令;usage表示只有使用权 +-----------------------------------------------------------------------------------------------------------------+ | Grants for [email protected]% | +-----------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO ‘wxtan2‘@‘192.168.8.%‘ IDENTIFIED BY PASSWORD ‘*23AE809DDACAF96AF0FD78ED04B6A265E05AA257‘ | | GRANT SELECT, INSERT ON `mydb`.`students` TO ‘wxtan2‘@‘192.168.8.%‘ | +-----------------------------------------------------------------------------------------------------------------+ mysql> grant all privileges on mydb.students to ‘wxtan2‘@‘192.168.8.%‘ identified by ‘123456‘; 给所有权限,并修改密码 Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) 第四篇 修改数据库默认密码 /etc/init.d/mysql stop (service mysqld stop ) /usr/bin/mysqld_safe --skip-grant-tables 另外开个SSH连接 [[email protected] ~]# mysql mysql>use mysql; mysql>update user set password=password("123456") where user="root"; mysql>flush privileges; mysql>exit CTRL+Z 终止进程 [[email protected] ~]# jobs [1]+ Stopped mysqld_safe --skip-grant-tables [[email protected] ~]# kill %1 [[email protected] ~]# service mysqld start Starting MySQL SUCCESS! 方法1: 用SET PASSWORD命令 首先登录MySQL。 格式:mysql> set password for 用户名@localhost = password(‘新密码‘); 例子:mysql> set password for [email protected] = password(‘123‘); 方法2:用mysqladmin 格式:mysqladmin -u用户名 -p旧密码 password 新密码 例子:mysqladmin -uroot -p123456 password 123 方法3:用UPDATE直接编辑user表 首先登录MySQL。 mysql> use mysql; mysql> update user set password=password(‘123‘) where user=‘root‘ and host=‘localhost‘; mysql> flush privileges; 方法4:在忘记root密码的时候,可以这样 以windows为例: 1. 关闭正在运行的MySQL服务。 2. 打开DOS窗口,转到mysql\bin目录。 3. 输入mysqld --skip-grant-tables 回车。--skip-grant-tables 的意思是启动MySQL服务的时候跳过权限表认证。 4. 再开一个DOS窗口(因为刚才那个DOS窗口已经不能动了),转到mysql\bin目录。 5. 输入mysql回车,如果成功,将出现MySQL提示符 >。 6. 连接权限数据库: use mysql; 。 6. 改密码:update user set password=password("123") where user="root";(别忘了最后加分号) 。 7. 刷新权限(必须步骤):flush privileges; 。 8. 退出 quit。 9. 注销系统,再进入,使用用户名root和刚才设置的新密码123登录。
以上是关于Mysql 基础语法的主要内容,如果未能解决你的问题,请参考以下文章