mysql的一些基本操作
一、连接MySQL服务器
输入指令:MySQL -uroot -h127.0.0.1 -p111(-h127.0.0.1看个人情况输不输)
注:用户名为“root”,MySQL数据库服务器地址为“127.0.0.1”,密码为“111”,三者之间必须有空格。
二、操作MySQL数据库
1、创建数据库
create database 数据库名;
2、查看数据库
show databases;
3、选择指定数据库
use 数据库名;
4、删除数据库
drop database 数据库名;
注:自动删除MySQL安装目录中的“C:/AppServ/MySQL/data”文件夹。
三、操作MySQL数据表
1、创建表
create table 表名 (column_name column_type not null,...)
属性 | 说明 | 属性 | 说明 |
column_name | 字段名 | Primary key | 该列是否为主码 |
column_type | 字段类型 | AUTO_INCREMENT | 该列是否自动编号 |
Not null | null | 该列是否允许为空 |
创建数据表后,“C:\AppServ\MySQL\data\数据库名\”中自动创建对应表文件(“表名.frm”,“表名.MYD”,“表名.MYI”)
2、查看数据库中的表
show tables;
3、查看数据库中所有的表
show tables;(前提是使用use database 数据库;)
4、查看数据表结构
describe 表名;
5、修改数据表结构
alter table 表名
add [column] create_definition [first | after column_name] //添加新字段
add primary key (index_col_name,...) //添加主码名称
alter [column] col_name {set default literal |rop default} //修改字段名称
change [column] old_col_name create_definition //修改字段名及类型
modify [column] create_definition //修改字段类型
drop [column] col_name //删除字段
drop primary key //删除主码
rename [as] new_tablename //更改表名
eg:alter table Admin_Info
drop A_Pwd,
rename as Admin_Info2;
6、删除指定数据表
drop table 表名;
四、操作MySQL数据
1、添加表数据
语法1:insert into 表名 values(值1,值2,...)(自增长的列应写null)
语法2:insert into 表名(字段1,字段2,...) values (值1,值2,...)
语法3:insert into 表名 set 字段1=值1,字段2=值2,...
2、更新表数据
update 表名 set 字段1=值1 where 查询条件
若无查询条件,表中所有数据行都会被修改。
3、删除表数据
delete from 表名 where 查询条件
若无查询条件,表中所有数据行都会被删除。
4、查询表数据
select * from 表名;
5、限制查询记录数
select * from 表名 limit[start] length
start:表示从第几行记录开始输出,0表示第1行
pythy中的mysql操作:
import pymysql # 创建连接 conn = pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='',db='student',charset='utf8') # 创建游标 cursor = conn.cursor() insdata=[('Jack',12,'2014-09-08'),('DEll',4,'2012-09-08'),('wuwei',42,'2011-09-08')] # 执行SQL,并返回收影响行数,多次执行。 effect_row=cursor.executemany("insert into cj (name,age,reg_date)values (%s,%s,%s)",insdata) effect_row = cursor.execute("select * from cj") print (cursor.fetchall()) # 执行SQL,并返回受影响行数 effect_row = cursor.execute("update cj set name = 'Alxx' where cj_id = 1") # 提交,不然无法保存新建或者修改的数据 conn.commit() # 关闭游标 cursor.close() # 关闭连接 conn.close()