Mtsql:常用代码Create table CeShi1( Uid varchar(50) primary key, Pwd varchar(50), Name varchar(50) Nation varchar(50), foreign key (Nation) references Nation(Code) //加外键关系 //references 引用 )写查询语句需要注意:1.创建表的时候,最后一列后面不要写逗号。2.如果有多条语句一起执行,注意在语句之间加分号分隔。3.写代码所有符号都是半角的。关系型数据库:表和表之间是有关系存在的创建表的几个关键字;1.主键:primary key2.非空: not null3.自增长列:auto_increment4.外键关系:foreign key (列名) references 表名(列名)CRUD操作: 增删改查1.添加数据:Insert into 表名 values(‘’,‘’,‘’,‘’)要求values 括号里面的值的个数要求和表里面的列数相同Insert into Info (Code,Name) values(‘’,‘’) 添加指定列的值2.修改数据:Update info set Name=’张三’ where Code = ’p001’3.删除数据:Delete from info where Code = ‘p001’4.查询数据:1.普通查询:查所有的Select *from info #查所有数据Select Code,Name from info #查指定列2.条件查询Select *from info where Code =’p001’ #一个条件Select *from info where Name =’张三’ and Nation =’n001’ #两个条件并的关系Select *from info where Name =’张三’ or Nation =’n001’ #两个条件或的关系3.排序查询Select *from info order by Birthday #默认升序排列asc 若果要降序排列 descSelect *from car order by Brand,Oil desc #多列排序4.聚合函数Select count(*) from info #取个数Select sum(price) from car #查询price列的和Select avg(price) from car #查询price列的平均值Select max(price) from car #查询 price列的最大值Select min(price) from car #查询 price列的最小值5.分页查询Select *from car limit n,m #跳过n条数据取m条数据 6.分组查询Select brand from car group by brand #简单分组查询Select brand from car group by brand having count(*)>2 #查询系列里面车的数量大于2的系列7.去重查询Select distinct brand from car8.修改列名Select brand as ‘系列’ from car9.模糊查询Select *from car where name like ‘奥迪%’ # %代表任意多个字符Select *from car where name like ‘_迪%’ # _代表一个字符 10.离散查询Select *from car where code in (‘c001’,’c002’,’c003’,’c004’)Select *from car where code not in (‘c001’,’c002’,’c003’,’c004’)