Mysql 常用语句汇总,表常用操作

进入数据库:

use databasename;

实例:use test_db

查看数据库中的表:

show tables;

创建表:

create table if not exists tablename(

column_list

)engine=table_type;

tablename是表的名字,必须是数据库中唯一。

if not exists 可选语句,防止表名在库中已经存在。

column_list是指定表的列表。字段的列用逗号分隔。

engine=table_type指定引擎。

实例:

create table if not exists teachers(

id int(11) not null auto_increment;

name varchar(45) default null,

email varchar(255) default null,

address varchar(255) default null,

primary key(id)

)engine=InnoDB;

解读:

if not exists 可选语句,防止表名重复

id 列名,int(11)数据类型

varchar数据类型,default默认值

primary key()主键

engine=InnoDB table使用的引擎

插入数据:


insert into tablename values(1,valuse,values,......);

实例:insert into teachers values(1,'zhangsan','1111@qq.com','beijing');

另一种写法 insert teachers values(1,'lisi','333@qq.com','shanghai');

不带into也可以

插入多行:

insert teachers values(3,'zhaowu','3333@qq.com','chongqing'),
-> (4,'shenlu','5555@qq.com','hebei');

insert teachers values(0,'....','....','.....');

0 可以根据顺序插入数据

使用select插入数据:

insert into tablename(c1,c2) 

select f1,f2 from tablename

删除表:

图中1,是删除整个表

图中2,是删除指定行的数据

alert table 表名 drop column 列名;

查询数据:

select * from tablename; 查询整个table数据

原文地址:https://www.cnblogs.com/zuodaozhudemeng/p/7694076.html