您的位置:首页 > 数据库

My SQL 基础常用语法

2015-10-09 21:12 513 查看
一、 Mysql常用指令
DOS:


登录:>mysql -h主机名称/IP地址 -u用户名 -p密码

查看当前mysql服务中所有数据列表:show databases;

使用某个数据库:use 数据库名称;

查询当前数据库中所有表:show tables;

查询表的结构:desc 表名;


二、数据操作的基本语法:


补充:char varchar Integer int,float,timestamp,date.....

create database 数据库名称; -- 创建数据库

use 数据库名称; --使用数据库

create table 表名

(
字段名称 字段类型 约束,
字段名称1 字段类型 约束,
字段名称2 字段类型 约束,
字段名称3 字段类型 约束

);

约束:[非空,检查,默认,主键,外键,唯一]

alter table 表名
add constraint 约束名 字段;

insert into 表名(字段1,字段2....字段n) values(值1,值2....值n);

delete from 表名 where 删除的条件;

update 表名 set 字段1=值1,字段n=值n where 修改的条件

select 筛选的字段 from 表名 where 条件;

--------------------具体脚本----------------
-- 创建数据库
create database myDatabase;
-- 使用数据库
use myDatabase;
-- 创建表
create table Student
(
stuId int not null,
stuName varchar(20) not NULL,
stuSex  char(2) not null,
stuAge int not NULL,
stuBirthday timestamp not null
) auto_increment=100001;
---------- 为表添加约束--------------
-- 添加主键约束
alter table Student 
add constraint PK_STUID primary key(stuId);
-- 添加唯一约束
alter table Student
add constraint UQ_STUNAME unique(stuName);
-- 添加检查约束【mysql中不生效】
alter table Student
add constraint CK_STUSEX check(stuSex in ('男','女'));
-- 修改字段
alter table student modify column stuBirthday timestamp not null default now();
-- 添加字段
alter table student add column stuQQ varchar(12) not null;
-- 修改主键为自动增长策略
alter table student modify column stuId int not null auto_increment;

-- 插入测试数据
insert into student values(1001,'张一','人妖',20,null,'2713346');
insert into student(stuId,stuName,stuSex,stuAge,stuQQ) values(1002,'小三','女',30,'5464646');
insert into student values(null,'张二','人妖',20,null,'2713346');
--  批量插入数据
insert into student values(null,'张一','人妖',20,null,'2713346'),(null,'张二','人妖',20,null,'2713346'),(null,'张三','人妖',20,null,'2713346'),(null,'张四','人妖',20,null,'2713346');

-- 修改数据
update student set stuName='新同学',stuSex='女' where stuId=1004;

-- 删除数据
delete from student where stuid=1006;

-- 查询(单表查询)
select * from student;
-- 查询编号大约1004以上的学生姓名及性别
select stuName 姓名,stuSex 性别 from student where stuid>1004;
-- select 姓名=stuName,性别=stuSex from student where stuid>1004;
仅SQLServer可用

-- 分页查询
   
select * from student limit 0,2;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql mysql dos