MySQL数据库

来源:互联网 发布:男士 长袖 知乎 编辑:程序博客网 时间:2024/06/09 16:16
使用MySQL数据库:
1.创建数据库
create database 数据库名 [其他选项];
例:
create database db_rookie character set utf8;


2.查看数据库
show databases;


3.选择数据库
use 数据库名;
例:
use db_rookie;


4.删除数据库
drop database 数据库名;
drop db_rookie;


5.创建数据表
create table table_name(列名1 属性, 列名2 属性 ...);
例如:
create table tb_student
(
id int unsigned not null auto_increment primary key,
name char(30) not null,
sex char(4) not null
);


如果将上面代码保存到当前目录的craete_table.sql文件里,使用下面命令可以执行。
mysql -D db_rookie -u root -p < craete_table.sql;




操作MySQL数据库:


6.向表中插入数据
insert [into] 表名 [(列名1, 列名2...)] values (值1, 值2 ...);
例:
insert into tb_student values(null, "rookie", "男");


7.查询表中的数据
select 列名 from 表名 [查询条件];
例:
select name, sex from tb_student;


8.按特定条件查询表中的数据
select 列名 from 表名 where 条件;
例:
select * from tb_student where sex="男";
条件可以使用=,>,<,>=,<=,!=,is [nut] null,in,like,or, and等
如:
select * from tb_student where name like "%ook%";


9.更新表中的数据
update 表名 set 列名=新值 where 更新条件;
例:
update tb_student set sex="女" where name like "%ook%";


10.删除表中的数据
delete from 表名 where 删除条件;
例:
delete from tb_student where name = "rookie";




创建后表的修改:
11.添加列
alter table 表名 add 列名 列数据类型 [after 插入位置];
例:
alter table tb_student add birthday date after sex;


12.修改列
altar table 表名 change 列名 列新名 新数据类型;
例:
alter table tb_student change birthday address char(20);


13.删除列
alter table 表名 drop 列名;
例:
alter table tb_student drop address;


14.重命名表
alter table 表名 rename 新表名;
例:
alter table tb_student rename tb_teacher;


15.删除表
drop table 表名;


16.删除数据库
drop database 数据库名;


17.查看数据库里所以表
show tables from 数据库名;
0 0
原创粉丝点击