MySQL数据库管理
(1)DDL:数据定义语言
(2)DML:数据操纵语言
(3)DQL:数据查询语言
(4)DCL:数据控制语言
(1)创建数据库:creste databaes 数据库名
(2)创建数据表:create table 表名 (字段定义……)
(1)删除指定的数据表:drop table [数据库名]表名
(2)删除指定的数据库:drop database 数据库名
(1)insert:插入新数据
(2)update:更新原有数据
(3)delete:删除不需要的数据
insert into 表名(字段1,字段2, .....) values(字段1的值,字段的值, .....)
4、修改、更新数据表P F的数据记录update 表名 set 字段名1=值1[,字段名2=值2] where 条件表达式
5、在数据表中删除指定的数据记录(1)delete from 表名 where 条件表达式
(2)不带where条件的语句表示删除表中所有记录(谨慎操作)
selext 字段名1,字段名2..... from 表名;
4、查询时指定条件select 字段名1,字段名2.... from 表名 where 条件表达式;
DCL语句操作1、设置用户权限(用户不存在时,则新建用户)GRANT 权限列表 ON 数据库名.表名 TO 用户名@来源地址 [ IDENTIFIED BY‘密码’ ]
2、查看用户的权限SHOW GRANTS FOR 用户名@来源地址
3、撤销用户的权限REVOKE 权限列表 ON 数据库名.表名 FROM 用户名@来源地址
操作实例1、查看数据库列表信息mysql> show databases; //查看数据库列表信息+--------------------+| Database |+--------------------+| information_schema || mysql | //其中mysql为系统数据库| performance_schema || sys |+--------------------+4 rows in set (0.02 sec)
2、创建数据库
mysql> create database school; //创建数据库schoolQuery OK, 1 row affected (0.02 sec)mysql> show databases; //查看数据库列表信息+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || school | //成功创建数据库| sys |+--------------------+5 rows in set (0.00 sec)mysql>
3、创建表
mysql> mysql> use school; //使用数据库schoolDatabase changedmysql> show tables;Empty set (0.00 sec)mysql> create table info ( -> id int(4) not null, -> name char(10) not null, -> address varchar(50) default 'beijing', -> score decimal, -> primary key(id)); //创建表infoQuery OK, 0 rows affected (0.01 sec)mysql> describe info; //查看表结构+---------+---------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+---------+---------------+------+-----+---------+-------+| id | int(4) | NO | PRI | NULL | || name | char(10) | NO | | NULL | || address | varchar(50) | YES | | beijing | || score | decimal(10,0) | YES | | NULL | |+---------+---------------+------+-----+---------+-------+4 rows in set (0.03 sec)
4、表中添加数据与查看表中数据
mysql> mysql> insert into info (id,name,address,score) values (1,'stu01','shanghai',88); //添加数据Query OK, 1 row affected (0.03 sec)mysql> insert into info (id,name,address,score) values (2,'stu02','nanjing',79); //添加数据Query OK, 1 row affected (0.01 sec)mysql> insert into info (id,name,address,score) values (3,'stu03',default,90); //添加数据Query OK, 1 row affected (0.00 sec)mysql> insert into info (id,name,address,score) values (4,'stu04','',60); //添加数据Query OK, 1 row affected (0.00 sec)mysql> select * from info; //查看info表中数据+----+-------+----------+-------+| id | name | address | score |+----+-------+----------+-------+| 1 | stu01 | shanghai | 88 || 2 | stu02 | nanjing | 79 || 3 | stu03 | beijing | 90 || 4 | stu04 | | 60 |+----+-------+----------+-------+4 rows in set (0.00 sec)
5、修改与删除表中数据
mysql> update info set address='hangzhou' where id=4 and name='stu04'; //修改id为4的address为“hangzhou”Query OK, 1 row affected (0.04 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from info; //查看表中数据+----+-------+----------+-------+| id | name | address | score |+----+-------+----------+-------+| 1 | stu01 | shanghai | 88 || 2 | stu02 | nanjing | 79 || 3 | stu03 | beijing | 90 || 4 | stu04 | hangzhou | 60 |+----+-------+----------+-------+4 rows in set (0.01 sec)mysql> delete from info where name='stu04'; //删除表中name为“stu04”的数据Query OK, 1 row affected (0.02 sec)mysql> select * from info; //查看表中数据+----+-------+----------+-------+| id | name | address | score |+----+-------+----------+-------+| 1 | stu01 | shanghai | 88 || 2 | stu02 | nanjing | 79 || 3 | stu03 | beijing | 90 |+----+-------+----------+-------+3 rows in set (0.00 sec)
6、删除表和数据库
mysql> drop table info; //删除表infoQuery OK, 0 rows affected (0.06 sec)mysql> show tables; //查看表,删除成功Empty set (0.00 sec)mysql> drop database school; //删除数据库Query OK, 0 rows affected (0.04 sec)mysql> show databases; //查看数据库,删除成功+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys |+--------------------+4 rows in set (0.00 sec)
7、设置、查看与撤销用户权限
mysql> show grants for 'root'@'%'; //查看权限+-------------------------------------------------------------+| Grants for root@% |+-------------------------------------------------------------+| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION |+-------------------------------------------------------------+1 row in set (0.00 sec)mysql> revoke all on *.* from 'root'@'%'; //删除权限Query OK, 0 rows affected (0.03 sec)mysql> show grants for 'root'@'%'; //查看权限+----------------------------------------------------+| Grants for root@% |+----------------------------------------------------+| GRANT USAGE ON *.* TO 'root'@'%' WITH GRANT OPTION |+----------------------------------------------------+1 row in set (0.00 sec)mysql> grant all on *.* to root@'%' identified by 'abc123'; //添加权限Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> show grants for 'root'@'%'; //查看权限+-------------------------------------------------------------+| Grants for root@% |+-------------------------------------------------------------+| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION |+-------------------------------------------------------------+1 row in set (0.00 sec)mysql>
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。