MySQL基本语法学习

来源:互联网 发布:2017基本宏观经济数据 编辑:程序博客网 时间:2024/05/16 07:40
登录MySQL服务器mysql -u root -p创建数据库create database samp_db character set gbk;查询所有数据库show databases;创建表use samp_db;create table students(    id int unsigned not null auto_increment primary key,    name char(8) not null,    sex char(4) not null,    age tinyint unsigned not null,    tel char(15) null default "-");向表中插入数据insert into students values(NULL,"王刚" ,"男", 20,"13811371377");insert into students values(NULL,"孙丽华" ,"女", 22,"18256973756");insert into students values(NULL,"王永恒" ,"男", 23,"18812345678");insert into students values(NULL,"李白" ,"男", 33,"17089898989");查询表中的数据select * from students;select name from students;条件查询select * from students where sex="男";(查询students表中男同学)select * from students where age=20;(查询students表中年龄为20的学生)更新表中的数据update 表名称 set 列名称=新值 where 更新条件;update students set age=21 where name="王刚";(将王刚的年龄改成21)update students set tel=default where id=2;(将id为2的同学的手机号变成默认值"-")update students set name="张永恒",tel="1821324679" where name="王永恒";(将王永恒的名字改成张永恒,手机号改成18213245679)删除表中的数据delete from 表名称 where 删除条件delete from students where id=4;(删除id=4的这个学生的数据)delete from students;(删除表中所有数据)添加列alter table 表名 add 列名 列数据类型 [after 插入位置];alter table students add address char(60);(往students表中插入address列)alter table students add birthday date after age;(向students表的age列之后插入birthday列)修改列alter table 表名 change 列名称 列新名称 新数据类型;alter table students change tel telphone char(15) default "-";(将tel列名修改为telphone)删除列alter table 表名 drop 列名称;alter table students drop birthday;(删除students表中的birthday列)重命名表alter table 表名 rename 新表名;alter table students rename workmates;(将students表名修改为workmates)删除整张表drop table 表名;drop table workmates;(删除workmates表)删除数据库drop database 数据库名drop database samp_db;(删除samp_db数据库)
原创粉丝点击