mysql 学习 day1
- 一般在创建数据库或表之前,需要保证不重名;
- 每条语句后都需要加 “;”
- 主键不能为空;
- 主键可以是具体的值,也可以是null,也可以是default;
常用命令:
-----显示所有的数据库: show databases;
-----创建数据库:create database 名字 charset=utf8;
-----删除数据库:drop database 名字;
-----显示当前数据库时间:select now();
-----当前数据库版本:select version();
-----查看创建数据库的语句:show create database 名字;
-----查看当前数据库:select databbese(); 若显示为null,表示当前未进入任何一个数据库
-----进入某个数据库:use 数据库;
-----创建表:create table 表名 (字段 类型 约束[,字段 类型 约束])
=> create table stu(id int,name varchar(20),int age,gender enum('男',‘女’,‘保密’) default '保密' );
-----查看表:show tables;
-----查看表的信息:desc 表名;
-----在表中插入信息:insert 表名 into values(创建表字段时的对应具体值) => insert stu into values(0,张三,33,'男')
-----查询表中的数据:select * from 表名;=====适用于数据较小的情况下
select * from table where 条件;=====数据较大,精确查找相关信息
-----查询指定列:select 列1,列2,..... from 表名;
-----查询指定列,使用as指定名字:select 列1 as 名1,列2 as 名2,..... from 表名;
-----修改表_添加字段:
alter table 表名 add 列名 类型;
=> alter table stu add birthday datetime;
-----修改表_修改字段(重命名版):
alter table 表名 change 原名 新名 类型及约束;
=>alter table stu change birthday birth datetime not null;
------修改表_修改字段(不重命名版):
alter table 表名 modify 列名 类型及约束;
=> alter table stu modify birth date not null;
-----删除表字段:alter table 表名 drop 列名;
-----删除表:drop table 表名;
-----部分插入:insert into 表名 (列名1,列名2,...) values (值1,值2,...);
-----多行部分插入:insert into 表名 (列名1,列名2,...) values (值1,值2,...), (值1,值2,...);
-----多行全部插入:insert into 表名 values (值1,值2,...), (值1,值2,...);
-----修改:update 表名 set 列名1=值1,... where 条件;
-----物理删除某条信息:delete from 表名 where 条件; =====按条件删除
------select s.name,s.gender from singers as s;(简便写法,通过给表起别名)
-----消除重复行:select distinct 字段 from 表名;