首页 > > > MySQL的简单运用——增、删、改、插入
数据是数据库中存储的基本对象。数据的种类有很多种,文本(Text)、图形(graph)、图像(lmage)、音频(Audio)、视频(Video)、各种档案记录等等。这都是属于数据的部分。数据表使用前需要自己创建一个。
在数据库中,大多数时,表的“列“称为”字段“,每个字段包含某一专题的信息。 有时候,字段也不代表数据表中的列。
下载可以访问此链接根据自己的电脑及要求来下载
一、如何登陆数据库服务器 格式:
|mysql |-h 主机地址 |-u 用户名 |-p 用户密码| |:------:| :---------------:| :--------------:| :--------------:| | |hostaddress|username| password |
mysql -u用户名 -p密码 #登录1、例1:连接到本机上的MYSQL。
首先在打开DOS窗口,然后进入目录 mysql bin,再键入命令mysql -uroot -p,回车后提示你输密码,如果刚安装好MYSQL,超级用户root是没有密码的,故直接回车即可进入到MYSQL中了,MYSQL的提示符是: mysql>。 2、例2:连接到远程主机上的MYSQL。 假设远主的IP为:128.60.0.60,用户名为root,密码为abcd。则键入以下命令:
mysql -h 128.60..60 -uroot -abcd==(注:u与root可以不用加空格,其它也一样)== 二、退出MYSQL命令 exit (回车)。 ## 如何查询数据库服务器中所有的数据库 mysql> show databases;
create database name; 创建数据库
use databasename; 选择数据库 mysql> mysql> use mysql; drop database name 删除数据库,(直接不提醒)
show tables; 显示表 describe tablename; 表的详细描述
select中加上distinct 去除重复字段
mysqladmin drop database name 删除数据库前,(有提示) 例如:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (.00 sec)
mysql> mysql> use mysql;
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (.00 sec)(注意:和上面不同,下面的因为是MySQL环境中的命令,所以后面都带一个分号作为命令结束符)
格式:grant select on 数据库.* to 用户名@登录主机 identified by \"密码\"
例1、增加一个用户test1密码为abc,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入MySQL,然后键入以下命令:
grant select,insert,update,
delete on *.* to test2@localhost identified by \"abc\";==如果你不想test2有密码,可以再打一个命令将密码消掉。==
grant select,insert,update,delete on mydb
.* to test2@localhost identified by \"\";格式:mysqladmin -u用户名 -p旧密码 password 新密码
例1:给root加个密码233333。首先在DOS下进入目录mysql bin,然后键入以下命令:
mysqladmin -uroot -password 233333==注:因为开始时root没有密码,所以-p旧密码一项就可以省略了。==
例2:再将root的密码改为_kingho_。
mysqladmin -uroot -pab12 password kinghocreate database staffer1)创建数据库staffer
mysql> create database staffer
2)创建表staffer,department,position,depart_pos
create table department
( id int not null auto_increment,
name varchar(20) not null default '系统部', #设定默认值
description varchar(100),
primary key PK_department (id) #设定主键
);
create table depart_pos
(
department_id int not null,
position_id int not null,
primary key PK_depart_pos (department_id,position_id) #设定复和主键
);
create table staffer
(
id int not null auto_increment primary key, #设定主键
name varchar(20) not null default '无名氏', #设定默认值
department_id int not null,
position_id int not null,
unique (department_id,position_id) #设定唯一值
);
3)删除
mysql> drop table depart_pos;
mysql> Create DATABASE MYSQLDATA;
mysql> USE MYSQLDATA; (按回车键出现Database changed 时说明操作成功!)
mysql> SHOW TABLES;
mysql> Create TABLE MYTABLE (name VARCHAR(20), sex CHAR(1));
显示表的结构:
mysql> DESCRIBE MYTABLE;
往表中加入记录
mysql> insert into MYTABLE values ("hyq","M");
用文本方式将数据装入数据库表中(例如D:/mysql.txt)
mysql> LOAD DATA LOCAL INFILE "D:/mysql.txt" INTO TABLEMYTABLE;
导入.sql文件命令(例如D:/mysql.sql)
mysql>use database;
mysql>source d:/mysql.sql;
删除表
mysql>drop TABLE MYTABLE;
清空表
mysql>delete from MYTABLE;
阅读原文戳这里-->
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3134036/ ,如需转载,请注明出处,否则将追究法律责任。