show databases #列出 MySQL 数据库管理系统的数据库列表
show tables #显示指定数据库的所有表
show columns from test_kettle #显示数据表的属性,属性类型,主键信息 ,是否为 NULL,默认值等其他信息。
show index from test_kettle #显示数据表的详细索引信息,包括PRIMARY KEY(主键)
show table status from test #显示数据库 RUNOOB 中所有表的信息
show table status from test like 'test%' #表名以test开头的表的信息
show table status from test like 'test%'\G; #加上 \G,查询结果按列打印
mysql -u root -p /*mysql -u 用户名 -p 密码 是连接数据库bai服务器的命令du。要求你输入自己连zhi接数据库的用户名和密码。考虑密码如果直接明文dao写在这条命令行上,有些不方便(怕被别人看到),可以像你写的那样,只输入:mysql -u 用户名 -p 然后回车,此时提示你输入密码,这时候输入的密码就不再是明文的了.*/
create DATABASE test2 #创建数据库
drop database test2 #删除数据库
use test2 #选择要操作的Mysql数据库,使用该命令后所有Mysql命令都只针对该数据库。
CREATE TABLE student (
id INT NOT NULL AUTO_INCREMENT,
NAME VARCHAR (20) NOT NULL,
sex VARCHAR (1) DEFAULT "男",
age VARCHAR (10) NOT NULL,
sub_date datetime DEFAULT now(),
PRIMARY KEY (id)) ENGINE = INNODB DEFAULT CHARSET = utf8;#创建数据表
/*1.如果你不想字段为 NULL 可以设置字段的属性为 NOT NULL, 在操作数据库时如果输入该字段的数据为NULL ,就会报错。
2.AUTO_INCREMENT定义列为自增的属性,一般用于主键,数值会自动加1。
3.PRIMARY KEY关键字用于定义列为主键。 您可以使用多列来定义主键,列间以逗号分隔。
4.ENGINE 设置存储引擎,CHARSET 设置编码。*/
insert into student (name,sex,age) values ('zhangsan', '女', '18') #添加表数据
drop table students #删除数据库表结构
truncate table student #是删除表中所有数据,但不能与where一起使用
delete from student where id=1 #删除表数据,可以与where连用,删除特定行
select * from student where id=1 limit 1 #查询数据
/*查询语句中你可以使用一个或者多个表,表之间使用逗号(,)分割,并使用WHERE语句来设定查询条件。
1.SELECT 命令可以读取一条或者多条记录。
2.你可以使用星号(*)来代替其他字段,SELECT语句会返回表的所有字段数据
3.你可以使用 WHERE 语句来包含任何条件。
4.你可以使用 LIMIT 属性来设定返回的记录数。*/
update student set name='lisi' where name='zhangsan' #修改/更新某列的某个值
update student set sex='男' #修改/更新某列的所有值
update student set name='lisi',sex='女',age=20 where id=2 #修改/更新id=2时,name,sex,age列的值
alter table student drop id #删除某表中的某个字段
alter table student add id int not null auto_increment primary key #添加表中的某个字段,并定义数据类型(执行完自动添加到数据表字段的末尾)。
alter table student add id int not null auto_increment primary key after age#添加表中的某个字段,定义数据类型,并指定在某列之后。
alter table student add id int not null auto_increment primary key first #添加表中的某个字段,并制定在某列之后。
#添加表中的某个字段,定义数据类型,并指定在首列。
alter table student modify name char(5) #修改表中的某个字段的类型
alter table student rename to students #修改表名
alter table students change sex gender varchar(2) #修改表中某个字段名及字段类型。
alter table students alter gender set default '女' #修改表中某个字段的默认值
alter table students alter gender drop default #删除表中某个字段的默认值
create index id on students(id) #创建普通索引
alter table students add index name(name) #添加索引
drop index name on students #删除索引
alter table students drop index name #删除索引
create table copy_students1 select * from students where 1=2 #只复制表结构到新表
create table copy_students2 like students #只复制表结构到新表
create table copy_students3 select * from students #复制表结构及数据到新表
insert into copy_students1 select * from students #复制旧表的数据到新表(假设两个表结构一样)
insert into copy_students2(id,name,age) select id,name,age from students#复制旧表的数据到新表(假设两个表结构不一样)
alter table students drop id;
alter table students add id int NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST; #重置序列
mysql数据库基础练习
最新推荐文章于 2024-05-19 03:18:57 发布